I am a beginner to Purebasic, just converting to Purebasic from 3 other dialects of Basic, and was surprised that LineXY has no thickness setting (two of the other Basic dialects did have) but will be doing 95% of my PC programming with Purebasic , so have had to roll my own with a workaround. My code could probably be improved and optimized a lot, but as mentioned I am still a beginner with Purebasic. See code below. I would also prefer a native LineXY with thickness setting.
My code below makes use of the Bresenham line drawing Algorithm, which is very quick, as it ONLY uses integer ADDITION and SUBTRACTION, no multiplication, division or sines or cosines. There is a lot on the web about the Bresenham Line Drawing Algorithm. There are other later algorithms almost as fast that also do antialiasing. I do not understand the line drawing algorithm, but it works. For explanation you will need to look up an explanation on the web.
Essentially the Bresenham line drawing algorithm "thickLineXY(start_x,start_y,end_x,end_y,thickness,color)" is called to calculate the pixels on the line, and instead of the procedure calling plot() to turn each pixel on, it calls circle() to draw a circle at the pixel location using the thickness of the required line as the diameter. This results in a lot of circles being drawn, so I feel that a native LineXY with thickness setting would speed this up.
Hope this helps.
Code: Select all
;thickLineXY(startx,starty,endx,endy,thickness,color)
;------------------------------------
;Bresenhams Line Drawing Algorithm
;The Bresenham line drawing algorithm uses
; integer addition And subtraction ONLY
; NO multiplication, NO division, NO sines or cosines
;------------------------------------
Procedure thickLineXY (start_x,start_y,end_x,end_y,thickness,whatcolor)
err_x = 0 : err_y = 0 : inc_x = 0 : inc_y = 0
delta_x = end_x - start_x;
delta_y = end_y - start_y;
If(delta_x > 0) :inc_x = 1:ElseIf (delta_x = 0) :inc_x = 0: Else :inc_x = -1:EndIf
If(delta_y > 0) :inc_y = 1:ElseIf (delta_y = 0) :inc_y = 0: Else :inc_y = -1:EndIf
delta_x = Abs(delta_x);
delta_y = Abs(delta_y);
If(delta_x > delta_y)
distance = delta_x;
Else
distance = delta_y;
EndIf
For xyz = 0 To distance+1 Step 1
; modified to place a circle at the pixel location to get a thick line
;Plot(start_x,start_y,whatcolor)
Circle(start_x,start_y,thickness,whatcolor)
err_x = err_x + delta_x
err_y = err_y + delta_y
If (err_x > distance)
err_x = err_x - distance
start_x = start_x + inc_x
EndIf
If (err_y > distance)
err_y = err_y - distance
start_y = start_y +inc_y
EndIf
Next
EndProcedure
;-----------------------------------------------------
; End of Bresenhams algorithm
;-----------------------------------------------------
;----------------------------------------------------------
; modified 2DDrawing Example to draw a multihand clock face
;----------------------------------------------------------
If OpenWindow(0, 0, 0, 800, 800, "2DDrawing Thick Lines", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_2DDrawing_Gradient)
If CreateImage(0, 800, 800) And StartDrawing(ImageOutput(0))
thickLineXY( 40, 40, 760, 40, 10, RGB(255,1,1)) ;outer red box top
thickLineXY(760, 40, 760, 760, 10, RGB(255,1,1));outer red box right side
thickLineXY(760, 760, 40, 760, 10, RGB(255,1,1));outer red box bottom
thickLineXY( 40, 760, 40, 40, 10, RGB(255,1,1));outer red box left side
thickLineXY(100, 680, 700, 680, 50, RGB(255,100,1)) ;orange base
thickLineXY(400, 500, 100,100, 1, RGB(255,1,1)) ; red hand
thickLineXY(100, 100, 100,100, 10, RGB(255,1,1)) ; red hand blob
thickLineXY(400, 500, 300, 100, 5, RGB(1,255,1)) ; green hand
thickLineXY(400, 500, 600, 100,10, RGB(1,1,255)) ; blue hand
thickLineXY(400, 500, 600, 600,15,RGB(1,255,255)) ; teal hand
thickLineXY(400, 500, 100, 600,20,RGB(255,255,1)) ; yellow hand
thickLineXY(400, 500, 100, 600,5,RGB(5,25,25)) ; yellow hand
thickLineXY(100, 100, 400, 500, 1, RGB(255,1,1)) ; red hand
thickLineXY(300, 100, 400, 500, 5, RGB(1,255,1)) ; green hand
thickLineXY(600, 100, 400, 500, 10, RGB(1,1,255)) ; blue hand
thickLineXY(400, 500, 400, 500, 36, RGB(255,255,255)) ; white dot
thickLineXY(400, 500, 400, 500, 22, RGB(255,0,255)) ; pink dot
StopDrawing()
ImageGadget(0, 0, 0, 200, 200, ImageID(0))
EndIf
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
End
;-------------------------------------------------------
;============================================================================