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



所以我有一个解决方案,可以根据地标角度(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
}

最新更新