Algorithm to return the point of intersection

4

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

    
asked by anonymous 05.08.2015 / 21:32

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 inverted so it was obtained λ = | 5/25 | instead of λ = | 25/5 | which is correct, for all other test cases this made no difference. >Anotherproblemwasnoticedinmyquestiononthe SOen , a comparison is made between double types and that 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, 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

    
05.08.2015 / 23:10
0

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

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

obs2: I'm supplying two points of the line, instead of a dot and a vector, to make it easier to see (just do a little '

-line1: start point (x = 1, y = 2) and end point (x = 4, y = 4)

-line2: start point (x = 2, y = 1) and end point (x = 3, y = 5)

I drew these two lines on a checkered paper, and they intersected 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 for the inconvenience, but I do not 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 damn:)

    
06.08.2015 / 00:16