DelphiBasics
PtInRect
Function
Tests to see if a point lies within a rectangle Types unit
 function PtInRect(const TheRectangle TRect; const ThePoint TPoint):Boolean;
Description
The PtInRect function returns true if ThePoint lies within TheRectangle.
 
Note that the rectangle inside is defined as:
 
(left, top, right-1, bottom-1)
Related commands
BoundsCreate a TRect value from top left and size values
PointGenerates a TPoint value from X and Y values
PointsEqualCompares two TPoint values for equality
RectCreate a TRect value from 2 points or 4 coordinates
TPointHolds X and Y integer values
TRectHolds rectangle coordinate values
 Download this web site as a Windows program.




 
Example code : Determine inside and outside points of a rectangle
var
  myRect : TRect;

begin
  // Create a rectangle
  // Note : The rectangle inisde starts at top left, but ends
  //        1 pixel inside bottom right.
  myRect := Rect(20, 30, 100, 200);

  // Check to see if (20,30) is inside the rectangle
  if PtInRect(myRect, Point(20,30))
  then ShowMessage(' 20, 30 is  inside the rectangle')
  else ShowMessage(' 20, 30 is outside the rectangle');

  // Check to see if (99,199) is inside the rectangle
  if PtInRect(myRect, Point(99,199))
  then ShowMessage(' 99,199 is  inside the rectangle')
  else ShowMessage(' 99,199 is outside the rectangle');

  // Check to see if (100,200) is inside the rectangle
  if PtInRect(myRect, Point(100,200))
  then ShowMessage('100,200 is  inside the rectangle')
  else ShowMessage('100,200 is outside the rectangle');
end;
Show full unit code
   20, 30 is inside the rectangle
   99,199 is inside the rectangle
  100,200 is outside the rectangle
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page