Skip to main content
 首页 » 编程设计

python之通过 Jython 设置对角线镜像(用户设置点)

2025年04月02日7unruledboy

试图让我的头脑围绕这个我们需要创建的程序 需要的是根据注释: 创建一个名为的函数 arbitraryMirror() 允许用户以任意角度放置镜子,导致相交并因此镜像图像。

这需要在方形或矩形图片上完成。

根据下面的图片,这是所需的输出。

Output

我知道如何用正方形图像镜像图片(如下所示),但我不知道这是否也可以用矩形图像来完成?

Cross

我看过一种使用 y=mx+b 的方法,但它似乎过于复杂? 也许我需要一些坐标几何?还是代数? 任何帮助将不胜感激!

请您参考如下方法:

关键公式是(python):

# (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] 

在这里你可以看到一个结果的例子

左上角的红色角是原始图像之外的镜像像素,上面的代码未触及它们。