Take a look at the below algorithm. Assuming it is working and all. Can you see any speed optimizations that can be done in the code?
Does it take memory or cpu to set all those variables at the start? Should I have those as arguments in the function instead? Would that make any difference? Any optimization tips are welcome.
Does it take memory or cpu to set all those variables at the start? Should I have those as arguments in the function instead? Would that make any difference? Any optimization tips are welcome.
' Check a Line against a Line ' Returns a point if they overlap, null if they do not ' This should be a very fast Algorithm. Method LineOverlap:Point2D( Line:Line2D ) Local Ax# = StartPoint.X Local Ay# = StartPoint.Y Local Bx# = EndPoint.X Local By# = EndPoint.Y Local Cx# = Line.StartPoint.X Local Cy# = Line.StartPoint.Y Local Dx# = Line.EndPoint.X Local Dy# = Line.EndPoint.Y ' Calculate a lot of values ' Their names is unesential ' I name them A,B,C and so on ' This it to make the math easier ' to manage and overlook. Local E# = ( Bx - Ax ) Local F# = ( Dy - Cy ) Local G# = ( By - Ay ) Local H# = ( Dx - Cx ) Local Q# = E*F - G*H If Q = 0 Then Return Null' Can not divide by Zero Local A# = ( Ay - Cy ) Local B# = ( Dx - Cx ) Local C# = ( Ax - Cx ) Local D# = ( Dy - Cy ) Local R# = (A*B - C*D)/Q If R > 1 Return Null If R < 0 Return Null Local C# = (A*E - C*G)/Q If C > 1 Return Null If C < 0 Return Null Local IntersectX# = Ax + R*E Local IntersectY# = Ay + S*G Return Point2D.Create( IntersectX, IntersectY ) EndMethod