我一直在通过 Wikipedia 上的页面阅读三边测量,并尝试制作自己的 2D 版本。但是,我遇到了一些问题,我希望能更好地掌握数学的人可以帮助我。
我正在使用 C# 在 Unity 中执行此操作。
我的场景由屏幕中心的 3 个球体组成。球体 1 始终位于标记 0,0,0。在这些球体旁边,我有一个立方体,我想将其移动到 3 个球体的交点。
到目前为止,这是我的代码:
public GameObject sphere1, sphere2, sphere3;
float x, y;
float P1, P2, P3;
public GameObject cube;
// Update is called once per frame
void Update ()
{
P1 = ( sphere1.transform.position.x * sphere1.transform.position.x) + ( sphere1.transform.position.y * sphere1.transform.position.y);
P2 = ((sphere1.transform.position.x - sphere2.transform.position.x) *(sphere1.transform.position.x - sphere2.transform.position.x)) + sphere1.transform.position.y;
P3 = ((sphere1.transform.position.x - sphere3.transform.position.x) *(sphere1.transform.position.x - sphere3.transform.position.x)) +
((sphere1.transform.position.y - sphere3.transform.position.y) *(sphere1.transform.position.y - sphere3.transform.position.y));
x = (P1 * P1) - (P2 * P2);
y = ((P1 * P1) - (P3 * P3)) + (sphere3.transform.position.x * sphere3.transform.position.x) + ((sphere3.transform.position.y * sphere3.transform.position.y) / 2 * sphere3.transform.position.y);
cube.transform.position = new Vector3(x, y, 0);
}
P1、P2 和 P3 是我对 wiki 页面中以下数学公式的解释:
我的 X 和 Y 公式如下:
当我连接所有这些时,我得到以下调试日志:
P1: 0
P2: 0.0989904
P3: 0.4478847
X is: -0.0097991Y is: 0.247284 // X and Y of cube
但是,当我运行我的代码时,立方体并没有进入交点,它只是移动到球体 1 的中心。当我移动球体 1 和球体 2 时,我的立方体确实移动了,但通常在完全不同的轴上. 当我移动球体 3 时,它似乎对程序完全没有影响。
我是否正确设置了三角测量?我错过了什么吗?