33 Votes

Delphi: Is Point (TPoint) inside or outside rectangle (TRect)?

Tip by Delphian | Last update on 2022-04-24 | Created on 2013-04-20

Problem: We would like to know whether a given point with X and Y coordinates is located within a rectangle (TRect) or not.

Answer: Fortunately, Delphi as well as Lazarus are providing a function for this, so that we do not have to compare the coordinates manually. The function is named PtInRect().

Example: Let's have a look at an example for PtInRect():

var
   R: TRect;
   P1, P2: TPoint;
begin
   R:=Rect(10, 10, 100, 100);
   P1:=Point(0, 0);
   P2:=Point(50, 50);

   if PtInRect(R, P1) then showmessage('P1 lies in R!');
   if PTInRect(R, P2) then showmessage('P2 lies in R!');
end;

First of all, we are defining a rectangle R and two points P1 and P2, one located inside the rectangle, the other one not. After that, we are testing with PtInRect, whether the points are lying within the rectangle. P1 should not lie in the rectangle, P2 should.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.