gpt4 book ai didi

c# - 找到两条线之间的交点

转载 作者:太空狗 更新时间:2023-10-29 23:39:50 25 4
gpt4 key购买 nike

所以我一直在研究这个相对简单的算法。我不确定我的代码有什么问题,但我没有得到它们实际相交的交点。

我正在使用 Unity3D,我试图在 x、z 平面中找到两条线相交的点,但不在 x、y 平面中。我假设适用于 x,y 的算法也适用于 x,z;

我的代码:

Vector3 thisPoint1 = thisCar.position + (2 * thisCar.forward);
Vector3 thisPoint2 = thisCar.position + (20 * thisCar.forward);

Debug.DrawLine(thisPoint1, thisPoint2, Color.white, 2);

Vector3 otherPoint1 = threateningCar.position + (2 * threateningCar.forward);
Vector3 otherPoint2 = threateningCar.position + (20 * threateningCar.forward);

Debug.DrawLine(otherPoint1, otherPoint2, Color.white, 2);

float A1 = thisPoint2.z - thisPoint1.z;
float B1 = thisPoint1.x - thisPoint2.x;
float C1 = A1 * thisPoint1.x + B1 * thisPoint1.z;

float A2 = otherPoint2.z - otherPoint1.z;
float B2 = otherPoint1.x - otherPoint2.x;
float C2 = A2 * otherPoint1.z + B2 * otherPoint1.z;

float det = A1 * B2 - A2 * B1;

float x = (B2 * C1 - B1 * C2) / det;
float z = (A1 * C2 - A2 * C1) / det;

return new Vector3(x, this.transform.position.y, z);

任何人都可以帮助指出我做错了什么吗?

thisCar.forwardthreatingCar.forward 通常是 [0,0,1], [0,0,-1][1,0,0], [-1,0,0]

最佳答案

找到了!!!

float A2 = otherPoint2.z - otherPoint1.z;
float B2 = otherPoint1.x - otherPoint2.x;
float C2 = A2 * otherPoint1.z + B2 * otherPoint1.z;

应该是:

float A2 = otherPoint2.z - otherPoint1.z;
float B2 = otherPoint1.x - otherPoint2.x;

float C2 = A2 * otherPoint1.x + B2 * otherPoint1.z;

白白浪费了很多时间 :/.

无论如何,这将帮助任何想要做线相交的人。

关于c# - 找到两条线之间的交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15648607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com