PlotLine
To run the demo: Under SMSQ/E type:
EX <path>PlotLine_bas.
Under Qdos type:
AUTO 100: LOAD flp1_PlotLine_bas<ENTER> <BREAK>
and then type:
RUN: PAUSE.
Note: In mode 0/4 you wont see the horizontal line as its black on black, so just alter the
colour codes to taste.
rem + ------------------------------------------------------------------------ +
rem |< Plot a Line >|
rem + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
rem | Draw a straight line using only integer math and using pixel coordinates |
rem | |
rem | Note: Unlike S*BASIC LINE, if given coordinates are outside the screen's |
rem | range the routine errors. So you either have to check this before call- |
rem | ing or insert some error trapping code of your own. (Which I did here.) |
rem + ------------------------------------------------------------------------ +
rem | V0.01, pjw, 2022 Dec 23 |
rem + ------------------------------------------------------------------------ +
:
:
rem Harness
cw = FOPEN("con_512x256a0x0"): CLS#cw
:
limX% = 510: rem Max x
limY% = 254: rem Max y
:
plotLine#cw; 0, 250, 510, 0, 6
plotLine#cw; 0, 0, 510, 254, 2
plotLine#cw; 0, 127, 510, 127, 1
plotLine#cw; 254, 0, 254, 254, 7
PAUSE#cw: CLOSE#cw
:
:
rem + ------------------------------------------------------------------------ +
rem |< plotLine >|
rem + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
rem | Draw a straight line using only integer math |
rem | |
rem | Bresenham's line algorithm. From Wikipedia, 2019 Jul 14 |
rem | |
rem | By switching the x and y axis an implementation for positive or |
rem | negative steep gradients can be written. |
rem | The complete solution detects whether x1 > x0 or y1 > y0 and reverses |
rem | the input coordinates before drawing. |
rem | |
rem | plotLine is the calling procedure, plLo and plHi are dependent |
rem | subroutines; they have no life of their own! |
rem | |
rem | For SMSQ/E all variables could be changed to integer! |
rem | |
rem | Dependencies: OutR and GLOBal limX%, limY% |
rem + ------------------------------------------------------------------------ +
rem | V0.00, pjw, 2019 Jul 14 |
rem | V0.01, pjw, 2022 Dec 23 |
rem + ------------------------------------------------------------------------ +
:
DEFine PROCedure plotLine(ch, x0,y0, x1,y1, c)
LOCal D, dx, dy
IF OutR(x0, limX%) OR OutR(x1, limX%) OR OutR(y0, limY%) OR OutR(y1, limY%): RETurn
IF x0 = x1 OR y0 = y1
IF x0 > x1: dx = x0: ELSE : dx = x1
IF x0 = x1
IF y1 > y0: dy = y0: ELSE : dy = y1
IF OutR(ABS(y1 - y0) + dy, limY%): RETurn
BLOCK#ch; 1, ABS(y1 - y0), x1, dy, c
ELSE
IF x1 > x0: dx = x0: ELSE : dx = x1
IF OutR(ABS(x1 - x0) + dx, limX%): RETurn
BLOCK#ch; ABS(x1 - x0), 1, dx, y1, c
END IF
RETurn
END IF
:
IF ABS(y1 - y0) < ABS(x1 - x0)
IF x0 > x1
plLo x1, y1, x0, y0
ELSE
plLo x0, y0, x1, y1
END IF
ELSE
IF y0 > y1
plHi x1, y1, x0, y0
ELSE
plHi x0, y0, x1, y1
END IF
END IF
END DEFine plotLine
:
DEFine PROCedure plLo(x0,y0, x1,y1)
LOCal x, y, yi
dx = x1 - x0
dy = y1 - y0
yi = 1
IF dy < 0
yi = -1
dy = -dy
END IF
D = dy + dy - dx
y = y0
:
FOR x = x0 TO x1
BLOCK#ch; 1, 1, x, y, c
IF D > 0
y = y + yi
D = D - dx - dx
END IF
D = D + dy + dy
END FOR x
END DEFine plLo
:
DEFine PROCedure plHi(x0,y0, x1,y1)
LOCal y, x, xi
dx = x1 - x0
dy = y1 - y0
xi = 1
IF dx < 0
xi = -1
dx = -dx
END IF
D = dx + dx - dy
x = x0
:
FOR y = y0 TO y1
BLOCK#ch; 1, 1, x, y, c
IF D > 0
x = x + xi
D = D - dy - dy
END IF
D = D + dx + dx
END FOR y
END DEFine plHi
:
:
DEFine FuNction OutR(n, limN)
IF n < 0 OR n > limN: RETurn 1
RETurn 0
END DEFine OutR
:
:
Generated with sb2htm on 2023 Aug 27
©pjwitte 2oo1 - 2o22
QL Software
