Line drawing overflow?

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Line drawing overflow?

Post by Joubarbe »

Code: Select all

OpenWindow(0, 0, 0, 250, 250, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CanvasGadget(0, 0, 0, 250, 250)

StartDrawing(CanvasOutput(0))
DrawingMode(#PB_2DDrawing_Outlined)
Box(10, 10, 200, 200, #Black)
DrawingMode(#PB_2DDrawing_Default)
LineXY(10, 100, 210, 100, #Black)
Line(10, 150, 200, 1, #Black)
StopDrawing()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
I don't quite understand why LineXY (the first one at the top) is overflowing of 1 pixel.
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Line drawing overflow?

Post by Demivec »

Joubarbe wrote: Thu Apr 10, 2025 10:52 amI don't quite understand why LineXY (the first one at the top) is overflowing of 1 pixel.
I didn't run your code but I have an explanation.

Box() uses width and height. Box(10, 10, 200, 200, #Black)
Box width of 200 covers pixels from its left at 10 to its right at 209 (i.e 10 + 200 -1).
Box height of 200 covers pixels from its top at 10 to its bottom at 209 (i.e 10 + 200 -1).

The LineXY() uses coordinates. LineXY(10, 100, 210, 100, #Black)
The line starts at (10, 100) and continues through (210, 100).
The line's rightmost coordinate is 210 while the box's is 209.

Line() also used width and height parameters. Line(10, 150, 200, 1, #Black)
This line starts at (10, 150) and continues through (209, 150) which is calculated as (10 + 200 -1, 150 + 1 - 1).
This line lines up with the box's dimensions.



The key take away is to remember that the width and height parameters include the starting point so you have to subtract that point to get the resulting coordinate. This also applies to other things that also use location and width and height parameters such as gadgets.
Post Reply