Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 直线相交方法无法正常工作_Java_Math_Physics_Algebra_Calculus - Fatal编程技术网

Java 直线相交方法无法正常工作

Java 直线相交方法无法正常工作,java,math,physics,algebra,calculus,Java,Math,Physics,Algebra,Calculus,我正在研究向量数学,我需要计算多边形的法向量。 我的代码: //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

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

    //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
//p是一个参数,是Vec2,第一行的第二个点
double[]vert=getvertices()//[垂直方向的任意长度][2]
对于(int i=0;i如果(a>=0D&&a=0D&&bOops,通过尝试解释它来解决它。我需要进行光线跟踪,或者使用光线相交测试。

如果您试图计算多边形的法线,看起来您试图做一些过于复杂的事情。您可以简单地使用两条相邻边的叉积来获得它。甚至更好,通过简单的数学运算,您甚至不需要计算边向量。只需取每个相邻顶点对的叉积,将它们相加即可:


输入?预期结果?实际结果?如果将其分解为更小的部分(方法),这将大大受益。(顺便说一句,我很确定这里没有微积分,只有向量代数。)我想我已经解决了我的问题,你知道当你试图解释一个问题时你是怎么解决的吗?我在测试线段,而我需要测试向量“射线”,和/或使用光线跟踪。等等。它是二维平面中的一个曲面。法线不总是沿着z轴吗?@jpmc26你对扭矩的想法,在二维的情况下,它通常只表示为由扭矩大小组成的+/-值。法线向量在物理中用于碰撞响应。a是垂直于轴的向量对象。由于对象是多边形(二维)在xy平面上,垂直向量必须指向远离xy平面的方向。不是吗?如果你不是将法线取到多边形而是取到一条线,那么它可以是2d,但我相信曲面的法线必须是3d。所以我遗漏了一些东西,你没有计算多边形的法线,或者法线沿着z轴(此外,扭矩大致相当于力的旋转当量,我看不到参考值。)