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:
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
now vi q found the result. td well then:)
– Felipe