旋转,缩放和平移2D坐标?

时间:2014-05-07 23:55:24

标签: python rotation translation scale python-imaging-library

我正在尝试使用Python Imaging Library创建Hilbert曲线的项目。我创建了一个函数,它将通过每次迭代生成曲线的新坐标,并将它们放入各种列表中,然后我希望能够移动,旋转和缩放。我想知道是否有人可以给我一些提示或方法来做到这一点,因为我完全无能为力。仍在研究很多代码。

#! 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 个答案:

答案 0 :(得分:4)

这是一个处理矩阵的解决方案(这对于这种类型的计算很有意义,最后,2D坐标是一列的矩阵!),

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

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

移动非常简单,您只需要为矩阵的每个元素添加偏移量,这里是使用矩阵乘法的方法:

# 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 = zero(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

然后创建您的翻译矩阵

TranMatrix = zero(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)

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

RotMatrix = zero(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