gpt4 book ai didi

python - 旋转、缩放和平移二维坐标?

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:40 26 4
gpt4 key购买 nike

目前我正在做一个项目,尝试使用 Python 成像库创建希尔伯特曲线。我创建了一个函数,它将通过每次迭代为曲线生成新坐标,并将它们放入各种列表中,然后我希望能够移动、旋转和缩放这些列表。我想知道是否有人可以给我一些提示或方法,因为我完全一无所知。仍在编写大量代码。

#! usr/bin/python
import Image, ImageDraw
import math

# Set the starting shape
img = Image.new('RGB', (1000, 1000))
draw = ImageDraw.Draw(img)

curve_X = [0, 0, 1, 1]
curve_Y = [0, 1, 1, 0]

combinedCurve = zip(curve_X, curve_Y)
draw.line((combinedCurve), fill=(220, 255, 250))
iterations = 5

# Start the loop
for i in range(0, iterations):
# Make 4 copies of the curve

copy1_X = list(curve_X)
copy1_Y = list(curve_Y)

copy2_X = list(curve_X)
copy2_Y = list(curve_Y)

copy3_X = list(curve_X)
copy3_Y = list(curve_Y)

copy4_X = list(curve_X)
copy4_Y = list(curve_Y)

# For copy 1, rotate it by 90 degree clockwise
# Then move it to the bottom left
# For copy 2, move it to the top left
# For copy 3, move it to the top right
# For copy 4, rotate it by 90 degrees anticlockwise
# Then move it to the bottom right

# Finally, combine all the copies into a big list
combinedCurve_X = copy1_X + copy2_X + copy3_X + copy4_X
combinedCurve_Y = copy1_Y + copy2_Y + copy3_Y + copy4_Y

# Make the initial curve equal to the combined one
curve_X = combinedCurve_X[:]
curve_Y = combinedCurve_Y[:]

# Repeat the loop

# Scale it to fit the canvas
curve_X = [x * xSize for x in curve_X]
curve_Y = [y * ySize for y in curve_Y]
# Draw it with something that connects the dots
curveCoordinates = zip(curve_X, curve_Y)
draw.line((curveCoordinates), fill=(255, 255, 255))

img2=img.rotate(180)
img2.show()

最佳答案

这是一个基于矩阵的解决方案(这对于此类计算很有意义,最终,二维坐标是具有 1 列的矩阵!),

缩放非常简单,只需将矩阵的每个元素乘以缩放因子即可:

scaled = copy.deepcopy(original)
for i in range(len(scaled[0])):
scaled[0][i]=scaled[0][i]*scaleFactor
scaled[1][i]=scaled[1][i]*scaleFactor

移动很容易,你所要做的就是为矩阵的每个元素添加偏移量,这里有一个使用矩阵乘法的方法:

import numpy as np
# Matrix multiplication
def mult(matrix1,matrix2):
# Matrix multiplication
if len(matrix1[0]) != len(matrix2):
# Check matrix dimensions
print 'Matrices must be m*n and n*p to multiply!'
else:
# Multiply if correct dimensions
new_matrix = np.zeros(len(matrix1),len(matrix2[0]))
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
new_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
return new_matrix

然后创建你的翻译矩阵

import numpy as np
TranMatrix = np.zeros((3,3))
TranMatrix[0][0]=1
TranMatrix[0][2]=Tx
TranMatrix[1][1]=1
TranMatrix[1][2]=Ty
TranMatrix[2][2]=1

translated=mult(TranMatrix, original)

最后,旋转有点棘手(你知道你的旋转角度吗?):

import numpy as np
RotMatrix = np.zeros((3,3))
RotMatrix[0][0]=cos(Theta)
RotMatrix[0][1]=-1*sin(Theta)
RotMatrix[1][0]=sin(Theta)
RotMatrix[1][1]=cos(Theta)
RotMatrix[2][2]=1

rotated=mult(RotMatrix, original)

进一步阅读我所做的事情:

所以基本上,如果您在代码中插入这些操作,将向量乘以旋转/平移矩阵,它应该可以工作

编辑

我刚刚发现这个 Python 库似乎提供了所有类型的转换:http://toblerity.org/shapely/index.html

关于python - 旋转、缩放和平移二维坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23530449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com