将线解方程转换为具有未知x和y的可重用javascript函数

将线解方程转换为具有未知x和y的可重用javascript函数,javascript,line,intersection,equation,angle,Javascript,Line,Intersection,Equation,Angle,因此,我有一个解决方案,根据地标角度(312.27)和(19.65)度,以及这些地标的栅格坐标(1,5)和(9,7),来求解某人的位置(交点)。所以我现在面临的问题是,如何将这些公式转换成可以动态插入角度和栅格坐标并返回x和y交点的位置 Equations of the Lines based on land marks: P1: y = cot(312.27)*x + 5 - cot(312.27)*1 ⇒ y = -0.91x + 5.91 P2: y = cot(19.65)*x + 7

因此,我有一个解决方案,根据地标角度(312.27)和(19.65)度,以及这些地标的栅格坐标(1,5)和(9,7),来求解某人的位置(交点)。所以我现在面临的问题是,如何将这些公式转换成可以动态插入角度和栅格坐标并返回x和y交点的位置

Equations of the Lines based on land marks:
P1: y = cot(312.27)*x + 5 - cot(312.27)*1 ⇒ y = -0.91x + 5.91
P2: y = cot(19.65)*x + 7 - cot(19.65) * 9 ⇒ y = 2.80x - 18.21

solve point of intersection:
P1 = P2
-0.91x + 5.91 = 2.80x - 18.21
5.91 + 18.21 = 2.80x + 0.91x
24.12 = 3.71x
6.5 = x
y = -0.91(6.5) + 5.91
y = 0
Your position is (6.5,0).
因此,我正在考虑创建一个函数,如:

function getLocation(angle1, angle2, coord1, coord2){}
但我只是在试图弄明白如何将这个解决方案转换成输出x和y的东西时遇到了麻烦。因为我必须绕过未知的x或y

任何想法都将不胜感激


注意:角度转换为弧度。

您需要根据角度和x,y坐标求解方程组:

// let phi1 be the first angle and phi2 be the second angle thus
// let P1 be the first point and P2 be the second point

y = x * cot(phi1) + P1.y - P1.x * cot(phi1)

Similarly

y = x * cot(phi2) + P2.y - P2.x * cot(phi2)

Equating both sides:

x * cot(phi1) + P1.y - P1.x * cot(phi1) = x * cot(phi2) + P2.y - P2.x * cot(phi2)

Solving for x

x * (cot(phi1) - cot(phi2)) = P2.y - P2.x * cot(phi2) - P1.y + P1.x * cot(phi1)

Thus:

x = (P2.y - P2.x * cot(phi2) - P1.y + P1.x * cot(phi1)) / (cot(phi1) - cot(phi2)) 

Once you get x you can plug x in any of the equations for y:

y = x * cot(phi1) + P1.y - P1.x * cot(phi1)
所以要得到x和y:

function getLocation(angle1, angle2, coord1, coord2) {

    let num = coord2.y - coord2.x * cot(angle2) - coord1.y + coord1.x * cot(angle1)
    let den = cot(angle1) - cot(angle2)
    let x = num / den
    let y = x * cot(angle1) + P1.y - P1.x * cot(angle1)

    // do something awesome with x and y
}

有了上面的内容,我如何交换坐标呢?当我插入angle1和angle2的值时,我得到了不同的x和y。getXandY(312.27*(Math.PI/180),19.65*(Math.PI/180))=[-24.11428433784922,27.828214405893622]我遗漏了什么吗?@TanCtIo对不起,我忘了用
cot(phi1)-cot(phi2)
除以x。请查看更新的答案,它现在应该可以工作了。交换坐标是什么意思?谢谢,我试试看。所以,如果我从不同的角度和坐标通过,这仍然有效吗?所以不同的方位和不同的参考点。目标是找到交叉点/位置@StaticBeagle,因此通过您的更改,我能够得到预期的答案。有没有办法插入新角度和栅格坐标?我想我没有看到这样做的方法,因为函数每次都会改变。