I need to return the relative positions between two lines, at this point in the program I'm sure they are competing. My method performs the following equation to avoid using linear systems:
Ifindalambdaandreplaceitintheequationtofindthepointincommon.Myproblemis:alltestcasesIwrotegotthecorrectoutputexceptwhentheequationisasfollows:
r:(x,y,z)=(8,1,9)+λ(2,-1,3)
s(x,y,z)=(3,-4,4)+μ(1,2,2)
Inthiscasethereturnmustbethepoint(-2,6,-6)butIgetthepoint(7.6,1.2,8.4).
Mymethod(AfterIdiscoveredthelambda,Icheckedwhetherthelambdavalueshouldbe-lambdaorlambda)
publicPoint3DintersectingLines(LinelineOne,LinelineTwo){doublex=lineOne.getPoint().getX()-lineTwo.getPoint().getX();doubley=lineOne.getPoint().getY()-lineTwo.getPoint().getY();doublez=lineOne.getPoint().getZ()-lineTwo.getPoint().getZ();Vector3DpointsDifference=newVector3D(x,y,z);Vector3Dsecond=pointsDifference.crossProduct(lineTwo.getVector());Vector3Dfirst=lineOne.getVector().crossProduct(lineTwo.getVector());doublelambda=first.getNorm()/second.getNorm();doublexIntersection=lineOne.getPoint().getX()+(lambda*lineOne.getVector().getX());doubleyIntersection=lineOne.getPoint().getY()+(lambda*lineOne.getVector().getY());doublezIntersection=lineOne.getPoint().getZ()+(lambda*lineOne.getVector().getZ());doublexInLineTwo=(xIntersection-lineTwo.getPoint().getX())/lineTwo.getVector().getX();doubleyInLineTwo=(yIntersection-lineTwo.getPoint().getY())/lineTwo.getVector().getY();doublezInLineTwo=(zIntersection-lineTwo.getPoint().getZ())/lineTwo.getVector().getZ();if(xInLineTwo==yInLineTwo&&xInLineTwo==zInLineTwo){returnnewPoint3D(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());returnnewPoint3D(xIntersection,yIntersection,zIntersection);}}
Mytest
@TestpublicvoidtestgetRelativePosition_concurrentsTwo()throwsException{LinelineOne=newLine().setPoint(newPoint3D(8.0,1.0,9.0)).setVector(newVector3D(2.0,-1.0,3.0));LinelineTwo=newLine().setPoint(newPoint3D(3.0,-4.0,4.0)).setVector(newVector3D(1.0,-2.0,2.0));Point3Dexpected=newPoint3D(-2.0,6.0,-6.0);Point3Dactual=newRelativePositions().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]
Whaterrorinthecodethatdoesnotmatchtheformula?Thanksifanyonecanhelpme
Source reference for this formula