Python 3个数组,反转多边形点
Python 3 array, inverting polygon points
我有一个坐标列表,但 x、y 坐标不在元组中:
width,height = 100
#my_list = (x1, y1, x2, y2, ...)
#my_list = (0, 50, 50, 0, 50, 100)
my_list = (0, height/2, width/2, 0, width/2, height)
这些点可以创建一个多边形。在我的例子中,一个三角形填充了指向左侧的 100x100 区域的一半。
我正在尝试反转(水平)三角形,使其指向右侧,因此解决方案是:
my_list = (width, height/2, width/2, 0, width/2, height)
但我在想我应该如何制作一个能够水平反转任何多边形的函数?所以像这样:
my_list = (0, height/2, width/2, 0, width/2, height)
invert_horizontally(my_list, width, height)
>>> (100, 50, 50, 0, 50, 100)
我认为,为了反转点,我必须做这样的事情:
for point in my_list:
if this point is x:
new_x = width - point
else:
pass
我的问题是:如何确定点是 X。还有如何用这段代码创建单行代码?
编辑:
到目前为止我的代码:
new_list = [ @@@-point for point in my_list ]
现在 @@@
必须根据点是 x 还是 y 进行更改。
编辑#2:
基本上我需要对每个偶数项'point'做宽度点
您知道点按顺序存储为列表中的 x, y
值。因此,您知道所有 x
都处于偶数位置。一个班轮可能是:
inverted_triangle = [width-coord if i % 2 == 0 else coord for i, coord in enumerate(triangle)]
您可以从 my_list
、zip
中创建一个迭代器与自身配对 x 和 y 坐标,并使用 x 和 y 的各自计算来获得倒置坐标:
def invert_horizontally(my_list, width, height):
i = iter(my_list)
return [coord for x, y in zip(i, i) for coord in (width - x, y)]
所以根据您的示例输入,
invert_horizontally(my_list, width, height)
returns:
[100, 50, 50, 0, 50, 100]
我有一个坐标列表,但 x、y 坐标不在元组中:
width,height = 100
#my_list = (x1, y1, x2, y2, ...)
#my_list = (0, 50, 50, 0, 50, 100)
my_list = (0, height/2, width/2, 0, width/2, height)
这些点可以创建一个多边形。在我的例子中,一个三角形填充了指向左侧的 100x100 区域的一半。
我正在尝试反转(水平)三角形,使其指向右侧,因此解决方案是:
my_list = (width, height/2, width/2, 0, width/2, height)
但我在想我应该如何制作一个能够水平反转任何多边形的函数?所以像这样:
my_list = (0, height/2, width/2, 0, width/2, height)
invert_horizontally(my_list, width, height)
>>> (100, 50, 50, 0, 50, 100)
我认为,为了反转点,我必须做这样的事情:
for point in my_list:
if this point is x:
new_x = width - point
else:
pass
我的问题是:如何确定点是 X。还有如何用这段代码创建单行代码?
编辑:
到目前为止我的代码:
new_list = [ @@@-point for point in my_list ]
现在 @@@
必须根据点是 x 还是 y 进行更改。
编辑#2:
基本上我需要对每个偶数项'point'做宽度点
您知道点按顺序存储为列表中的 x, y
值。因此,您知道所有 x
都处于偶数位置。一个班轮可能是:
inverted_triangle = [width-coord if i % 2 == 0 else coord for i, coord in enumerate(triangle)]
您可以从 my_list
、zip
中创建一个迭代器与自身配对 x 和 y 坐标,并使用 x 和 y 的各自计算来获得倒置坐标:
def invert_horizontally(my_list, width, height):
i = iter(my_list)
return [coord for x, y in zip(i, i) for coord in (width - x, y)]
所以根据您的示例输入,
invert_horizontally(my_list, width, height)
returns:
[100, 50, 50, 0, 50, 100]