Code: Select all
Procedure DrawDashedLineXY(x1.i, y1.i, x2.i, y2.i, col1.i, col2.i, Dash_Length.i, Offset.i = 0)
; Dash_Length must be 1 or greater
; Offset can be 0 to (2 * Dash_Length) - 1
Protected Steep.i, DeltaX.i, DeltaY.i, YStep.i, XStep.i, Error.i
Protected x.i, y.i, cc.i, cs.i, c.i
Protected Max_Offset.i
If Abs(y2 - y1) > Abs(x2 - x1);
steep =#True
Swap x1, y1
Swap x2, y2
EndIf
If x1 > x2
Swap x1, x2
Swap y1, y2
EndIf
DeltaX = x2 - x1
DeltaY = Abs(y2 - y1)
Error = DeltaX / 2
y = y1
If y1 < y2
YStep = 1
Else
YStep = -1
EndIf
If Dash_Length < 1 : Dash_Length = 1 : EndIf
Max_Offset = Dash_Length * 2 - 1
If Offset > Max_Offset : Offset = Max_Offset: EndIf
If Offset < 0 : Offset = 0 : EndIf
cc = Offset ; colour counter
If cc > Dash_Length - 1 : cs = 1 : Else : cs = 0 : EndIf
For x = x1 To x2
If cs = 0 : c = col1 : Else : c = col2 : EndIf
If Steep
Plot(y, x, c)
Else
Plot(x, y, c)
EndIf
Error = Error - DeltaY
If Error < 0
y = y + YStep
Error = Error + DeltaX
EndIf
cc = cc + 1
If cc = Dash_Length
cs = 1 - cs
EndIf
If cc = Dash_Length * 2
cc = 0
cs = 1 - cs
EndIf
Next
ProcedureReturn cc ; return the offset so drawing can continue
EndProcedure
Define Event.i, Quit.i, Offset.i
OpenWindow(0,300, 300, 640, 480, "Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
CreateImage(0, 640, 480, 32, RGB(150, 150, 150))
CreateImage(1, 640, 480, 32, RGB(150, 150, 150))
Offset = 0
Repeat
StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_AllChannels)
DrawDashedLineXY(100, 100, 300, 100, RGBA(0,0,0,255), RGBA(0,0,0,0), 8, Offset)
DrawDashedLineXY(100, 110, 300, 110, RGBA(0,0,0,255), RGBA(255,255,255,255), 8, 0)
DrawDashedLineXY(100, 120, 300, 120, RGBA(0,0,0,255), RGBA(0,0,0,0), 8, 4)
DrawDashedLineXY(100, 130, 300, 130, RGBA(0,0,0,255), RGBA(0,0,0,0), 8, 13)
DrawDashedLineXY(100, 150, 300, 400, RGBA(0,0,0,255), RGBA(0,0,0,0), 4)
DrawDashedLineXY(500, 100, 500, 400, RGBA(0,0,0,255), RGBA(0,0,0,0), 8, Offset)
LineXY(100, 160, 300, 160, RGBA(0,0,0,0))
StopDrawing()
StartDrawing(WindowOutput(0))
DrawingMode(#PB_2DDrawing_AllChannels)
DrawImage(ImageID(1), 0, 0)
DrawAlphaImage(ImageID(0), 0, 0)
StopDrawing()
Delay(100)
Offset = Offset + 1
If Offset = 16 : Offset = 0 : EndIf
Repeat
Event = WaitWindowEvent(10)
Select Event
Case #PB_Event_CloseWindow
Quit = 1
EndSelect
Until Event = 0
Until Quit = 1