返回交叉点时出错

时间:2015-08-05 19:00:02

标签: java geometry

我的算法在此时检查两条线之间的相对位置我确定这些线是并发的,并且想要返回交点。我使用这个公式没有线性系统:
enter image description here

我的问题是当输入行如下:
r:X =(8,1,9)+λ(2,-1,3)
s:X(3,-4,4)+μ(1,-2,2)
我希望输出为(-2,6,6),但是(7.6,1.2,8.4)。
有没有人知道为什么会这样?

我的方法

public Point3D intersectingLines(Line lineOne, Line lineTwo) {
        double x = lineOne.getPoint().getX() - lineTwo.getPoint().getX();
        double y = lineOne.getPoint().getY() - lineTwo.getPoint().getY();
        double z = lineOne.getPoint().getZ() - lineTwo.getPoint().getZ();
        Vector3D pointsDifference = new Vector3D(x, y, z);
        Vector3D second = pointsDifference.crossProduct(lineTwo.getVector());
        Vector3D first = lineOne.getVector().crossProduct(lineTwo.getVector());

        double lambda = first.getNorm() / second.getNorm();
        double xIntersectionOne = lineOne.getPoint().getX() + (lambda * lineOne.getVector().getX());
        double yIntersectionOne = lineOne.getPoint().getY() + (lambda * lineOne.getVector().getY());
        double zIntersectionOne = lineOne.getPoint().getZ() + (lambda * lineOne.getVector().getZ());

        double xInLineTwo = (xIntersectionOne - lineTwo.getPoint().getX()) / lineTwo.getVector().getX();
        double yInLineTwo = (yIntersectionOne - lineTwo.getPoint().getY()) / lineTwo.getVector().getY();
        double zInLineTwo = (zIntersectionOne - lineTwo.getPoint().getZ()) / lineTwo.getVector().getZ();
        //Here I check if the point is even correct or lambda must be negative to obtain the correct point
        if (xInLineTwo == yInLineTwo && xInLineTwo == zInLineTwo) {
            return new Point3D(xIntersectionOne, yIntersectionOne, zIntersectionOne);
        } else {
            xIntersectionOne = lineOne.getPoint().getX() + (-1 * lambda * lineOne.getVector().getX());
            yIntersectionOne = lineOne.getPoint().getY() + (-1 * lambda * lineOne.getVector().getY());
            zIntersectionOne = lineOne.getPoint().getZ() + (-1 * lambda * lineOne.getVector().getZ());

            return new Point3D(xIntersectionOne, yIntersectionOne, zIntersectionOne);
        }
    }

1 个答案:

答案 0 :(得分:0)

主要问题在于等式,其他测试没有区别但是特定的等式是。等式在"反向"为了得到lambda = | 0,2 |而不是lambda = | 5 |。
enter image description here

另一个问题是评论的大卫华莱士,条件检查双打类型的平等。

See more