以v1为起始边,按照逆时针到v2,求这个θ的角度和弧度
// 只是按照余弦定理只能得出(0~π)
function get2VectorRadian(v1, v2) {
const {x: x1, y: y1} = v1
const {x: x2, y: y2} = v2
// 与v1起始向量方向相反
const _v1 = {x: -x1, y: -y1}
// 余弦定理
// C(0,0)
// /θ\
// b/ \a
// / \
// (x1,y1)A_______B(x2,y2)
// c
// c^2 = a^2 + b^2 - 2*a*b*cosθ
let cosRadian =
(Math.pow(x1, 2) +
Math.pow(y1, 2) +
Math.pow(x2, 2) +
Math.pow(y2, 2) -
(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))) /
(2 *
Math.sqrt(Math.pow(x1, 2) + Math.pow(y1, 2)) *
Math.sqrt(Math.pow(x2, 2) + Math.pow(y2, 2)))
return Math.acos(cosRadian)
}
分别求两个点在坐标系中的角度,接下来可能会好算很多,当然还要考虑在不同象限的不同情况