11 Vote

Delphi: Compare two points (TPoint) - Are two points identical?

Tip by Delphian | 2013-04-20 at 17:24

Question: We would like to compare two points of the type TPoint, that is, we would like to check whether the two points have the same coordinates.

Problem: Unfortunately, it is not possible to compare the points using P1 = P2 or P1 <> P2, because a TPoint is a record having stored several data parts (X and Y value). Such complex data types can not simply compared with = or <>.

Solution: Delphi as well as Lazarus are providing the function PointsEqual for this problem. Passing two points as parameters, PointsEqual returns true or false depending on whether the points are equal or not.

Example: This is illustrated in the following example.

var
  P1, P2, P3: TPoint;
begin
  P1 := Point(1, 1);
  P2 := Point(1, 1);
  P3 := Point(1, 2);

  if PointsEqual(P1, P2) then showmessage('P1 and P2 are identical!');
  if PointsEqual(P1, P3) then showmessage('P1 and P3 are identical!');
end;

We define three points P1, P2 and P3, where P1 and P3 are identical. Afterwards, we are checking the points on equality using PointsEqual and we will get the message "P1 and P2 are identical".

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.