Drawing a polygon with n number of sides in Python 3.2

I need to write a Python program that reads the value of n and draws a polygon from n sides on the screen. I can use either the turtle graphics module or the graphics.py module.

I know how to draw a polygon when n = the number of points you entered and then press n times on the screen, but I had trouble understanding how to convert multiple sides to a polygon.

Here's the code i for a polygon with n number of points:

def newPolygon(self,cmd): p = eval(input("how many points")) print("click",p,"times") num = [] for i in range(p): vertices = self.win.getMouse() num.append(vertices) poly = Polygon(num) poly.setFill(self.color) poly.draw(self.win) self.figs.append(poly) 

This is not all program code (these are 384 lines). This is only part of the program where the draw polygon function is where self.figs = [], a list of pictures.

+6
source share
2 answers

I assume that you want to create the same one-sided polygon coordinates that you can feed into your drawing program. I'm not sure which library you are using, so I will 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 is an honest cue ball, so I’ll talk about it. The basic approach is to sweep the circle and draw n equally spaced points on it. These will be the points of our polygon, starting from the 12-hour position.

The first thing to do is work out the angle (in radians) of each wedge from the center out. The total number of radians in the circle is 2 pi, so our value is 2 pi / n per segment.

After that, a little basic trigger gives us our points ( https://en.wikipedia.org/wiki/Trigonometry#Extending_the_definitions ). At this moment, we scale to our desired radius and are also able to compensate for the rotation by a fixed amount.

After that, we translate the values ​​by a certain amount, because you probably want your polygon to be in the center of the screen, and 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 have to grind them to integers before you can use them.

+5
source

I don't know if this will help, but to define a polygon using the number of sides and length, 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 will be the number of sides, and l will be the length of the sides.

It took me quite a while because I am only 13, and I did not move forward, but it was a fun project!

0
source

All Articles