I'm trying to draw lines like you can in Paint and other image editors. The example is setup to be like 200% zoom, but the lines are far too smooth - they should be pixelated like if the image were doubled in size. Does anyone know how I can make it behave like that?
Code: Select all
Global mX, mY, oX, oY, iX, iY, lineSize, lineImg
lineSize = 2
lineImg = CreateImage(#PB_Any, lineSize, lineSize, 24, RGB(0, 0, 0))
Procedure DrawLine(imgID, x0, y0, x1, y1)
; https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#PureBasic
Protected steep, error, dX, dY, x, y, xStep, yStep, plotX, plotY
If Abs(y1 - y0) > Abs(x1 - x0)
steep = #True
Swap x0, y0
Swap x1, y1
EndIf
If x0 > x1
Swap x0, x1
Swap y0, y1
EndIf
dX = x1 - x0
dY = Abs(y1 - y0)
error = dX / 2
y = y0
If y0 < y1
yStep = 1
Else
yStep = -1
EndIf
For x = x0 To x1
If steep
plotY = x / lineSize : plotX = y / lineSize
Else
plotX = x / lineSize : plotY = y / lineSize
EndIf
DrawAlphaImage(imgID, plotX, plotY)
error - dY
If error < 0
y + yStep
error + dX
EndIf
Next
EndProcedure
Procedure OnLeftButtonDown()
iX = GetGadgetAttribute(0, #PB_Canvas_MouseX) * lineSize
iY = GetGadgetAttribute(0, #PB_Canvas_MouseY) * lineSize
EndProcedure
Procedure OnMouseMove()
mX = GetGadgetAttribute(0, #PB_Canvas_MouseX) * lineSize
mY = GetGadgetAttribute(0, #PB_Canvas_MouseY) * lineSize
If GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton
oX = iX
oY = iY
iX = mX / lineSize * lineSize
iY = mY / lineSize * lineSize
If StartDrawing(CanvasOutput(0))
DrawLine(ImageID(lineImg), oX, oY, iX, iY)
StopDrawing()
EndIf
EndIf
EndProcedure
OpenWindow(0, 0, 0, 500, 400, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0, 0, 0, 500, 500)
BindGadgetEvent(0, @OnLeftButtonDown(), #PB_EventType_LeftButtonDown)
BindGadgetEvent(0, @OnMouseMove(), #PB_EventType_MouseMove)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow