[PB 6.20] StrokePath() cannot draw a 1px width line?

Just starting out? Need help? Post your questions and find answers here.
osg
New User
New User
Posts: 3
Joined: Wed Oct 27, 2010 11:11 am

[PB 6.20] StrokePath() cannot draw a 1px width line?

Post by osg »

Subj

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 400, 200)
    SetGadgetColor(0, #PB_Gadget_BackColor, #Black)
    
    If StartVectorDrawing(CanvasVectorOutput(0))
        
        MovePathCursor(0,10)
        AddPathLine(68, 0, #PB_Path_Relative)
        VectorSourceColor(RGBA(146, 146, 146, 255))
        StrokePath(1)
        
        MovePathCursor(0,20)
        AddPathLine(68, 0, #PB_Path_Relative)
        VectorSourceColor(RGBA(146, 146, 146, 255))
        StrokePath(2)

        StopVectorDrawing()
    EndIf
    
    Repeat
        Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
EndIf
The width of the first line must be 1 pixel but it is 2px and kind of blurry.
Image
User avatar
STARGÅTE
Addict
Addict
Posts: 2228
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: [PB 6.20] StrokePath() cannot draw a 1px width line?

Post by STARGÅTE »

The coordinate 0,10 is exactly between two pixels, on the edge.
You have to draw at at 0.5, 10.5 in the middle of a pixel.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
osg
New User
New User
Posts: 3
Joined: Wed Oct 27, 2010 11:11 am

Re: [PB 6.20] StrokePath() cannot draw a 1px width line?

Post by osg »

STARGÅTE wrote: Sun Jun 08, 2025 6:04 pm The coordinate 0,10 is exactly between two pixels, on the edge.
You have to draw at at 0.5, 10.5 in the middle of a pixel.
Thank you. MovePathCursor(0.5,10.5) (or MovePathCursor(0,10.5)) helps a little, but it makes the line longer and blurry at the ends. This is all wrong, I think (0, 10) must define the pixel where the line starts.

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 400, 200)
    SetGadgetColor(0, #PB_Gadget_BackColor, #Black)
    
    If StartVectorDrawing(CanvasVectorOutput(0))   
    
        MovePathCursor(0.5,10.5)
        AddPathLine(68, 0, #PB_Path_Relative)
        VectorSourceColor(RGBA(146, 146, 146, 255))
        StrokePath(1)
        
        MovePathCursor(0,20)
        AddPathLine(68, 0, #PB_Path_Relative)
        VectorSourceColor(RGBA(146, 146, 146, 255))
        StrokePath(2)
        
        StopVectorDrawing()
    EndIf    
    
    Repeat
        Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
EndIf
Image
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [PB 6.20] StrokePath() cannot draw a 1px width line?

Post by infratec »

I think you think wrong.

A line in vectorgraphic has always a center of 0 width and the stroke width is around that on all sides.
You can see this, for example, in inkscape if you choose the line end.

If you want the same length with stroke 2 and stroke 1 you need:

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 0, 0, 400, 200)
  SetGadgetColor(0, #PB_Gadget_BackColor, #Black)
  
  If StartVectorDrawing(CanvasVectorOutput(0))   
    
    MovePathCursor(1.5, 10.5)
    AddPathLine(68, 0, #PB_Path_Relative)         ; 1.5 - 0.5 to 1.5 + 68.0 + 0.5 -> 1 to 70
    VectorSourceColor(RGBA(146, 146, 146, 255))
    StrokePath(1, #PB_Path_SquareEnd)
    
    MovePathCursor(2, 20)
    AddPathLine(67, 0, #PB_Path_Relative)         ; 2.0 - 1.0 to 2.0 + 67.0 + 1.0 -> 1 to 70
    VectorSourceColor(RGBA(146, 146, 146, 255))
    StrokePath(2, #PB_Path_SquareEnd)
    
    StopVectorDrawing()
  EndIf    
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
I placed it a bit right, because with a start at 0 you don't see that the stroke with 2 goes into the negative area.
I also use #PB_Path_SquareEnd that the line ending is 'plain'.

Both lines should go now from 1 to 70.
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [PB 6.20] StrokePath() cannot draw a 1px width line?

Post by infratec »

With #PB_Path_Default, you can use this:

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 0, 0, 400, 200)
  SetGadgetColor(0, #PB_Gadget_BackColor, #Black)
  
  If StartVectorDrawing(CanvasVectorOutput(0))   
    
    MovePathCursor(1.5, 10.5)
    AddPathLine(68, 0, #PB_Path_Relative)         
    VectorSourceColor(RGBA(146, 146, 146, 255))
    StrokePath(1)
    
    MovePathCursor(1, 20)
    AddPathLine(68, 0, #PB_Path_Relative)         
    VectorSourceColor(RGBA(146, 146, 146, 255))
    StrokePath(2)
    
    StopVectorDrawing()
  EndIf    
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
Because then the line ends directly.

Problems occures when you want to make a vertical and horizontal line with different thickness and they should end at the same pixel.
Then you have to think about the coordinates.
osg
New User
New User
Posts: 3
Joined: Wed Oct 27, 2010 11:11 am

Re: [PB 6.20] StrokePath() cannot draw a 1px width line?

Post by osg »

Jesus... I didn't know that something could start between the pixels, thank you guys.
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [PB 6.20] StrokePath() cannot draw a 1px width line?

Post by infratec »

Vectorlines always use an imaginary middle line of width 0.
The stroke width is arround this imaginary line.
So if you start at 0 with a stroke width of 1 the line is drawing at -0.5 to 0.5
That's the reason why all vector stuff uses floating point for the values.
In general and not only PB :wink:

Vector drawing is not always as simple as it sounds.
Post Reply