Jump to content

draw dynamic polygons


AviNahum

Recommended Posts

hey...

im making a simple game, and im trying to draw a dynamic polygon around each star in the game to show the stars area.

for example, lets say "STAR A" positioned on X=50,Y=100 on the map, in the database i have a colum that determine the star's surended area for example, area=100; is that posible to darw a polygon around each star depending on the star area on the DB? another think is that each polygon shuold be in diffrent color. im not asking for the full soultion, but if someone can please give me some directions how to do it... im not good at math :|

i made an image to demostarate what im trying to reach...

 

Thanks in advance...


post-71948-0-69375800-1363704545_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/275867-draw-dynamic-polygons/
Share on other sites

If you are working within an image you can use the GD functions to draw lines...

 

http://php.net/manual/en/function.imageline.php

 

you would have to figure out exactly where each line begins and ends (x,y coordinates), and you can specify color.

 

hope this helps.

found the 'drawStar' function below from stack overflow user licson, made this script draw random stars... is this more of what you need?

Edit: sorry, misread what you said.... this draws stars. still a good function though, lol. How do you plan to determine the boundaries, or the dynamics of each polygon?

 

// function grabbed from user "licson" on stackoverflow.com
function drawStar($img,$x,$y,$radius,$sides,$color,$spikness=0.5)
{
   $point =array();
   $t = 0;
   for($a = 0;$a <= 360;$a += 360/($sides*2))
   {
        $t++;
        if($t % 2 == 0)
        {
            $point[] = $x + ($radius * $spikness) * cos(deg2rad($a));
            $point[] = $y + ($radius * $spikness) * sin(deg2rad($a));
        }else{
            $point[] = $x + $radius * cos(deg2rad($a));
            $point[] = $y + $radius * sin(deg2rad($a));
        }
    }
    return imagefilledpolygon($img,$point,$sides*2,$color);
}
 
$im = imagecreate(500, 500);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
 
for($i=0; $i<10; $i++)
{
    drawStar($im, rand(0,500), rand(0,500), rand(20,50), rand(5, , $white);
}
 
// output image to browser
header('Content-Type: image/jpeg');
imagejpeg($im,"",100);
 
// destroy the image
imagedestroy($source);
imagedestroy($im);

i'll explain better...

i have a div with a background, i call it the galaxy...

inside the galaxy div there a few div's with absalute position, i call each div like this a star...

i need that each star will be surounded with a polygon as i demostrated in the picture...

Your example showed neighbouring areas having common boundaries and you also state the area of the polygon must be as specified in the table. I'm not sure these two can both be achieved.

 

If stars are placed randomly, what prevents a star being placed within the area of another star?

 

Are all stars in place before drawing the boundaries?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.