Recent

Author Topic: Adjacent rectangles not aligned  (Read 687 times)

ron.dunn

  • New Member
  • *
  • Posts: 28
Adjacent rectangles not aligned
« on: January 17, 2026, 12:54:19 am »
Drawing adjacent rectangles on a canvas has a small gap between rectangles.

This small fragment draws two rectangles which share a boundary on X=100.

Code: Pascal  [Select][+][-]
  1.     with cDraw.Canvas do begin
  2.         Rectangle (10,10,100,100);
  3.         Rectangle (100,10,200,200);
  4.         end;
  5.  

This creates the attached image, where there is a visible gap between the rectangles.

If I change the code to this - note the 101 in the first Rectangle - then the rectangles align perfectly.

Code: Pascal  [Select][+][-]
  1.     with cDraw.Canvas do begin
  2.         Rectangle (10,10,101,100);
  3.         Rectangle (100,10,200,200);
  4.         end;
  5.  

This seems wrong to me, is there an option or explanation that I'm missing?

Edit: Desktop is Linux Mint.
« Last Edit: January 17, 2026, 12:58:03 am by ron.dunn »

jamie

  • Hero Member
  • *****
  • Posts: 7518
Re: Adjacent rectangles not aligned
« Reply #1 on: January 17, 2026, 01:28:45 am »
There is a slight error in the implementation of the rectangle where what you indicate should work but does not.

This is because that overload you are using calls the TRECT version which requires you to add +1 to the Right and Bottom of the TRECT which has been around and adopted via MS-Windows etc.

 The reason for the extra values on the Right and Bottom is due to being able to report and calculate the width and height of the interior without extra coding, simply Right-Left gives you the width.


Getting back to the overload you are using, that version is calling the TRECT version without regard of adding +1 to the Right and Bottom.

So, add + 1 to those values for now. Maybe aliens will come and visit us and fix all our problems!

Jamie


The only true wisdom is knowing you know nothing

ron.dunn

  • New Member
  • *
  • Posts: 28
Re: Adjacent rectangles not aligned
« Reply #2 on: January 17, 2026, 01:31:51 am »
Thank you, Jamie, much appreciated.

VisualLab

  • Hero Member
  • *****
  • Posts: 712
Re: Adjacent rectangles not aligned
« Reply #3 on: January 18, 2026, 12:16:40 pm »
Drawing adjacent rectangles on a canvas has a small gap between rectangles.

<cut>...</cut>

This creates the attached image, where there is a visible gap between the rectangles.

If I change the code to this - note the 101 in the first Rectangle - then the rectangles align perfectly.

<cut>...</cut>

This seems wrong to me, is there an option or explanation that I'm missing?

Try drawing these rectangles as filled rectangles, for example, the left rectangle in red and the right one in blue, but without any outlines. Then, take two screenshots of both drawn cases, paste them (each separately) into a graphics editor, and enlarge them several times. You will then notice that in the case of the code that adds 1 pixel:

Code: Pascal  [Select][+][-]
  1. Rectangle (10,10,101,100);

the edge of the rectangle drawn later slightly obscures the edge of the rectangle drawn earlier. Exactly 1 pixel wide. Avoiding this behavior would probably be more cumbersome than adding 1 pixel in your case. Your image, named "separate.jpg," actually shows two adjacent rectangles (aligned). But the optical illusion creates the impression that they are separated because you only drew the outlines of the rectangles.

Therefore, the drawing routines work so that the graphic shape is drawn to a coordinate that is 1 pixel smaller (both vertically and horizontally) than the given one. This was probably to avoid overlapping 1 pixel wide shapes. Avoiding the overlap would probably be more cumbersome to work around than adding 1 pixel for your case.

440bx

  • Hero Member
  • *****
  • Posts: 6070
Re: Adjacent rectangles not aligned
« Reply #4 on: January 18, 2026, 01:56:29 pm »
In addition to what @Jamie and @VisualLab mentioned, you'll find some comments specific to how Rectangle works along with screenshots at:
https://forum.lazarus.freepascal.org/index.php/topic,53690.msg397420.html#msg397420

While that thread is Windows specific, the behavior of the Rectangle function seems to be the same in Linux, making those comments equally applicable to the situation you have.  As they say, a picture is worth a 1000 words (in this case a screenshot ;) )

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 7518
Re: Adjacent rectangles not aligned
« Reply #5 on: January 18, 2026, 02:36:34 pm »
I would use the TRECT version and add the extra +1 as it should be just in case the non-TRect version gets fixed, at least it won't cause too much destruct.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: Adjacent rectangles not aligned
« Reply #6 on: January 19, 2026, 07:55:56 am »
I posted this before.
https://devblogs.microsoft.com/oldnewthing/20120326-00/?p=8003
Makes it hard to see any bug there, even if Windows specific.

The only thing is really that Windows as a widget set differs from other widget sets in its window handling and that it is by design, however inconvenient for Lazarus users.
[edit]
There are actually two posts, by Raymond Chen. Will add the second later, because this explains just a bit of it.
« Last Edit: January 19, 2026, 08:28:28 am by Thaddy »
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

jamie

  • Hero Member
  • *****
  • Posts: 7518
Re: Adjacent rectangles not aligned
« Reply #7 on: January 19, 2026, 06:21:09 pm »
I posted this before.
https://devblogs.microsoft.com/oldnewthing/20120326-00/?p=8003
Makes it hard to see any bug there, even if Windows specific.

The only thing is really that Windows as a widget set differs from other widget sets in its window handling and that it is by design, however inconvenient for Lazarus users.
[edit]
There are actually two posts, by Raymond Chen. Will add the second later, because this explains just a bit of it.

Nothing new for you.
Blind in one eye, cant see out the other.

Jamie
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018