[Lazarus] ComparePoints func

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
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

--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
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.



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
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
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" <[hidden email]> 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

--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus


--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list


On Wed, 26 Jul 2017, Graeme Geldenhuys via Lazarus wrote:

> 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.
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
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: >, <, =.

--
Regards,
Alexey

--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list


On Wed, 26 Jul 2017, Alexey via Lazarus wrote:

> 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.
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
IMHO you should consider 5 cases: P1 = P2 and the four quadrants


- 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" <[hidden email]> wrote:
On <a href="tel:26.07.2017%2018" value="+12607201718" target="_blank">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: >, <, =.

--
Regards,
Alexey


--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus


--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
On 2017-07-26 16:22, Michael Van Canneyt via Lazarus 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
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
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.
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
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
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

--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
On Wed, 26 Jul 2017 18:53:34 +0100
Graeme Geldenhuys via Lazarus <[hidden email]> 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.

R.
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list

Am 26.07.2017 19:51 schrieb "Graeme Geldenhuys via Lazarus" <[hidden email]>:
>> 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


--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
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.


On 2017-07-26 21:14, Sven Barth via Lazarus 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
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list


On Wed, 26 Jul 2017, Alexey via Lazarus wrote:

>>> 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.
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list


On Wed, 26 Jul 2017, Reimar Grabowski via Lazarus wrote:

> On Wed, 26 Jul 2017 18:53:34 +0100
> Graeme Geldenhuys via Lazarus <[hidden email]> 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.
--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Lazarus] ComparePoints func

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
 >Therefore it makes no sense to introduce a general
ComparePoints-function.

Yes, it is right, func not needed. And my case [compare source editor x/y coords] is rare.


--
Regards,
Alexey

--
_______________________________________________
Lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Loading...