QB64 Phoenix Edition
"Slower" Line Drawing Example - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7)
+---- Thread: "Slower" Line Drawing Example (/showthread.php?tid=1683)



"Slower" Line Drawing Example - James D Jarvis - 05-13-2023

An example program for drawing lines "slower" for illustrative purposes. This was something someone asked about in a facebook group and I'm sharing the code here.

Code: (Select All)
'Example of Drawing lines slowly using qb64
Screen _NewImage(800, 500, 32)
Print "Slowly Drawing 4 lines"
For n = 1 To 700 Step 10
    fatline 1, 100, n, 100, 3, _RGB32(200, 200, 200)
    angle_line 1, 100, n, 3, 3, _RGB32(200, 100, 100)
    angle_line 1, 100, n, 13, 3, _RGB32(100, 100, 200)
    angle_line 1, 100, n, 33, 3, _RGB32(100, 200, 100)
    _Delay 0.04
    _Display 'eliminates very minor flicker, not strictly needed
Next n
Sub angle_line (x, y, Lnth, ang, thk, klr As _Unsigned Long)
    'draw a line from x,y lnth units long (from center of line) at angle ang of radial thickness thk in color klr
    ox = x: oy = y
    nx = ox + Lnth * Cos(0.01745329 * ang)
    ny = oy + Lnth * Sin(0.01745329 * ang)
    fatline ox, oy, nx, ny, thk, klr
End Sub
Sub fcirc (CX As Long, CY As Long, R As Long, klr As _Unsigned Long)
    'draw a filled circle with the quickest routine in qb64, not my development
    Dim subRadius As Long, RadiusError As Long
    Dim X As Long, Y As Long
    subRadius = Abs(R)
    RadiusError = -subRadius
    X = subRadius
    Y = 0
    If subRadius = 0 Then PSet (CX, CY): Exit Sub
    Line (CX - X, CY)-(CX + X, CY), klr, BF
    While X > Y
        RadiusError = RadiusError + Y * 2 + 1
        If RadiusError >= 0 Then
            If X <> Y + 1 Then
                Line (CX - Y, CY - X)-(CX + Y, CY - X), klr, BF
                Line (CX - Y, CY + X)-(CX + Y, CY + X), klr, BF
            End If
            X = X - 1
            RadiusError = RadiusError - X * 2
        End If
        Y = Y + 1
        Line (CX - X, CY - Y)-(CX + X, CY - Y), klr, BF
        Line (CX - X, CY + Y)-(CX + X, CY + Y), klr, BF
    Wend
End Sub

Sub fatline (x0, y0, x1, y1, r, klr As _Unsigned Long)
    'draw a line with dots with a radial thickness of r    from x0,y0 to x1,y1 in color klr
    If Abs(y1 - y0) < Abs(x1 - x0) Then
        If x0 > x1 Then
            lineLow x1, y1, x0, y0, r, klr
        Else
            lineLow x0, y0, x1, y1, r, klr
        End If
    Else
        If y0 > y1 Then
            lineHigh x1, y1, x0, y0, r, klr
        Else
            lineHigh x0, y0, x1, y1, r, klr
        End If
    End If
End Sub
Sub lineLow (x0, y0, x1, y1, r, klr As _Unsigned Long)
    dx = x1 - x0
    dy = y1 - y0
    yi = 1
    If dy < 0 Then
        yi = -1
        dy = -dy
    End If
    d = (dy + dy) - dx
    y = y0
    For x = x0 To x1
        fcirc x, y, r, klr
        If d > 0 Then
            y = y + yi
            d = d + ((dy - dx) + (dy - dx))
        Else
            d = d + dy + dy
        End If
    Next x
End Sub
Sub lineHigh (x0, y0, x1, y1, r, klr As _Unsigned Long)
    dx = x1 - x0
    dy = y1 - y0
    xi = 1
    If dx < 0 Then
        xi = -1
        dx = -dx
    End If
    D = (dx + dx) - dy
    x = x0
    For y = y0 To y1
        fcirc x, y, r, klr
        If D > 0 Then
            x = x + xi
            D = D + ((dx - dy) + (dx - dy))
        Else
            D = D + dx + dx
        End If
    Next y
End Sub



RE: "Slower" Line Drawing Example - mnrvovrfc - 05-13-2023

LOL!

This reminds me of a book I borrowed from the library while I was a teenager, for an 8-bit computer and therefore the author had to do everything in "text" mode -- including graphics. So he presented a line-drawing routine very much like the "linelow" subprogram listed above.


RE: "Slower" Line Drawing Example - James D Jarvis - 05-13-2023

Pretty standard algorithms for line drawing on a computer. The linelow and linehigh are in there because you have to  account for the impact of quadrant shifts on the slope of a line. If you just use built in line statements you just don't see it.  The poster asking the question about how to slow down line-drawing to a segment or even pixel by pixel and had no idea at all how it could be done. The algorithms will work fine and dandy in text mode too. Not everyone is coming to QB64 with the same depth of computing and graphics knowledge. I used to program my own filters for Photoshop, Freehand, and Illustrator with postscript and C/C++ before they were as feature rich as they are today but  would often pop a simple algorithm into basic  (usually powerbasic at the time because it had pointer support) before moving on to the serious code.