2016-10-16 35 views
2

試圖讓我的我的頭這個程序,我們需要創建 需要什麼樣的解決辦法是按調: 創建一個名爲 arbitraryMirror(功能),允許用戶以任意角度放置鏡子,造成相交併因此鏡像圖像。通過Jython的設置對角鏡像(用戶設定點)

這將需要在正方形或矩形圖片上完成。

根據下面的圖片,這是所需要的輸出。

Output

我知道如何鏡像PIC採用方形的圖像(如下圖所示),但我不能工作了,如果這也可以用矩形圖像來完成?

Cross

我看了一下使用y的方法= mx + b中,但似乎過於複雜? 也許我需要一些座標幾何?或代數? 任何幫助將不勝感激!

回答

1

關鍵的公式是(蟒蛇):

# (x0, y0) and (x1, y1) are two points on the mirroring line 
# dx, dy, L is the vector and lenght 
dx, dy = x1 - x0, y1 - y0 
L = (dx**2 + dy**2) ** 0.5 

# Tangent (tx, ty) and normal (nx, ny) basis unit vectors 
tx, ty = dx/L, dy/L 
nx, ny = -dy/L, dx/L 

# For each pixel 
for y in range(h): 
    for x in range(w): 
     # Map to tangent/normal space 
     n = (x+0.5 - x0)*nx + (y+0.5 - y0)*ny 
     t = (x+0.5 - x0)*tx + (y+0.5 - y0)*ty 

     # If we're in the positive half-space 
     if n >= 0: 
      # Compute mirrored point in XY space 
      # (negate the normal component) 
      xx = int(x0 + t*tx - n*nx + 0.5) 
      yy = int(y0 + t*ty - n*ny + 0.5) 

      # If valid copy to destination 
      if 0 <= xx < w and 0 <= yy < h: 
       img[y][x] = img[yy][xx] 

在這裏你可以看到結果 example of mirroring

左上角的紅色角落是將外面被鏡像像素像素的例子原始圖像,並且它們不受以上代碼的影響。

+0

有趣,還沒有碰到過這些。我可以問一下0.5是什麼嗎? –

+0

@SamSarzentich:屏幕左上角的像素是一個從(0,0)到(1,1)的框,其中心的座標是(0.5,0.5)。對於這些公式中的計算,我只在像素中心採用了每個像素一個採樣點(無抗鋸齒)。 – 6502

+0

好吧,我唯一的其他問題,如果我要採取用戶輸入將我使用x0,x1,y0,y1的開始/結束座標? –