Python 如何旋转多边形?

Python 如何旋转多边形?,python,graphics,polygon,zelle-graphics,Python,Graphics,Polygon,Zelle Graphics,原因: 目前正在尝试/做一些深入学习的东西,对python来说是全新的。 我们已经为我们的东西运行了一些代码。我们现在想数一数我们找到的“东西”。因为我们没有找到任何关于多只猫和狗的机密数据。我想用六边形创建一些随机生成的图像 我多么想让他们中的一些人轮换。但是我不知道怎么做 from graphics import * from random import randint import math image_height = 1000 image_height = 1000; def mai

原因: 目前正在尝试/做一些深入学习的东西,对python来说是全新的。 我们已经为我们的东西运行了一些代码。我们现在想数一数我们找到的“东西”。因为我们没有找到任何关于多只猫和狗的机密数据。我想用六边形创建一些随机生成的图像

我多么想让他们中的一些人轮换。但是我不知道怎么做

from graphics import *
from random import randint
import math

image_height = 1000
image_height = 1000;
def main():
    win = GraphWin("Window",image_height,image_height)
    win.setBackground(color_rgb(255, 255, 255))

    for _ in range(0,8):
          figure = drawahexagon(80)
         #figure = rotatePolygon(figure,randint(0,90))
          figure.draw(win)
    win.getMouse()
    win.close


def drawahexagon(length):
    x = randint(0, image_height-length)
    y = randint(0, image_height-length)
    poly = Polygon(Point(x+getRandom(0),y+getRandom(0)),
                   Point(x+length+getRandom(1),y+getRandom(1)),
                   Point(x+(length*1.5)+getRandom(0),y+(length/2)+getRandom(1)),
                   Point(x+length+getRandom(1),y+length+getRandom(1)),
                   Point(x+getRandom(0),y+length+getRandom(1)),
                   Point(x-(length/2)+getRandom(1),y+(length/2)+getRandom(0)))
    poly.setFill(color_rgb(255,0,0))
    return poly


def getRandom(base):
  if base == 0:
    foo = randint(0,5)
  else:
    foo = randint(3,10)
  return foo



main()

下面是如何将技术和数学应用到类似的问题中,以实现您“想要”的目标(如果您想围绕中心点旋转它们)

我使用
graphics
模块的5.0版进行了测试,我从以下网站下载了该模块:


正如我在一篇评论中提到的那样,通过先创建一个未旋转的多边形,克隆该多边形,然后旋转副本来创建旋转的多边形,这种方式有点低效,因为可以通过先创建旋转点,然后创建
多边形来完成

下面是一个实现:

def drawarotatedhexagon(length, degrees):
    x = randint(0, image_height-length)
    y = randint(0, image_height-length)
    points = [Point(x+getRandom(0), y+getRandom(0)),
              Point(x+length+getRandom(1), y+getRandom(1)),
              Point(x+(length*1.5)+getRandom(0), y+(length/2)+getRandom(1)),
              Point(x+length+getRandom(1), y+length+getRandom(1)),
              Point(x+getRandom(0), y+length+getRandom(1)),
              Point(x-(length/2)+getRandom(1), y+(length/2)+getRandom(0))]

    theta = radians(degrees)  # Convert angle to radians
    cosang, sinang = cos(theta), sin(theta)

    n = len(points)
    cx = sum(pt.getX() for pt in points) / n
    cy = sum(pt.getY() for pt in points) / n
    for pt in points:
        tx, ty = pt.getX()-cx, pt.getY()-cy
        nx = ( tx*cosang + ty*sinang) + cx
        ny = (-tx*sinang + ty*cosang) + cy
        pt.x, pt.y = nx, ny

    poly = Polygon(*points)
    poly.setFill(color_rgb(255, 0, 0))
    return poly

graphics
不是标准的Python模块。这是什么?你从哪里弄来的?仅仅说你“想”旋转某个东西是不够的,除了旋转量,你还必须定义旋转发生的点。你可能会发现这很有用。非常感谢,这正是我“想”做的:)。非常爱,有非常棒的日子。听你这么说真好。FWIW,从一开始就生成旋转的多边形会更有效。这可以通过更改
drawahexagon()
来实现,因此它将
rotatePolygon()
中正在进行的变换应用于创建返回的
多边形对象之前或期间的所有坐标的x和y值。这主意不错,但我如何在不先绘制六边形的情况下获得中心“点”?知道我怎么从中心画一个六边形吗?我会试着自己找出答案,但首先我需要几个小时的睡眠:)你可以使用你现在用于初始选择坐标的代码将这些点临时存储在本地列表中。在此之后,可以计算“中心”点,因为它不会改变,因为它是所有其他点都将围绕的点。然后再次浏览列表并对其进行数学运算。完成后,您可以使用更新的值构建
多边形。度过一个宁静的夜晚
;-)。若要构造一个仅给定一个中心点(cx,cy)和长度的六边形,可以随机选取六个角度,每个角度都大于最后一个角度(即选取六个并对其进行排序)。然后可以使用
pt[i].x=(长度*cos(ang[i])+cx
pt[i].y=(长度*sin(ang[i])+cx
,从每个角度计算一个(x,y)点。要使其更不规则,可以在每个值上稍微改变
长度
值。
def drawarotatedhexagon(length, degrees):
    x = randint(0, image_height-length)
    y = randint(0, image_height-length)
    points = [Point(x+getRandom(0), y+getRandom(0)),
              Point(x+length+getRandom(1), y+getRandom(1)),
              Point(x+(length*1.5)+getRandom(0), y+(length/2)+getRandom(1)),
              Point(x+length+getRandom(1), y+length+getRandom(1)),
              Point(x+getRandom(0), y+length+getRandom(1)),
              Point(x-(length/2)+getRandom(1), y+(length/2)+getRandom(0))]

    theta = radians(degrees)  # Convert angle to radians
    cosang, sinang = cos(theta), sin(theta)

    n = len(points)
    cx = sum(pt.getX() for pt in points) / n
    cy = sum(pt.getY() for pt in points) / n
    for pt in points:
        tx, ty = pt.getX()-cx, pt.getY()-cy
        nx = ( tx*cosang + ty*sinang) + cx
        ny = (-tx*sinang + ty*cosang) + cy
        pt.x, pt.y = nx, ny

    poly = Polygon(*points)
    poly.setFill(color_rgb(255, 0, 0))
    return poly