LineIntersectRect
State = LineIntersectRect(X1#, Y1#, X2#, Y2#, RectX1#, RectY1#, RectX2#, RectY2#)
 
Parameters:

    X1# = The X1 cord of line
    Y1# = The Y1 cord of line
    X2# = The X2 cord of line
    Y2# = The Y2 cord of line
    RectX1# = The Top Left X cord of the region
    RectY1# = The Top Left Y cord of the region
    RectX2# = The Bottom Right X cord of the region
    RectY2# = The Bottom Right Y cord of the region
Returns:

    State = The intersection state (-1= line outside, 0 = line inside, 1=line intersects)
 

      The LineIntersectRect function checks if a line is inside/outside or intersects a rectangular region.


Return Values

      -1 = Line is completely outside of region
      0 = Line is inside this region
      1 = Line intersects one or more of the region edges.



FACTS:


      * If the line intersects the region, you can get it's new clipped coordinates using the GetIntersectX#() and GetIntersectY#() functions.



Mini Tutorial:


      This example lets the user check for a intersection between a user controlled line and a rectangular region. If the line intersects, the clipped line segment (the segment inside the rectangle were clipping against) is displayed.


  
  Repeat
     Cls RGB(0,0,0)
     Print "Press Mouse Button To Change Position of Test Line"
     
   ; Create the coords for LINE A
     If MouseButton()
        x1#=Rnd(GetScreenWidth())
        y1#=Rnd(GetScreenHeight())
     EndIf
     
     x2#=MouseX()
     y2#=MouseY()
     
   ; Draw Line A
     Line x1#,y1#,x2#,y2#
     
   ; Init the coordinates we'll use for our Rect region.
     RectX1#=200
     RectY1#=100
     RectX2#=600
     RectY2#=400
     
   ; Draw a box to represent this region
     Box RectX1#,RectY1#,RectX2#,RectY2#,0
     
   ; Check if Line A and Line B Intersect
     Flag=LineIntersectRect(x1#,y1#,x2#,y2#,Rectx1#,Recty1#,Rectx2#,Recty2#)
     
     Select Flag
             
         Case -1
             Print "Line is outside the region"
             
         Case 0
             Print "Line is inside the region"
             
         Case 1
             Print "Line Intersects the Region"
             ax1#=GetIntersectX#(0)
             ay1#=GetIntersectY#(0)
             bx2#=GetIntersectX#(1)
             by2#=GetIntersectY#(1)
             
           ; draw a read circle to show the intersection point
             CircleC ax1#,ay1#,5,1,RGB(255,0,0)
             CircleC bx2#,by2#,5,1,RGB(255,255,0)
             LineC ax1#,ay1#,bx2#,by2#,RGB(0,255,0)
     EndSelect
     
     
   ; display the screen and continue the repeat/until loop
     Sync
  Until EscKey()
  
  




 
Related Info: CirclesIntersect | GetDistance2D | GetIntersectX# | GetIntersectY# | LineIntersectCircle | LinesIntersect | PointIntersectCircle :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com