When you talk about the length of a pentagon, we’re assuming you’re talking (a) a regular pentagon and (b) length is the length of a side, not a diagonal, correct?
Now then, knowing the length of a side, it is possible to find the length of a diagonal. Two diagonals would cross in the center. Hence, now that we’ve found the center, we can find where the five vertices must be.
Or, knowing the length of a side, we also know the measure of the interior angles (i.e.: the measure of each included angle formed by two adjacent sides). We know this because there is a relationship for a regular polygon between number of sides and measure of the interior angle: IIRC, it is: A = (360 * (n-2))/2
where A = angle in degrees and n = number of sides. - Jinx
There are several ways to tackle this, but one way to find the coorindates of the vertices might be:
Using the info above, if you want to find specific coordinates for the vertices, you could then use the point-slope method to find the coordinates. In other words, we can assume the center is at the origin (0,0). Now, we also know the length of a diagonal and we can find the slope of a diagonal which will then allow us to find the coordinates of the endpoints (i.e.: the vertices)
Or, once we find the coordinates for one vertex, we can work our way around the pentagon (using the length of a side and the slope of each side) to find the coordinates for remaining vertices. Remember what algebra taught you about equations for a line given two points, or a point and the slope. At worst, you might need to fiddle with the distance between two points formula, but I believe this can be worked around - to avoid overcomplicating the problem needlessly. - Jinx
Don’t forget: Using algebra alone, you’ll chase your tail to find the slope of a line if you don’t know the endpoints. But, using the trig value for the tangent of the angle for the line will give you the slope. Thinking back to the “unit circle”, recall tan() is defined as sin()/cos()…which is rise over run, in algebra lingo!
Nitpick: you also need to specify the orientation. How about having a point on the positive x-axis?
Then follow the advice given to find the radius of a circle, and loop through 2pi radians (or 360 degrees) in 5 steps calculating verticies.
But I would say, consider your approach: it’s easier to draw starting with the centre and the radius, and it may be easier to find the radius of the circle without bothering with the side-length, it depends how you work that out. (eg. if you just make it up so it looks right, or so the pentagon fits next to something)
Very good point. For simplicity sake, it is most convenient to have the “top” vertex coincide with the vertical axis (y-axis) assuming we’re working on the x-y plane.
Now, if you want to get fancy, you can throw in a condition that the pentagon’s “top” vertex is rotated 30 degrees off the vertical axis…if you want to add some spice to the problem!
Ultimately, you could make your program draw any regular polygon tilted to any angle off* (*off what most of us would describe as being) the vertical.
Have fun! Hope we’ve all given you some food for thought…
To reinforce what Shade posted. It’s a piece of cake to do find the coordinates of the vertices in polar coordinates and then convert those to Cartesian.
I’m also not sure as to what is meant by the length of a pentagon, but use that to find the distance from any vertex to the origin. If the “length” is the length of a side the distance from the origin to a vertex is:
R = L * sin (3pi/10) / sin(2pi/5)
Now our polar coords for our vertices (starting with one on the positive x axis and going counterclockwise) are:
If the pentagon is oriented differently just add the theta of the vertex you want to start at to the theta coordinate of every vertex. (add pi/2 (or even pi/10 if you want to be extra clever) to each theta to orient the pentagon so there is a vertex on the positive y. Subtract those same values for a point down satanic style math/programming problem just in time for Halloween.)
Once you have your polar coords, it’s easy to switch to Cartesian.
Using Shade’s approach, the vertices would have coordinates of
x = r * cos(2pi * (i/5) + offset), y = r * sin(2pi * (i/5) + offset)
where r = the radius (i.e. the distance from the center to any vertex), i runs from 0 through 5 (with i=5 putting you right back where you started), and offset = the angle you want the pentagon “tilted” at (0 if you want the first vertex on the positive x-axis, pi/2 if you want it on the positive y-axis). For a regular n-gon, you can replace 5 with n in the above formula.
This is all assuming I haven’t made any stupid mistakes, of course.
(On preview, I see that Lance Turbo has said just about the same thing, but I’m posting mine anyway!)
For those who are familiar with a little complex algebra, here’s an algorithm to find the vertices of a regular n-gon with radius r and center (a, b). Let z denote a complex number.
for 0 < k < n - 1
z[sub]k[/sub] = r * exp(2k[symbol]pi[/symbol]/n) + a + b[symbol]i[/symbol]
Pretty simple, right? Keep in mind that exp([symbol]iq[/symbol]) = cos([symbol]q[/symbol]) + [symbol]i[/symbol]sin([symbol]q[/symbol]), and you can derive all the formulas that you need for this.
Since nobody else has challenged this, I will. Draw all the diagonals of a pentagon, and you’ll end up with five different intersection points, forming a smaller pentagon.
You really want to have the radius (distance from center to each vertex), not the edge length. If you absolutely have to start with the edge lengths, then draw the pentagon, then draw a couple of the radii, to form a triangular slice. Then, draw a line perpendicular to the side of the pentagon, going through the center (so you divide your triangular slice into two right triangles). Now, you can use your trig functions to get the length of the radius (the hypoteneuse of the triangle) from the length of an edge of the pentagon.
You don’t need to draw that perpendicular. Knowing the length of the edge and how many sides allows you to determine all three angles of the radius-radius-edge isoceles triangle. With all the angles and one of the lengths all you need is the law of sines.