If I'm given a number of vertices/edges, how can I dynamically draw a polygon?
Code:function drawPolygon($edges) {
imagepolygon(/* ... */);
}
Printable View
If I'm given a number of vertices/edges, how can I dynamically draw a polygon?
Code:function drawPolygon($edges) {
imagepolygon(/* ... */);
}
You will need to have the GD extension / enabled or installed to do this. The function you need is simiar to the one in VB.
http://uk2.php.net/manual/en/functio...ledpolygon.php
or
http://uk2.php.net/manual/en/function.imagepolygon.php
I know how to use the function, that's not the problem. What is the problem is that I'm going to have an unknown number of edges.Quote:
Originally Posted by visualAd
The function I outlined above, takes $edges as a parameter, which is an integer, 2 to...say 15, or whatever. This number will tell how many edges the polygon I have to draw is. Rather than hard coding the (x,y) coords for each possibile polygon, is there a way to do this dynamically?
I don't see that being a problem. As long as the array containing the points, contains only the co-ordintates for the for the polygon, you can obtain the number of points by using the array count:
PHP Code:$points = (int) (count($pointarray) / 2);
You're not understanding me. I don't have an array containing the points. I need to build that array based on how many points I'll have.Quote:
Originally Posted by visualAd
Ie, all I know is how many points/edges.
You'll need to know one other thing, the shape of the polygon. Given the first point and the basic shape, you can work out the other points relative to the first.
I.e: If you shape has 4 points / sides, how will you know where to put these? If however you know that it has 4 sides and is a diamond, you can work out where to place the other points from the first and build the array appropriatley.
This will draw a diamond:
PHP Code:<?php
function draw_diamond($x, $y, $length, $image, $color)
{
$point_array = Array($x, $y,
$x + $length, $y + $length,
$x, $y + (2 * $length),
$x - $length, $y + $length);
imagepolygon($image, $point_array, 4, $color);
}
?>
That's what I was wondering. Whether or not I'd have to write a function for each number. That'd be a lot of functions. Triangle, diamond, pentagon, etc. What if the number is 22? I don't even know the name for that.
I was hoping for a super cool method that could take care of it.
I played about with something like this before but never got very far with it. Heres the code I had, its not brilliant but it might give you an idea?
Hope it at least gives you an idea of how i was thinking of doing itPHP Code:$Image_Width = 450;
$Image_Height = 450;
$temp = @imagecreate($Image_Width, $Image_Height);//create our image
$background_color = imagecolorallocate($temp , 255, 255, 255);//fill the background
$color = imagecolorallocate($temp , 0, 0, 0);//create our pencolour
$point_array = GetPoints(5);
imagepolygon($temp, $point_array, count($point_array)/2, $color);
imagejpeg($temp,'',100);
imagedestroy($temp);
function GetPoints($sides)
{
$d = 360 / $sides;
$d = 180 - $d;
$y = 100;
$x = 100;
$points[]=$x;
$points[]=$y;
$sidelength = 100;
$rotation = $d;
for($i = 1; $i < $sides;$i++)
{
$x = Cos(DegToRad($rotation ))* $sidelength + $x;
$points[] = $x;
$y= Sin(DegToRad($rotation ))* $sidelength + $y;
$points[] =$y;
$rotation += $d;
}
return $points;
}
function DegToRad($var)
{
return M_PI * $var/ 180.0;
}