Drawing Polygon With N Number Of Sides In Python 3.2
Solution 1:
I'm assuming what you would like is a way of generating equal sided polygon coordinates which you can feed to your drawing program. I'm not sure which library you are using, so I'm going to stick to lists of pairs of values:
import math
def polygon(sides, radius=1, rotation=0, translation=None):
one_segment = math.pi * 2 / sides
points = [
(math.sin(one_segment * i + rotation) * radius,
math.cos(one_segment * i + rotation) * radius)
for i in range(sides)]
if translation:
points = [[sum(pair) for pair in zip(point, translation)]
for point in points]
return points
There's a fair bit going on in there, so I'll talk through it. The basic approach is to sweep out a circle, and put n
equally spaced points on it. These will be the points of our polygon, starting at the 12 'o' clock position.
The first thing to do is work out the angle (in radians) of each wedge from the center outwards. The total number of radians in a circle is 2 pi, so our value is 2 pi / n
per segment.
After that a bit of basic trig gives us our points (https://en.wikipedia.org/wiki/Trigonometry#Extending_the_definitions). At this point we scale by our desired radius, and have the opportunity to offset the rotation by a fixed amount too.
After that we translate the values by a certain amount, because you probably want your polygon in the center of the screen, not in the corner.
A few examples
print polygon(5) # A unit pentagon
# [(0.0, 1.0), (0.9510565162951535, 0.30901699437494745), (0.5877852522924732, -0.8090169943749473), (-0.587785252292473, -0.8090169943749476), (-0.9510565162951536, 0.30901699437494723)]
print polygon(4, 100) # A square, point up, 100 from the center to the points
# [(0.0, 100.0), (100.0, 6.123233995736766e-15), (1.2246467991473532e-14, -100.0), (-100.0, -1.8369701987210297e-14)]
print polygon(4, 2, math.pi / 4, [10, 10]) # A flat square centered on 10, 10
# [[11.414213562373096, 11.414213562373096], [11.414213562373096, 8.585786437626904], [8.585786437626904, 8.585786437626904], [8.585786437626904, 11.414213562373094]]
As you can see, these are all floats, so you may need to squish these to integers before you can use them.
Solution 2:
I don't know if this will help but to define a polygon using number of sides and length then I would use my code:
import turtle as t
def polygon(n,l):
f = (n - 2) * 180/n
for i in range(n):
t.forward(l)
t.right(180 - f)
polygon()
In this case, n
would be number of sides and l would be length of sides.
This took me quite a while as I am only 13 and am not advanced but this was a fun project!
Post a Comment for "Drawing Polygon With N Number Of Sides In Python 3.2"