Random MAP coordinates?

Hi all,
Need some assistance with this concept: I have a map graphic and it has fixed Rect coordinate ‘hotspots’ on it and the graphic is 650x447 in size. What I would like to do is create 25 Rect hotspots that are random every time rather than fixed. Is it possible?

Here’s my current code:

[php]Print "

















<area shape=‘rect’ coords=‘138,410,203,475’href=’?act=search&search=".$Type."’>








[/php]

I would appreciate some help with this. Thanks!
Derek

Derek,

See if this is what you are looking for…[php]<?php
$xmin=0;
$xmax=650;
$ymin=0;
$ymax=447;
$minxsz=5;
$minysz=5;

echo "
";

for($i=0;$i<25;$i++)
{
$x1 = rand($xmin,$xmax-$minxsz);
$x2 = rand($x1+$minxsz,$xmax);
$y1 = rand($ymin,$ymax-$maxysz);
$y2 = rand($y1+$minysz,$ymax);

echo "<area shape='rect' coords='$x1,$y1,$x2,$y2' href='?act=search&search=".$Type."'>";

}
echo ‘’;[/php]

Works great! Wow, you’re the PHP coder I hope to be one day. :slight_smile:

Thanks! I’m glad it worded OK. I appreciate the nice compliment, but I’m not sure I deserve it!

I set it up so that all rectangles should be at least $minxsz wide by $minysz high. I also set it up to use variables for the min and max values. This way the same code can be easily adjusted to fit whatever image you are using. Lastly, it makes sure that x2>x1 and y2>y1. If you have no need for any of these options, you can skip all the variables and just plug your dimensions directly into the rand() function.

If you wanted to get really fancy, you could also get the image dimensions and use them to automatically populate the $xmax and $ymax.

Best,

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service