Ooops
November 28, 2020, 11:50:57 AM

Author Topic: [bb] Yet Another Line Intersect Function by Andy_A [ 1+ years ago ]  (Read 602 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Yet Another Line Intersect Function
Author : Andy_A
Posted : 1+ years ago

Description : Hopefully this function is easier to understand and has a simple usage demo.

Instructions:
Press a key to generate a new pair of random line segments.
Press [ESC] to exit.


Code :
Code: BlitzBasic
  1. ;Lines Intersect Function
  2. ;       By: Andy Amaya
  3. ; Date: Nov 07, 2006
  4.  
  5. AppTitle "Line Intersection Function by Andy_A"
  6.  
  7. Graphics 800,600,32,2
  8. SetBuffer BackBuffer()
  9.  
  10. Global intersectX%, intersectY%
  11. SeedRnd MilliSecs()
  12.  
  13. While KeyHit(1) = 0
  14.         Cls
  15.         x1 = Rand(0,799) : y1 = Rand(60,599)
  16.         x2 = Rand(0,799) : y2 = Rand(60,599)
  17.         Line(x1,y1, x2,y2)
  18.  
  19.         x3 = Rand(0,799) : y3 = Rand(60,599)
  20.         x4 = Rand(0,799) : y4 = Rand(60,599)
  21.         Line(x3,y3, x4,y4)
  22.  
  23.         result = linesIntersect(x1,y1,x2,y2,x3,y3,x4,y4)
  24.  
  25.         Text 0,0,"Result= "+result
  26.         If result = True
  27.                 Text(10,20,"Intersection Coords")
  28.                 Text(10,40,"   X="+ Str(intersectX) +" , Y="+Str(intersectY))
  29.                 Color 255,0,0
  30.                 Oval intersectX-3, intersectY-3, 7, 7, True
  31.                 Color 255,255,255
  32.         Else
  33.                 Text(10,20,"No Intersection")
  34.         End If
  35.  
  36.         Flip
  37.         WaitKey()
  38. Wend
  39.  
  40. End
  41.  
  42. ;Source: http://Local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
  43. ;Intersection point of two lines
  44. ;(2 dimensions)
  45. Function linesIntersect(x1%,y1%, x2%,y2%, x3%,y3%, x4%,y4%)
  46.  
  47.         numeratorA#  = (x4-x3)*(y1-y3)-(y4-y3)*(x1-x3)
  48.         numeratorB#  = (x2-x1)*(y1-y3)-(y2-y1)*(x1-x3)
  49.         denominator# = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)
  50.  
  51.         If denominator = 0.0 Then
  52. ;               Text(10,20,"No intersection")
  53. ;               If numeratorA = 0.0 And numeratorB = 0.0 And denominator = 0.0 Then
  54. ;                       Text(10,40,"Lines are coincident")
  55. ;               Else
  56. ;                       Text(10,40,"Lines are parallel")
  57. ;               End If
  58.                 Return False
  59.         Else
  60.                 Ua# = numeratorA/denominator
  61.                 Ub# = numeratorB/denominator
  62.                 range1% = Ua >= 0.0 And Ua <= 1.0
  63.                 range2% = Ub >= 0.0 And Ub <= 1.0
  64.                 If range1 And range2 Then
  65.                         intersectX% = Floor(x1 + Ua*(x2-x1)+.5)
  66.                         intersectY% = Floor(y1 + Ua*(y2-y1)+.5)
  67.                         Return True
  68.                 Else
  69. ;                       Text(10,20,"No intersection")
  70.                         Return False
  71.                 End If
  72.     End If
  73. End Function


Comments :


RemiD(Posted 6 months ago)

 @Andy>>thanks for sharing !


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal