gpt4 book ai didi

java - 线相交方法无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 15:14:23 25 4
gpt4 key购买 nike

我正在研究一些 vector 数学,我需要计算多边形的法 vector 。我的代码:

    //p is a parameter, is a Vec2, second point on first line
double[][] vert = getVerticies(); //[any length, # of verticies][2]
for(int i = 0; i < vert.length; i++) {
Vec2 cm = Vec2.ZERO_VEC;//first point on first line, always is <0, 0> as it is the origin
Vec2 rcm = getCM(); // just used to get relative positions.
Vec2 v1 = cm.sub(new Vec2(vert[i])); //the first point in one of all edges of the shape, second line
Vec2 v2 = cm.sub(new Vec2(i == vert.length - 1 ? vert[0] : vert[i + 1])); // the second point on the second line.
double den = (v2.getY() - v1.getY()) * (p.getX() - cm.getX()) - (v2.getX() - v1.getX()) * (p.getY() - cm.getY());
if(den == 0D) {
continue;
}
double a = ((v2.getX() - v1.getX()) * (cm.getY() - v1.getY()) - (v2.getY() - v1.getY()) * (cm.getX() - v1.getX())) / den;
double b = ((p.getX() - cm.getX()) * (cm.getY() - v1.getY()) - (p.getY() - cm.getY()) * (cm.getX() - v1.getX())) / den;
if(a >= 0D && a <= 1D && b >= 0D && b <= 1D) {
Vec2 mid = v2.add(v2.sub(v1).scale(0.5D)); //this is just normal vector calculation stuff, I know the error isn't here, as if it was, it would return a non-unit-scale vector.
return mid.uscale(); //hats the vector, returns
}
}
return p; // return the parameter, second point on first line, used as a contingency, should never actually run, as the first line is fully contained in the lines were testing against

我已经进行了一些调试,但我只是没有看到发生了什么。谁能告诉我我的数学有什么问题吗?它似乎运行得很好,但数学似乎不太正确。我使用此代码的目标是确定我的线相交的两个顶点的索引。

最佳答案

哎呀,通过尝试解释它弄清楚了。我需要进行光线追踪,或者使用光线相交测试。

关于java - 线相交方法无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21012776/

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