December 03, 2020, 07:27:08 PM

Author Topic: [bb] Lines_Intersect() by sswift [ 1+ years ago ]  (Read 370 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Lines_Intersect() by sswift [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Lines_Intersect()
Author : sswift
Posted : 1+ years ago

Description : This function determines if two line segments intersect in 2D, and if so, where they intersect.

Code :
Code: BlitzBasic
  1. Global Intersection_X#                                                                  ; Values returned by the Lines_Intersect() function.
  2. Global Intersection_Y#
  3. Global Intersection_AB#
  4. Global Intersection_CD#
  5.  
  6.  
  7. ; -------------------------------------------------------------------------------------------------------------------
  8. ; This function determines if two lines in intersect in 2D.
  9. ;
  10. ; A & B are the endpoints of the first line segment.  C & D are the endpoints of the second.
  11. ;
  12. ;
  13. ; If the lines DO NOT instersect, the function returns FALSE.
  14. ;
  15. ; If the lines DO intersect, the point of intersection is returned in the global variables:
  16. ; Intersection_X#, Intersection_Y#, Intersection_AB#, and Intersection_CD#
  17. ;
  18. ;
  19. ; Those last two variables indicate the location along each line segment where the point of intersection lies.
  20. ;
  21. ; For example:
  22. ;
  23. ; If Intersection_AB# is 0, then the point of intersection is at point A.  If it is 1, then it is at point B.
  24. ; If it is 0.5, then it is halfway between the two.  And if it is less than 0 or greater than 1, then the point lies
  25. ; on the line but outside of the specified line segment.
  26. ;
  27. ;
  28. ; Because you can determine if the intersection point lies within both line segments, you can also use this function
  29. ; to check to see if the line segments themselves intersect.
  30. ;
  31. ; Also, if these line segments indicate vectors of motion, then if either of the location values returned is negative
  32. ; then you know that the objects paths intersected in the past, and will not intersect in the future.
  33. ;
  34. ; And finally, please note that segments which are coincident (lie on the same line) are considered to be
  35. ; non-intersecting, as there is no single point of intersection.  You can easily detect this condition by changing
  36. ; the code below slightly as indicated.
  37. ; -------------------------------------------------------------------------------------------------------------------
  38. Function Lines_Intersect(Ax#, Ay#, Bx#, By#, Cx#, Cy#, Dx#, Dy#)
  39.  
  40.  
  41.         Rn# = (Ay#-Cy#)*(Dx#-Cx#) - (Ax#-Cx#)*(Dy#-Cy#)
  42.         Rd# = (Bx#-Ax#)*(Dy#-Cy#) - (By#-Ay#)*(Dx#-Cx#)
  43.  
  44.         If Rd# = 0
  45.                
  46.                 ; Lines are parralel.
  47.  
  48.                 ; If Rn# is also 0 then lines are coincident.  All points intersect.
  49.                 ; Otherwise, there is no intersection point.
  50.        
  51.                 Return False
  52.        
  53.         Else
  54.        
  55.                 ; The lines intersect at some point.  Calculate the intersection point.
  56.        
  57.                 Sn# = (Ay#-Cy#)*(Bx#-Ax#) - (Ax#-Cx#)*(By#-Ay#)
  58.  
  59.                 Intersection_AB# = Rn# / Rd#
  60.                 Intersection_CD# = Sn# / Rd#
  61.  
  62.                 Intersection_X# = Ax# + Intersection_AB#*(Bx#-Ax#)
  63.                 Intersection_Y# = Ay# + Intersection_AB#*(By#-Ay#)
  64.                        
  65.                 Return True
  66.                
  67.         EndIf
  68.  
  69.  
  70. End Function


Comments :


AntonyWells(Posted 1+ years ago)

 Doesn't work, always returns true intersection and treats the second line as if it were infinitely long. I.e it's accurate but not usable.


RemiD(Posted 6 months ago)

 @SSwift>>thanks for sharing !


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal