关键的公式是(蟒蛇):
# (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的开始/结束坐标? –