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.
About the Author
The author has not added a profile short description yet.
Show Profile
Related Topics
HTML5 Canvas: Beginner Tutorial Chapter 3 - Rectangles and Circles
Tutorial | 0 Comments
Delphi: Compare two points (TPoint) - Are two points identical?
Tip | 0 Comments
Delphi: GetCursorPos problem on Windows 8.1 with scaling
Open Question | 1 Answer
Delphi/Lazarus: Retrieve Cursor Position relative to Form/Window
Tip | 0 Comments
Delphi/Lazarus: 3 Ways to round a Number to X Decimal Places
Tutorial | 7 Comments
Delphi/Lazarus: Only allow Numbers in TEdit
Tip | 0 Comments
Delphi/Lazarus: Is the ALT, SHIFT or CTRL key pressed?
Tutorial | 0 Comments
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.