Advertisement
Dr_Davenstein

2d intersection stuff

Nov 12th, 2023
1,694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "fbgfx.bi"
  2. Randomize Timer
  3. screenres 640,480,32
  4.  
  5. Const As Single PI = 3.1415926535897932, PI2 = PI*2, PId2 = PI/2, PID180 = PI/180
  6.  
  7.  
  8. Type vec2f
  9.     As Single x,y
  10. End Type
  11.  
  12.  
  13. Declare Operator + ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  14. Declare Operator + ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  15. Declare Operator - ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  16. Declare Operator - ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  17. Declare Operator * ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  18. Declare Operator * ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  19. Declare Operator / ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  20. Declare Operator / ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  21. Declare Operator / ( Byval lhs As vec2f, Byval rhs As Integer ) As vec2f
  22. Declare Function Lines_Intersect( Byref ipoint As vec2f, Byref va As vec2f, Byref vb As vec2f, Byref vc As vec2f, Byref vd As vec2f ) As Integer
  23.  
  24. Dim As vec2f Line1(1 To 2), Line2(1 To 20)
  25.  
  26.  
  27. For i As Integer = 1 To Ubound(Line2)-1
  28.    
  29.     Dim As Single rad = i * ( PI2 / ( Ubound(Line2) - 1 ) )
  30.     Dim As Integer rSize = 100 + Int( Rnd * 50)
  31.     Line2(i) = Type( 320 + rSize * Cos(rad), 240 + rSize * -Sin(rad) )
  32.    
  33. Next
  34.  
  35. Line2( Ubound(Line2) ).X = Line2(1).X
  36. Line2( Ubound(Line2) ).Y = Line2(1).Y
  37.  
  38.  
  39. dim as integer mx, my
  40. dim as vec2f p1, p2, center
  41.  
  42.  
  43.  
  44. do
  45.     getmouse(mx,my)
  46.    
  47.     Dim As Single rad = Timer * .25
  48.     Line1(1) = Type(320, 240)
  49.     Line1(2) = Type( 320 + 125 *  Cos(rad), 240 + 125 * -Sin(rad) )
  50.    
  51.    p1 = Line1(1)
  52.    p2 = type(mx, my)
  53.    
  54.    Dim As vec2f intersection_point
  55.    dim as integer hCount
  56.    
  57.    For i As Integer = 1 To Ubound(Line2)-1
  58.    
  59.     If Lines_Intersect( intersection_point, Line2(i), Line2(i+1), p1, p2 ) then
  60.         hCount+=1
  61.     end if
  62.    
  63.    next
  64.    
  65.    
  66.     screensync
  67.     screenlock
  68.     cls
  69.    
  70.     intersection_point = Line1(2)
  71.    
  72.    
  73.     For i As Integer = 1 To Ubound(Line2)-1
  74.         Dim As Integer linecol = &hffffff
  75.        
  76.         If Lines_Intersect( intersection_point, Line2(i), Line2(i+1), Line1(1), Line1(2) ) Then
  77.             'linecol = &h0000ff
  78.         End If
  79.        
  80.         line(Line2(i).X, Line2(i).Y)-(Line2(i+1).X, Line2(i+1).Y),linecol
  81.     Next
  82.    
  83.    if (hCount and 1) = 0 then
  84.        
  85.         print "Mouse inside"
  86.     paint(320,240),&HFF0000,&HFFFFFF
  87.     else
  88.        
  89.         print "Mouse outside"
  90.        
  91.    end if
  92.    
  93.    
  94.     Line(Line1(1).X, Line1(1).Y)-(intersection_point.x, intersection_point.y),&hffff00
  95.    
  96.     circle(intersection_point.x, intersection_point.y), 3
  97.    
  98.    
  99.    
  100.     screenunlock
  101.    
  102.     Sleep 3,1
  103.    
  104. Loop Until multikey(FB.SC_ESCAPE)
  105.  
  106.  
  107.  
  108. Function Lines_Intersect( Byref ipoint As vec2f, Byref va As vec2f, Byref vb As vec2f, Byref vc As vec2f, Byref vd As vec2f ) As integer
  109.    
  110.     Dim As vec2f v1 = vb - va
  111.     Dim As vec2f v2 = vd - vc
  112.     Dim As vec2f v3 = vc - va
  113.     Dim As single perp = (v1.x * v2.y) - (v1.y * v2.x)
  114.    
  115.     If perp<>0 Then
  116.         Dim As Single d1 = ((v3.x * v2.y) - (v3.y * v2.x)) / perp
  117.         If  d1<=1 And d1>=0 Then
  118.             Dim As Single d2 = ((v3.x * v1.y) - (v3.y * v1.x)) / perp
  119.             If d2<=1 And d2>=0 Then
  120.                 Dim As Single m1, m2, c1, c2, recip
  121.                
  122.                 If v1.x <> 0 Then
  123.                     m1 = v1.y / v1.x
  124.                 Else
  125.                     m1 = 10000000000
  126.                 End If
  127.                
  128.                 If v2.x <> 0 Then
  129.                     m2 = v2.y / v2.x
  130.                 Else
  131.                     m2 = 10000000000
  132.                 End If
  133.                
  134.                 c1 = va.y - (m1*va.x)
  135.                 c2 = vc.y - (m2*vc.x)
  136.                
  137.                 recip = 1 / -(m1 - m2)
  138.                
  139.                 ipoint.x = -(c2-c1) * recip
  140.                 ipoint.y = ((m2*c1) - (m1*c2)) * recip
  141.                
  142.                 Return TRUE
  143.             End If
  144.         End If
  145.     End If
  146.    
  147.     Return FALSE
  148. End Function
  149.  
  150.  
  151. Operator + ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  152. Return type<vec2f>( lhs.x + rhs.x, lhs.y + rhs.y )
  153. End Operator
  154.  
  155. Operator + ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  156. Return type<vec2f>( lhs.x + rhs, lhs.y + rhs )
  157. End Operator
  158.  
  159. Operator - ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  160. Return type<vec2f>( lhs.x - rhs.x, lhs.y - rhs.y )
  161. End Operator
  162.  
  163. Operator - ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  164. Return type<vec2f>( lhs.x - rhs, lhs.y - rhs )
  165. End Operator
  166.  
  167. Operator * ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  168. Return type<vec2f>( lhs.x * rhs.x, lhs.y * rhs.y )
  169. End Operator
  170.  
  171. Operator * ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  172. Return type<vec2f>( lhs.x * rhs, lhs.y * rhs )
  173. End Operator
  174.  
  175. Operator / ( Byval lhs As vec2f, Byval rhs As vec2f ) As vec2f
  176. Return type<vec2f>( lhs.x / rhs.x, lhs.y / rhs.y )
  177. End Operator
  178.  
  179. Operator / ( Byval lhs As vec2f, Byval rhs As Single ) As vec2f
  180. Return type<vec2f>( lhs.x / rhs, lhs.y / rhs )
  181. End Operator
  182.  
  183. Operator / ( Byval lhs As vec2f, Byval rhs As Integer ) As vec2f
  184. Return type<vec2f>( lhs.x / rhs, lhs.y / rhs )
  185. End Operator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement