2016-10-16 36 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的开始/结束坐标? –