2014-05-07 314 views
1

我正在使用Python圖像庫創建希爾伯特曲線,目前我正在開發一個項目。我創建了一個函數,它會通過每次迭代爲曲線生成新的座標,並將它們放入各種列表中,然後我希望能夠移動,旋轉和縮放。我想知道是否有人可以給我一些提示或方法來做到這一點,因爲我完全無能爲力。仍在研究大量的代碼。旋轉,縮放和平移2D座標?

#! 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() 
+0

查看標題爲[_Python函數旋轉2D對象_](http://stackoverflow.com/q/20023209/355230)的問題的答案以及我對問題的回答[_Rotate圍繞給定兩個頂點的中心點_](http ://stackoverflow.com/a/14842362/355230)。 – martineau

回答

2

這裏是矩陣工作的解決方案(這是有道理的,這種類型的計算,並在年底,二維座標矩陣與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 

搬家是很容易,你需要做的是將偏移添加到矩陣的每個元素,使用矩陣乘法這裏有一個方法:

# 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

+0

謝謝得到它的工作:) – user3602179