[Lazarus] ptInRect question
Hans-Peter Diettrich
DrDiettrich1 at aol.com
Fri Jan 30 09:01:57 CET 2009
Michael Van Canneyt schrieb:
> Is there a reason why the top/left sides are included (>=), and the
> left/bottom sides are not included ? I would expect them to be included as well.
There exist reasons, mostly the definition in the WinAPI.
Dunno how to explain the most important reason, but let me try it:
Take an arbitrary scale, e.g. for temparatures or dates, and try to map
coordinates into differences. Easy? Not really, when you have to work
with integral numbers. Do you want to paint points (pixels) *at* given
coordinates (tick marks for the scale), or lines (distances) *between*
two coordinates? Then you see that you can't do both at once, for a
distance of 1 unit (painting: 1 pixel) :-(
On a 2D canvas a grid for cells of size=1 (pixel) either would fill the
entire area or, when you fill the cells with pixels, there's no room
left for grid lines.
As a practical compromise a canvas is painted in pixels, i.e. rectangles
of size=1, so that a rectangle from (0,0) to (1,1) has a *size* of 1
pixel, and that single pixel is *located* at the coordinates (0,0). Now
you see that the end coordinates of a rectangle are *not* part of the
area of the rectangle.
One should understand that the transformation of rectangles, between
origin/extent (XY)-(WH) and TopLeft/BottomRight (TL)-(BR)
representation, includes an imaginary shift of half a pixel. I really
would appreciate different data types for both representations, instead
of the unique TRect record - then it were obvious, that PtInRect works
with the (TL)-(BR) representation, and consequently has to exclude the
bottom and right values from the area of the rectangle.
The same considerations apply to lines, as mentioned above. When the pen
is moved to (x0,y0) on a canvas, and then consecutive lines are drawn to
(x1,y1) and (x2,y2), the point (x1,y1) is excluded from the first line,
and (x2,y2) is excluded from the second line. Nonetheless the pen is
*located* at (x2,y2) afterwards. This way one can paint a rectangular
border, with a PolyLine(T,L, T,R, B,R, B,L, T,L), without repainting the
edges of the rectangle. When the lines are painted in different colors,
the end points are painted in the color of the *next* line.
Imagine how complicated were the calculation of the "real" (not
excluded) end points of a series of lines, when arbitrary
world-coordinates are used, expressed in something different from
integral pixel coordinates.
HTH
DoDi
More information about the Lazarus
mailing list