Algorithm to return the point of intersection

Asked

Viewed 825 times

4

I need to return the relative positions between two straight lines, at this point in the program I’m sure they are competitors. My method performs the following equation to avoid the use of linear systems:
inserir a descrição da imagem aqui

I find a lambda and replace in the equation to find common ground. My problem is: all cases of testing I wrote I got the right way out except for when the equation is as follows:

r: (x, y, z) = (8, 1, 9) + λ(2, -1, 3)
s: (x, y, z) = (3, -4, 4) + µ(1, -2, 2)

In this case the return should be the point (-2, 6, -6) but I get the point (7.6, 1.2, 8.4).

My method (After I discovered the lambda, I check whether the lambda value should be -lambda or lambda)

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 xIntersection = lineOne.getPoint().getX() + (lambda * lineOne.getVector().getX());
        double yIntersection = lineOne.getPoint().getY() + (lambda * lineOne.getVector().getY());
        double zIntersection = lineOne.getPoint().getZ() + (lambda * lineOne.getVector().getZ());

        double xInLineTwo = (xIntersection - lineTwo.getPoint().getX()) / lineTwo.getVector().getX();
        double yInLineTwo = (yIntersection - lineTwo.getPoint().getY()) / lineTwo.getVector().getY();
        double zInLineTwo = (zIntersection - lineTwo.getPoint().getZ()) / lineTwo.getVector().getZ();

        if (xInLineTwo == yInLineTwo && xInLineTwo == zInLineTwo) {
            return new Point3D(xIntersection, yIntersection, zIntersection);
        } else {
            xIntersection = lineOne.getPoint().getX() + (-1 * lambda * lineOne.getVector().getX());
            yIntersection = lineOne.getPoint().getY() + (-1 * lambda * lineOne.getVector().getY());
            zIntersection = lineOne.getPoint().getZ() + (-1 * lambda * lineOne.getVector().getZ());

            return new Point3D(xIntersection, yIntersection, zIntersection);
        }
    }

My test

@Test
    public void testgetRelativePosition_concurrentsTwo() throws Exception{
        Line lineOne = new Line().setPoint(new Point3D(8.0, 1.0, 9.0)).setVector(new Vector3D(2.0, -1.0, 3.0));
        Line lineTwo = new Line().setPoint(new Point3D(3.0, -4.0, 4.0)).setVector(new Vector3D(1.0, -2.0, 2.0));

        Point3D expected = new Point3D(-2.0, 6.0, -6.0);
        Point3D actual = new RelativePositions().intersectingLines(lineOne, lineTwo);
        Assert.assertEquals(expected, actual);
    }

java.lang.AssertionError: 
Expected :Point3D [x = -2.0, y = 6.0, z = -6.0]
Actual   :Point3D [x = 7.6, y = 1.2, z = 8.4]

What is the error in the code that does not match the formula? Thank you if someone can help me

Reference to the origin of that formula

2 answers

5


The main problem is in the formula in question, both the table test and the program returned the same results. This formula is reversed so it was obtained λ = |5/25| instead of λ = |25/5| which is correct, for all other test cases this made no difference. inserir a descrição da imagem aqui
Another problem was noted in my question on Soen, is made a comparison between double types and which will probably be false because they will not be exact:

I Suspect the problem is the line if (xInLineTwo == yInLineTwo && xInLineTwo == zInLineTwo). Even in the case Where this condition should hold, it’s unlikely to hold Exactly, as These are all double variables, not Exact Numbers. You should probably apply some Kind of tolerance to this condition, for example, Math.abs(xInLineTwo - yInLineTwo) < 0.001 or Something of that Kind

Discussion in Mathematics

0

Daniela, you could modify your test using the following two lines, please?

Obs1: leave all coordinates Z=0 to make drawing easier on a plane (x,y) instead of a space (x,y,z)

Obs2: I am providing two points of the line, instead of a point and a vector, to make it easier to see too (just make a conversation to rotate in your test :))

-line 1: starting point (x=1, y=2) and period (x=4, y=4)

-Linha2: starting point (x=2, y=1) and period (x=3, y=5)

I drew these two lines on a checkered paper, and they did intersection at the point:

(x=3.5 and y=3)

Could you post the new version of @test when you can adapt to these new two lines? Sorry to bother you, but I don’t have any java compiler on this pc to test your code, but I can help by inference :) --- I program in opengl, so I think I can give a little push :)

  • now vi q found the result. td well then:)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.