gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:21:45 25 4
gpt4 key购买 nike

试图让我的头脑围绕这个我们需要创建的程序需要的是根据注释:创建一个名为的函数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]

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

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

关于python - 通过 Jython 设置对角线镜像(用户设置点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40069185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com