Board index » cppbuilder » Is this a bug

Is this a bug


2008-03-12 06:05:36 PM
cppbuilder55
Hi,
I am afraid PtInRect function has a bug or someone could explain me why.
The following code snippet has been taken from Types.pas (I am using BCB6).
Shouldn't have to be the code like that
return (P.X>= Rect.Left && P.X <= Rect.Right && P.Y>=
Rect.Bottom && P.Y <= Rect.Top );
Thanks
Sabetay
function PtInRect(const Rect: TRect; const P: TPoint): Boolean;
begin
Result := (P.X>= Rect.Left) and (P.X < Rect.Right) and (P.Y>= Rect.Top)
and (P.Y < Rect.Bottom);
end;
extern PACKAGE bool __fastcall PtInRect(const TRect Rect, const TPoint P);
Description
PtInRect returns true if the point, P, lies inside the rectangle, Rect,
and false if P is not in Rect.
PtInRect does not consider a point inside a rectangle if the rectangle
has a negative width or height. A point is considered inside a rectangle
if it lies on the left or top edge, but not if it lies on the right or
bottom edge.
 
 

Re:Is this a bug

Quote
PtInRect does not consider a point inside a rectangle if the rectangle has
a negative width or height.
What rectangle has a negative width or negative height?
. Ed
Quote
Sabetay Toros wrote in message
news: XXXX@XXXXX.COM ...

I am afraid PtInRect function has a bug or someone could explain me why.
The following code snippet has been taken from Types.pas (I am using
BCB6).

Shouldn't have to be the code like that

return (P.X>= Rect.Left && P.X <= Rect.Right && P.Y>= Rect.Bottom &&
P.Y <= Rect.Top );

Thanks

Sabetay

function PtInRect(const Rect: TRect; const P: TPoint): Boolean;
begin
Result := (P.X>= Rect.Left) and (P.X < Rect.Right) and (P.Y>=
Rect.Top)
and (P.Y < Rect.Bottom);
end;

extern PACKAGE bool __fastcall PtInRect(const TRect Rect, const TPoint P);

Description

PtInRect returns true if the point, P, lies inside the rectangle, Rect,
and false if P is not in Rect.

PtInRect does not consider a point inside a rectangle if the rectangle has
a negative width or height. A point is considered inside a rectangle if it
lies on the left or top edge, but not if it lies on the right or bottom
edge.
 

Re:Is this a bug

Ed Mulroy [TeamB] yazmış:
Quote
>PtInRect does not consider a point inside a rectangle if the rectangle has
>a negative width or height.

What rectangle has a negative width or negative height?

. Ed

>Sabetay Toros wrote in message
>news: XXXX@XXXXX.COM ...
>
>I am afraid PtInRect function has a bug or someone could explain me why.
>The following code snippet has been taken from Types.pas (I am using
>BCB6).
>
>Shouldn't have to be the code like that
>
>return (P.X>= Rect.Left && P.X <= Rect.Right && P.Y>= Rect.Bottom &&
>P.Y <= Rect.Top );
>
>Thanks
>
>Sabetay
>
>function PtInRect(const Rect: TRect; const P: TPoint): Boolean;
>begin
>Result := (P.X>= Rect.Left) and (P.X < Rect.Right) and (P.Y>=
>Rect.Top)
>and (P.Y < Rect.Bottom);
>end;
>
>extern PACKAGE bool __fastcall PtInRect(const TRect Rect, const TPoint P);
>
>Description
>
>PtInRect returns true if the point, P, lies inside the rectangle, Rect,
>and false if P is not in Rect.
>
>PtInRect does not consider a point inside a rectangle if the rectangle has
>a negative width or height. A point is considered inside a rectangle if it
>lies on the left or top edge, but not if it lies on the right or bottom
>edge.


Ed,
My rectangle is something like that
FRect(42947, 47263, 43840, 46363) { Left,Top, Right, Bottom }
And my point is P(43367,46367)
Width = 43840 - 42947>0 (Right - Left)
Height = 47263 - 46323>0 (Top - Bottom)
Is there any other way to calculate Width & Height?
Besides in the .pas file PtInRect function does not control either if
the Height or Width is negative.
Thanks
Sabetay
 

{smallsort}

Re:Is this a bug

"Sabetay Toros" < XXXX@XXXXX.COM >wrote in message
Quote

Hi,

I am afraid PtInRect function has a bug or someone could explain me why. The
following code snippet has been taken from Types.pas (I am using BCB6).

Shouldn't have to be the code like that

return (P.X>= Rect.Left && P.X <= Rect.Right && P.Y>= Rect.Bottom
&& P.Y <= Rect.Top );

PtInRect is for use with Windows GDI, where the origin is in the upper left of
the screen. Therefore Rect.Bottom>= Rect.Top.
--
Bruce
 

Re:Is this a bug

On Wed, 12 Mar 2008 12:05:36 +0200, Sabetay Toros wrote:
Quote
[snip]
PtInRect does not consider a point inside a rectangle if the rectangle
has a negative width or height.
Yes. It is relatively rare that a right or bottom coordinate would be
less than a left or top coordinate, so for performance reasons in the
most common situations, I'm glad it does not handle the other case.
You will have to make sure that your Left < Right and Top < Bottom before
passing to PtInRect, something like (untested, also note r passed by
value):
bool TestPoint (TRect r, const TPoint &p) {
if (r.Left>r.Right) std::swap(r.Left, r.Right);
if (r.Top>r.Bottom) std::swap(r.Top, r.Bottom);
return PtInRect(r, p);
}
Quote
A point is considered inside a rectangle
if it lies on the left or top edge, but not if it lies on the right or
bottom edge.
This is correct. TRect and TPoint are direct analogs for the Windows RECT
and POINT structures. In the Windows API, it is a common convention that
the Right and Bottom coordinate be 1 beyond the actual bounds of the
rectangle. The width and height of a rectangle can be calculated just by
doing Right-Left or Bottom-Top; e.g. a 1x1 rectangle located at
coordinates X=9, Y=12 would be:
Left = 9
Top = 12
Right = 10
Bottom = 13
It is only a matter of convention, Windows API functions that deal with
RECT structures assume that this is the case, and I'm guessing,
therefore, that VCL functions dealing with TRects make the same
assumptions. For the sake of consistency, especially if you are planning
on using these structures with VCL / Windows API, you should also stick
to this convention.
Jason
 

Re:Is this a bug

" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
Quote

>

PtInRect is for use with Windows GDI, where the origin is in the upper
left of the screen. Therefore Rect.Bottom>= Rect.Top.
I believe that is the case only for _most_ DeviceContext mapping modes, not
all...
 

Re:Is this a bug

On Wed, 12 Mar 2008 08:35:11 -0500, Bruce Salzman wrote:
Quote
PtInRect is for use with Windows GDI,
There are other API functions besides GDI functions that use TRect (RECT).
Quote
where the origin is in the upper
left of the screen.
More often than not the origin is actually the upper-left corner of the
window you are drawing on, not the screen. I believe what you mean to say
is that the positive Y axis goes from "top" to "bottom", and then you can
say:
Quote
Therefore Rect.Bottom>= Rect.Top.
Jason
 

Re:Is this a bug

Functions such as PtInRect deal with the Windows GUI so are used in
calculating during screen update time. A consequence of that is that their
processing time be short. To that end the function assumes the calling
parameter will contain a standard collection of values, left <= right,
bottom <= top so that it need not spend CPU cycles checking caller data.
If you wish to use non-standard data then create a non-standard function
which can handle it and use that instead of using the CodeGear
Types::PtInRect or the Windows API ::PtInRect.
. Ed
Quote
Sabetay Toros wrote in message
news:47d7d9a0$ XXXX@XXXXX.COM ...

My rectangle is something like that

FRect(42947, 47263, 43840, 46363) { Left,Top, Right, Bottom }

And my point is P(43367,46367)

Width = 43840 - 42947>0 (Right - Left)
Height = 47263 - 46323>0 (Top - Bottom)

Is there any other way to calculate Width & Height?

Besides in the .pas file PtInRect function does not control either if the
Height or Width is negative.
 

Re:Is this a bug

On Wed, 12 Mar 2008 11:12:05 -0400, dhoke wrote:
Quote
" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
news:47d7dc0d$ XXXX@XXXXX.COM ...
>
>
>>
>PtInRect is for use with Windows GDI, where the origin is in the upper
>left of the screen. Therefore Rect.Bottom>= Rect.Top.
I believe that is the case only for _most_ DeviceContext mapping modes,
not all...
Read this, and the documents it leads to:
msdn2.microsoft.com/en-us/library/ms536132(VS.85).aspx
Or: tinyurl.com/yr8nq2
As far as the right and bottom edge goes in the O.P., here is one
specific mention of that convention (FillRect docs on msdn.microsoft.com):
"When filling the specified rectangle, FillRect does not include the
rectangle's right and bottom sides. GDI fills a rectangle up to, but not
including, the right column and bottom row, regardless of the current
mapping mode."
One thing to remember is that "right", "top", "left", and "bottom" are
just definitions; they don't necessarily correspond to that physical
location on your monitor. For example, many of the right-to-left reading
order window flags, and functions like SetLayout, reverse right and left.
Also, you can turn your monitor on it's side, or upside-down. However,
you do always know that in terms of the X and Y coordinates you are
using, bottom should be>= top in Y and right should be>= left in X.
Jason