且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

你如何找到与直线垂直距离的点?

更新时间:2022-01-17 00:05:19

您需要计算垂直于线段的单位向量.避免计算斜率,因为这会导致除以零误差.

You need to compute a unit vector that's perpendicular to the line segment. Avoid computing the slope because that can lead to divide by zero errors.

dx = x1-x2
dy = y1-y2
dist = sqrt(dx*dx + dy*dy)
dx /= dist
dy /= dist
x3 = x1 + (N/2)*dy
y3 = y1 - (N/2)*dx
x4 = x1 - (N/2)*dy
y4 = y1 + (N/2)*dx