UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on November 12, 2009, 08:43:00 PM

Title: Check If a point is inside a box/rect region (Manual PointInBox)
Post by: kevin on November 12, 2009, 08:43:00 PM
   This mini example demo's how you can manually detect If a point is inside a box/rect region.  

  Note: This is the equivalent of the built in PointInBox function.

[pbcode]

   setfps 30

   x1=100
   y1=200
   x2=500
   y2=300


   BackDropColour=0


   Do
         ; read the mouse position
         x=mousex()
         y=mousey()

         ; manually check if it's inside a rect shape
         if Check_IF_POint_Inside_Box(X,Y,x1,y1,x2,y2)
               BackdropColour=rgb(100,200,50)
         else
               BackdropColour=0
         endif

         ; draw the screen
         Cls BackDropColour


         boxc x1,y1,x2,y2,true,rgb(0,255,0)
         dot x,y         

      Sync
   loop



Function Check_IF_POint_Inside_Box(MyPointX,MyPointY,x1,y1,x2,y2)
   result=false

   ; check if this point to the right of the boxes left hand side
   if MyPointX>=X1

      ; check if this point to the left of the boxes right hand side
      if MyPointX<=X2

         ; check if our point is within the boxes TOP and BOTTOM limits      
         if MyPointY>=Y1
            if MyPointY<=Y2
                  result=true
            endif
         endif
      endif   
   endif


EndFunction   result


[/pbcode]


 
Related To:

   * Toggle The  Mouse Pointer Off When It's Not Moving (http://www.underwaredesign.com/forums/index.php?topic=3894.0)
   * Storing User Input In Global Types (http://www.underwaredesign.com/forums/index.php?topic=3644.0)
   * Drag Rectangles/ GUI Selection (http://www.underwaredesign.com/forums/index.php?topic=3647.0)