Line command with variable thickness

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Line command with variable thickness

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by tinman.

Sure, it's a bit messy but it does the job (or looks like it does) :)

Code: Select all

Procedure ClipLine(x1, y1, x2, y2, thickness)
  w = x2 - x1
  h = y2 - y1
  If w <> h 
    w = w + 1 
  Else 
    h = h + 1 
    If w = h 
      w = w + 1 
    EndIf
  EndIf
  
  extra = 0
  For i = 1 To thickness
    If w > h 
      y1 = y1 + extra 
    Else
      x1 = x1 + extra
    EndIf
    Line(x1, y1, w, h)
    If extra < 0 
      extra = extra - 1 
    Else 
      extra = extra + 1 
    EndIf
    extra = -extra
  Next
EndProcedure

If OpenWindow(0, 0, 0, 250, 200, "Variable Line Thickness",
              #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If CreateImage(0, 250, 200) And StartDrawing(ImageOutput(0))
      FrontColor(RGB(255, 0, 0))
      ClipLine(10, 10, 240, 190, 6)
      FrontColor(RGB(255, 255, 0))
      ClipLine(50, 10, 240, 150, 3)
    StopDrawing() 
    ImageGadget(0, 0, 0, 200, 200, ImageID(0))
  EndIf
  While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend 
EndIf
--
It's not minimalist - I'm increasing efficiency by reducing input effort.