/**
* 计算垂足
* @param {point} A
* @param {point} B
* @param {point} C
* @returns
*/
projectFootPoint(A, B, C) {
//中点
const centerPoint = {
x: (A.x + B.x) / 2,
y: (A.y + B.y) / 2
}
//向量
const AB = {
x: B.x - A.x,
y: B.y - A.y
}
const AC = {
x: C.x - A.x,
y: C.y - A.y
}
//计算向量AB模长
const lengthAB = Math.sqrt(AB.x * AB.x + AB.y * AB.y)
//半长
const halfLength = lengthAB / 2
//计算AB单位向量
const _AB = {
x: AB.x / lengthAB,
y: AB.y / lengthAB
}
//计算AC在AB的投影长度
const projection_length = AC.x * _AB.x + AC.y * _AB.y;
//计算投影坐标
const point = {
x: _AB.x * projection_length + A.x,
y: _AB.y * projection_length + A.y
}
//计算中点到垂点距离
const centerLength = Math.sqrt(Math.pow(point.x - centerPoint.x, 2) + Math.pow(point.y - centerPoint.y, 2))
//是否在线段上 (近似判断)
const flag = Math.round(centerLength * 1000) / 1000 <= Math.round(halfLength * 1000) / 1000
//计算垂足到线段距离
if(!flag){
console.log(centerLength - halfLength);
}
//计算垂距
const length = Math.sqrt(Math.pow(point.x - C.x, 2) + Math.pow(point.y - C.y, 2))
return {
flag, // 是否在线段
point, // 垂足
length // 垂距
}
},
04-25
2157

08-03
648
