|
-
Mar 18th, 2005, 07:37 PM
#1
Thread Starter
Stuck in the 80s
Draw a Polygon on the fly?
If I'm given a number of vertices/edges, how can I dynamically draw a polygon?
Code:
function drawPolygon($edges) {
imagepolygon(/* ... */);
}
-
Mar 19th, 2005, 05:18 PM
#2
Re: Draw a Polygon on the fly?
-
Mar 19th, 2005, 10:31 PM
#3
Thread Starter
Stuck in the 80s
Re: Draw a Polygon on the fly?
 Originally Posted by visualAd
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.
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?
-
Mar 20th, 2005, 04:57 AM
#4
Re: Draw a Polygon on the fly?
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);
-
Mar 20th, 2005, 04:42 PM
#5
Thread Starter
Stuck in the 80s
Re: Draw a Polygon on the fly?
 Originally Posted by visualAd
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.
Ie, all I know is how many points/edges.
-
Mar 20th, 2005, 05:20 PM
#6
Re: Draw a Polygon on the fly?
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);
}
?>
-
Mar 20th, 2005, 07:25 PM
#7
Thread Starter
Stuck in the 80s
Re: Draw a Polygon on the fly?
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.
-
Mar 21st, 2005, 09:16 AM
#8
<?="Moderator"?>
Re: Draw a Polygon on the fly?
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?
PHP 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; }
Hope it at least gives you an idea of how i was thinking of doing it
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|