[Lazarus] ptInRect question

Graeme Geldenhuys graemeg.lists at gmail.com
Fri Jan 30 08:23:40 CET 2009


On Fri, Jan 30, 2009 at 12:11 AM, Michael Van Canneyt
<michael at freepascal.org> wrote:
>
> function PtInRect(const Rect : TRect;const p : TPoint) : Boolean;
>
> begin
>  PtInRect:=(p.y>=Rect.Top) and
>            (p.y<Rect.Bottom) and
>            (p.x>=Rect.Left) and
>            (p.x<Rect.Right);
> end;
>
> 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.

I would guess to conform with Microsoft's GDI behaviour. GDI
rectangles are drawn one pixel less than what you specified. Probably
also relates to Mattias's answer, that a pixel also takes up a certain
amount of space. I remember in fpGUI I had plenty of issues getting
the GDI and X11 drawing backends to work identical. Hence I created a
custom TfpgRect which includes the Width and Height values. In fpGUI I
tried to make all Canvas drawing methods use Top,Left,Width and
Height. This gave a more consistent and expected behaviour.

  TfpgRect = object  // not class for static allocations
    Top: TfpgCoord;
    Left: TfpgCoord;
    Width: TfpgCoord;
    Height: TfpgCoord;
    procedure SetRect(aleft, atop, awidth, aheight: TfpgCoord);
    function  Bottom: TfpgCoord;
    function  Right: TfpgCoord;
    procedure SetBottom(Value: TfpgCoord);
    procedure SetRight(Value: TfpgCoord);
  end;


Regards,
  - Graeme -


_______________________________________________
fpGUI - a cross-platform Free Pascal GUI toolkit
http://opensoft.homeip.net/fpgui/



More information about the Lazarus mailing list