如何旋转多边形?
How to rotate a polygon?
为什么:
目前 trying/doing 一些深度学习的东西,对 python 来说是全新的。
我们已经为我们的东西准备了一些代码 运行。我们现在只想计算我们找到的 "things"。因为我们没有找到任何关于多只猫和多只狗的分类数据。我想用六边形创建一些随机生成的图像。
我怎么想让它们中的一些旋转。但是我不知道怎么办。
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()
以下是如何将 my answer 中的技术和数学应用到一个类似的问题,可以完成您 "wanna" 的问题(如果您想围绕它们的中心点旋转它们)。
我用 graphics
模块的 5.0 版测试了它,我从以下网址下载:
http://mcsp.wartburg.edu/zelle/python/graphics.py
from graphics import *
from random import randint
from math import sin, cos, radians
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(8):
figure = drawahexagon(80)
figure = rotatePolygon(figure, randint(0, 90))
figure.draw(win)
try:
win.getMouse() # causes graphics.GraphicsError: getMouse in closed window
except GraphicsError: # ignore error
pass
win.close()
def rotatePolygon(polygon, degrees):
""" Rotate polygon the given angle about its center. """
theta = radians(degrees) # Convert angle to radians
cosang, sinang = cos(theta), sin(theta)
points = polygon.getPoints()
# find center point of Polygon to use as pivot
n = len(points)
cx = sum(p.getX() for p in points) / n
cy = sum(p.getY() for p in points) / n
new_points = []
for p in points:
x, y = p.getX(), p.getY()
tx, ty = x-cx, y-cy
new_x = ( tx*cosang + ty*sinang) + cx
new_y = (-tx*sinang + ty*cosang) + cy
new_points.append(Point(new_x, new_y))
rotated_ploygon = polygon.clone() # clone to get current attributes
rotated_ploygon.points = new_points
return rotated_ploygon
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()
正如我在评论中提到的那样,以这种方式创建旋转的多边形——首先创建一个未旋转的多边形,克隆它,然后旋转副本——效率有点低,因为它可以通过先创建旋转的点然后再创建旋转的点来完成创建 Polygon
.
这是一个执行此操作的实现:
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
为什么: 目前 trying/doing 一些深度学习的东西,对 python 来说是全新的。 我们已经为我们的东西准备了一些代码 运行。我们现在只想计算我们找到的 "things"。因为我们没有找到任何关于多只猫和多只狗的分类数据。我想用六边形创建一些随机生成的图像。
我怎么想让它们中的一些旋转。但是我不知道怎么办。
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()
以下是如何将 my answer 中的技术和数学应用到一个类似的问题,可以完成您 "wanna" 的问题(如果您想围绕它们的中心点旋转它们)。
我用 graphics
模块的 5.0 版测试了它,我从以下网址下载:
http://mcsp.wartburg.edu/zelle/python/graphics.py
from graphics import *
from random import randint
from math import sin, cos, radians
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(8):
figure = drawahexagon(80)
figure = rotatePolygon(figure, randint(0, 90))
figure.draw(win)
try:
win.getMouse() # causes graphics.GraphicsError: getMouse in closed window
except GraphicsError: # ignore error
pass
win.close()
def rotatePolygon(polygon, degrees):
""" Rotate polygon the given angle about its center. """
theta = radians(degrees) # Convert angle to radians
cosang, sinang = cos(theta), sin(theta)
points = polygon.getPoints()
# find center point of Polygon to use as pivot
n = len(points)
cx = sum(p.getX() for p in points) / n
cy = sum(p.getY() for p in points) / n
new_points = []
for p in points:
x, y = p.getX(), p.getY()
tx, ty = x-cx, y-cy
new_x = ( tx*cosang + ty*sinang) + cx
new_y = (-tx*sinang + ty*cosang) + cy
new_points.append(Point(new_x, new_y))
rotated_ploygon = polygon.clone() # clone to get current attributes
rotated_ploygon.points = new_points
return rotated_ploygon
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()
正如我在评论中提到的那样,以这种方式创建旋转的多边形——首先创建一个未旋转的多边形,克隆它,然后旋转副本——效率有点低,因为它可以通过先创建旋转的点然后再创建旋转的点来完成创建 Polygon
.
这是一个执行此操作的实现:
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