所需点是顶点(例如顶点 C)到包含对边(例如 AB)的直线上的正交投影。
要找到投影点,请获取 AB 和 AC 的向量
AB = (B - A) //in coordinates ab.x = b.x-a.x, ab.y = b.y-a.y
AC = (C - A)
Run Code Online (Sandbox Code Playgroud)
并使用 AB 和 AC 的标量积查找参数
t =(AB * AC) / (AB * AB)
t =((b.x-a.x)*(c.x-a.x) + (b.y-a.y)*(c.y-a.y)) / ((b.x-a.x)*(b.x-a.x) + (b.y-a.y)*(b.y-a.y))
Run Code Online (Sandbox Code Playgroud)
投影点坐标
P = A + AB * t
p.x = a.x + (b.x-a.x) * t
p.y = a.y + (b.y-a.y) * t
Run Code Online (Sandbox Code Playgroud)
就这样
def orthoProjection(ax, ay, bx, by, cx, cy):
abx = bx - ax
aby = by - ay
acx = cx - ax
acy = cy - ay
t = (abx * acx + aby * acy) / (abx * abx + aby * aby)
px = ax + t * abx
py = ay + t * aby
return px, py
print(orthoProjection(0, 0, 4, 4, -1, 5))
>>(2.0, 2.0)
Run Code Online (Sandbox Code Playgroud)