關鍵的公式是(蟒蛇):
# (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]
在這裏你可以看到結果
左上角的紅色角落是將外面被鏡像像素像素的例子原始圖像,並且它們不受以上代碼的影響。
有趣,還沒有碰到過這些。我可以問一下0.5是什麼嗎? –
@SamSarzentich:屏幕左上角的像素是一個從(0,0)到(1,1)的框,其中心的座標是(0.5,0.5)。對於這些公式中的計算,我只在像素中心採用了每個像素一個採樣點(無抗鋸齒)。 – 6502
好吧,我唯一的其他問題,如果我要採取用戶輸入將我使用x0,x1,y0,y1的開始/結束座標? –