在Python 3.2中绘制具有n个边数的多边形 [英] Drawing polygon with n number of sides in Python 3.2

查看:736
本文介绍了在Python 3.2中绘制具有n个边数的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用Python编写一个程序,该程序读取值n并在屏幕上绘制n边的多边形.我可以使用turtle图形模块或graphics.py模块.

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

当n =输入的点数,然后在屏幕上单击n次时,我知道如何绘制多边形,但是我很难理解如何将多个边转换为多边形.

I know how to draw a polygon when n = the number of points you input and then click n times on the screen, but I'm having some trouble getting an idea on how to transform a number of sides into a polygon.

这是点数为n的多边形的代码:

Here's the code I have for the 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)

这不是程序的全部代码(384行).这只是程序中绘制多边形函数所在的部分,其中self.figs = [](绘制图形的列表).

This isn't the whole code to the program(which is 384 lines). This is just the part of the program where the draw polygon function is where self.figs = [ ] , a list of drawn figures.

推荐答案

我假设您想要的是一种生成等边多边形坐标的方法,您可以将其输入到绘图程序中.我不确定您使用的是哪个库,因此我将坚持使用值对列表:

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

里面有很多事情,所以我会讲一下.基本方法是扫出一个圆,并在其上放置n等距的点.这些将是我们多边形的点,从12点钟位置开始.

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.

首先要做的是从中心向外算出每个楔形的角度(以弧度为单位).一个圆的弧度总数为2 pi,因此我们的值为每段2 pi/n.

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.

此后,我们进行了一些基本的操作( https://en.wikipedia. org/wiki/Trigonometry#Extending_the_definitions ).此时,我们将按所需的半径进行缩放,并有机会也将旋转量偏移固定值.

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.

一些示例

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.

这篇关于在Python 3.2中绘制具有n个边数的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆