已知p1 和 p2 ,是L上的两点,点p3为直线外一点,求p3在直线L上的投影点p
getProjectionPointOnLine(p1, p2, p3) {
p1 = {
x: p1[0],
y: p1[1]
}
p2 = {
x: p2[0],
y: p2[1]
}
p3 = {
x: p3[0],
y: p3[1]
}
//垂直x轴的情况
if (p1.x - p2.x === 0) {
return [p1.x, p3.y]
}
const A = (p1.y - p2.y) / (p1.x - p2.x)
const B = (p1.y - A * p1.x)
const m = p3.x + A * p3.y
const px = (m - A * B) / (A * A + 1)
const py = A * px + B
return [px, py]
}