AviNahum Posted March 19, 2013 Share Posted March 19, 2013 (edited) 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... Edited March 19, 2013 by aviavi Quote Link to comment Share on other sites More sharing options...
fooDigi Posted March 19, 2013 Share Posted March 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
AviNahum Posted March 19, 2013 Author Share Posted March 19, 2013 Thanks... i know how to use the GD library, my problem is doing the math... Quote Link to comment Share on other sites More sharing options...
Barand Posted March 19, 2013 Share Posted March 19, 2013 The easiest way would be to pre-define the polygon for each star and store as a string of x,y, coordinate pairs x1,y1,x2,y2,x3,y3....xn,yn Quote Link to comment Share on other sites More sharing options...
AviNahum Posted March 19, 2013 Author Share Posted March 19, 2013 The easiest way would be to pre-define the polygon for each star and store as a string of x,y, coordinate pairs x1,y1,x2,y2,x3,y3....xn,yn thr problem is that the number of stars and their positions are dynamic... Quote Link to comment Share on other sites More sharing options...
fooDigi Posted March 19, 2013 Share Posted March 19, 2013 (edited) 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); Edited March 19, 2013 by fooDigi Quote Link to comment Share on other sites More sharing options...
AviNahum Posted March 19, 2013 Author Share Posted March 19, 2013 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... Quote Link to comment Share on other sites More sharing options...
fooDigi Posted March 19, 2013 Share Posted March 19, 2013 Oh, this is with html divs? Sorry, can't help further... maybe html5 and canvas? good luck! Quote Link to comment Share on other sites More sharing options...
AviNahum Posted March 19, 2013 Author Share Posted March 19, 2013 i have no problem to draw something on the screen, my main problem is to do the math and get the X,Y points... Quote Link to comment Share on other sites More sharing options...
Barand Posted March 19, 2013 Share Posted March 19, 2013 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.