Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-07

ComparePoints func

16 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alexey

I think LCL has many CompareNNNN funcs, but misses func for TPoint. Here it's comparing x/y screen coordinates

function ComparePoints(P1, P2: TPoint): integer;
begin
   if (P1.X=P2.X) and (P1.Y=P2.Y) then exit(0);
   if (P1.Y>P2.Y) then exit(1);
   if (P1.Y<P2.Y) then exit(-1);
   if (P1.X>P2.X) then exit(1) else exit(-1);
end;
Regards, Alexey

Reply by email

Graeme Geldenhuys
to Alexey
Alexey wrote
I think LCL has many CompareNNNN funcs, but misses func for TPoint. Here it's comparing x/y screen coordinates

Why not extend TPoint with an IsEquals() method, so you can do something like

var
   p1: TPoint;
   p2: TPoint;
begin
   // ... populate p1 & p2
   if p1.IsEquals(p2) then
     // do something.

ComparePoints() returning a Integer is not a very friendly return type either, whereas Boolean makes more sense.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Sandro Cumerlato
to Alexey: I think LCL has many CompareNNNN funcs, but misses func for TPoint. Here it's comparing…

What do you think about:

function ComparePoints(P1, P2: TPoint): TPoint;
begin
  Result.X := (P1.X - P2.X);
  Result.Y := (P1.Y - P2.Y);
end;

Best regards.

Sandro Cumerlato

On Jul 26, 2017 3:56 PM, "Alexey via Lazarus"
wrote:

I think LCL has many CompareNNNN funcs, but misses func for TPoint. Here it's comparing x/y screen coordinates

function ComparePoints(P1, P2: TPoint): integer;
begin
  if (P1.X=P2.X) and (P1.Y=P2.Y) then exit(0);
  if (P1.Y>P2.Y) then exit(1);
  if (P1.Y<P2.Y) then exit(-1);
  if (P1.X>P2.X) then exit(1) else exit(-1);
end;
Regards, Alexey --

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys: Why not extend TPoint with an IsEquals() method, so you can do something like var p1:…
Graeme Geldenhuys wrote 19 lines
On 2017-07-26 14:54, Alexey via Lazarus wrote: > I think LCL has many CompareNNNN funcs, but misses func for TPoint. Here > it's comparing x/y screen coordinates Why not extend TPoint with an IsEquals() method, so you can do something like var p1: TPoint; p2: TPoint; begin // ... populate p1 & p2 if p1.IsEquals(p2) then // do something. ComparePoints() returning a Integer is not a very friendly return type either, whereas Boolean makes more sense.

First start by defining what ComparePoints is supposed to measure or compare.

If "equals" is needed, why not simply use p1=p2 ?

Michael.

Reply by email

Michael Van Canneyt
to Alexey
Alexey wrote 6 lines
On 26.07.2017 18:11, Graeme Geldenhuys via Lazarus wrote: > ComparePoints() returning a Integer is not a very friendly return type > either, Hm, in my app Int makes sense: i need to compare Points in binary search, and see: >, <, =.

This compare is very specific to your application.

I don't think it would be a good idea to generalize this.

Michael.

Reply by email

Sandro Cumerlato
to Alexey: Hm, in my app Int makes sense: i need to compare Points in binary search, and see: >, <,…

IMHO you should consider 5 cases: P1 = P2 and the four quadrants

https://en.m.wikipedia.org/wiki/Quadrant_(plane_geometry)

- Quadrant I should correspond to >
- Quadrant III should correspond to <
- What about quadrants II and IV? You could compare your point to Y = -X line to reduce your study to 3 cases.

Best regards

Sandro Cumerlato

On Jul 26, 2017 6:27 PM, "Alexey via Lazarus"
wrote:

Graeme Geldenhuys wrote
ComparePoints() returning a Integer is not a very friendly return type either,

Hm, in my app Int makes sense: i need to compare Points in binary search, and see: >, <, =.

Regards, Alexey --

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt: First start by defining what ComparePoints is supposed to measure or compare. If "equals"…
Michael Van Canneyt wrote
First start by defining what ComparePoints is supposed to measure or compare.

If "compare" doesn't mean "equals", then a better name should be used, otherwise that too is up for incorrect interpretation.

If "equals" is needed, why not simply use p1=p2 ?

If operator overload exists for the types then sure, but as you know I like OOP approaches to things.

Also is TPoint a record or a object or a class?

In fpGUI the equivalent of TPoint and TRect are object types and has many related methods defined inside them. I don't believe p1 = p2 is going to work with the object data type.

https://github.com/graemeg/fpGUI/blob/develop/src/corelib/fpg_base.pas#L160..L191

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Graeme Geldenhuys
to Alexey: Hm, in my app Int makes sense: i need to compare Points in binary search, and see: >, <,…
Alexey wrote
Hm, in my app Int makes sense: i need to compare Points in binary search, and see: >, <, =.

Ever heard of Manhatten Length? That would be a more logical approach. It is a commonly used length calculation for points.

http://en.wikipedia.org/wiki/Taxicab_geometry

The interface as in fpGUI's TfpgPoint:

https://github.com/graemeg/fpGUI/blob/develop/src/corelib/fpg_base.pas#L164..L165

The implementations:

https://github.com/graemeg/fpGUI/blob/develop/src/corelib/fpg_base.pas#L2021..L2029

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Alexey
to Graeme Geldenhuys: If "compare" doesn't mean "equals", then a better name should be used, otherwise that too…
> First start by defining what ComparePoints is supposed to measure or > compare.

yes, of course: compare source editor x/y positions. Ie, it is CompareCoords.

Regards, Alexey -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20170726/6e894067/attachment.html>

Reply by email

Reimar Grabowski
to Graeme Geldenhuys: Ever heard of Manhatten Length? That would be a more logical approach. It is a commonly…
Show quoted text 5 lines
On 2017-07-26 17:27, Alexey via Lazarus wrote: > Hm, in my app Int makes sense: i need to compare Points in binary > search, and see: >, <, =. Ever heard of Manhatten Length? That would be a more logical approach.

If a metric would be more logical (I don't know why exactly it should be but whatever) the euclidean metric would be even more logical, wouldn't it?

It is a commonly used length calculation for points.

That sounds horrible.
Is length really used like this?
The length of two points???
Metrics are distance functions.

Any 2 dimensional metric can be used.
But it may totally not be the thing the TC needs as the metric conditions may not be desirable in his context.

The point is (and Michael made it already) that there is no definition of how to compare points. You have quite a lot of options and the current problem determines which function to use. Therefore it makes no sense to introduce a general ComparePoints-function.

R.

Reply by email

Sven Barth
to Graeme Geldenhuys: If "compare" doesn't mean "equals", then a better name should be used, otherwise that too…

Am 26.07.2017 19:51 schrieb "Graeme Geldenhuys via Lazarus" <

Show quoted text 5 lines
: > If "equals" is needed, why not simply use p1=p2 ? If operator overload exists for the types then sure, but as you know I

like OOP approaches to things.

Also is TPoint a record or a object or a class?

It's an advanced record with methods and operator overloads: https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/rtl/inc/typshrdh.inc?revision=33062&view=markup

In fpGUI the equivalent of TPoint and TRect are object types and has many

related methods defined inside them. I don't believe p1 = p2 is going to work with the object data type.

For object types you'll need to declare global operator overloads to use e.g. the = operator (which have the disadvantage that they don't work with generics... at least not yet :/ ).

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20170726/9ead2c8a/attachment-0001.html>

Reply by email

Graeme Geldenhuys
to Sven Barth

Hi Sven,

Thanks for clarifying the TPoint type. I thought it was a record structure, but just wanted to make sure - as I mentioned, different frameworks or libraries might use different types.

Sven Barth wrote
For object types you'll need to declare global operator overloads to use e.g. the = operator

Then it's also good that I don't like the = operator for objects and classes. As I mentioned earlier, I like the more OOP approach where methods of an object/class act on the data the type holds. So TPoint.Equals() makes much more sense to me that p1 = p2. Just my personal opinion I guess. ;-)

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Michael Van Canneyt
to Alexey: yes, of course: compare source editor x/y positions. Ie, it is CompareCoords. Alexey…
Alexey wrote 5 lines
>> First start by defining what ComparePoints is supposed to measure or >> compare. yes, of course: compare source editor x/y positions. Ie, it is CompareCoords.

In that case you need some enumerated.
TRelativePosition =
(rpEqual,
rpNorth,rpEast,rpSouth,rpWest,

  rpNorthEast,rpSouthEast,rpSouthWest,rpNorhWest);

+1, 0, -1 does not tell you enough.

Michael.

Reply by email

Michael Van Canneyt
to Reimar Grabowski: On Wed, 26 Jul 2017 18:53:34 +0100 If a metric would be more logical (I don't know why…
Reimar Grabowski wrote 22 lines
On Wed, 26 Jul 2017 18:53:34 +0100 Graeme Geldenhuys via Lazarus wrote: > On 2017-07-26 17:27, Alexey via Lazarus wrote: > > Hm, in my app Int makes sense: i need to compare Points in binary > > search, and see: >, <, =. > > Ever heard of Manhatten Length? That would be a more logical approach. If a metric would be more logical (I don't know why exactly it should be but whatever) the euclidean metric would be even more logical, wouldn't it? > It is a commonly used length calculation for points. That sounds horrible. Is length really used like this? The length of two points??? Metrics are distance functions. > http://en.wikipedia.org/wiki/Taxicab_geometry Any 2 dimensional metric can be used. But it may totally not be the thing the TC needs as the metric conditions may not be desirable in his context. The point is (and Michael made it already) that there is no definition of how to compare points. You have quite a lot of options and the current problem determines which function to use. Therefore it makes no sense to introduce a general ComparePoints-function.

Exactly.

Michael.

Reply by email