' Anti-Aliased Line Plotter,copyright1999 Robert Covington
'This FN plots Anti-Aliased looking lines. Similar to Wu lines, but using a
'different approach. At least it is one I came up with on my own. It plots the X
'and Y values by computing them from the actual line and uses any integer error distance to
'set the pixel's color. Then it plots a secondary line right next to the first using inverse
'colors which blend in with the original line.
' Something like that. :) Robert Covington or
'This may still contain bugs. "Use at your own risk" standard type disclaimer applies.
'Wu lines demo at Ex.: LONG COLOR 65535,col%,col%
'...for other colors. At least one RGB channel of each long color statement has to be "col%".
WINDOW OFF
CLEAR LOCAL
LOCAL FN ALine(x1!,y1!,x2!,y2!)
DIM x1!,x2!,y1!,y2!
DIM x!
DIM y!
DIM nuX!
DIM nuY!
DIM m!
IF FN BUTTON THEN END
LONG COLOR 0,0,0 'preset to Black for below cases...
'Special Slope Situations...
'Lines pass directly through slopes of 1,and 0
'(45 Degree,horizontal and vertical lines) - Check for these.
'If they exist, plot quickly (no calcs), then Exit Fn
IF x2!-x1!=0 THEN PLOT x1!,y1! TO x2!,y2!:EXIT FN
' ^ Avoid Divide by Zero-no X Change, horizontal line
m!=(y2!-y1!)/(x2!-x1!) ' Get slope, rise over run
IF m!=0 THEN PLOT x1!,y1! TO x2!,y2!:EXIT FN ' Zero slope, no X change, vertical line
LONG IF m!=1 OR m!=-1 ' diagonal line
PLOT x1!,y1! TO x2!,y2!:EXIT FN
END IF
LONG IF ABS(m!)<1 ' Different cases of slope
IF x1!>x2! THEN SWAP x2!,x1!:SWAP y2!,y1! ' else we get no workee situation.
FOR x!=x1! TO x2! 'solving for Y, so x1 to x2
nuY!=(x!-x1!)*m!+y1! ' get new Y value from slope and other x info
col%=FRAC(nuY!)*65535 ' Error of Y from actual line as color setter
LONG COLOR col%,col%,col% 'grayscale
PLOT x!+100,nuY!+100 'translate more to center.
col%=65535-col% 'invert parallel color
LONG COLOR col%,col%,col% 'grayscale
PLOT x!+100,nuY!+101 'translate more to center, 1 extra for parallel line.
NEXT x!
XELSE
IF y1!>y2! THEN SWAP y2!,y1!:SWAP x2!,x1! ' as above, no workee situation otherwise.
FOR y!=y1! TO y2! 'We are solving for X, so from y1 to y2
nuX!=((y!-y1!)/m!)+x1! ' get new X value from slope and other y info
col%=FRAC(nuX!)*65535 ' Error of X from actual line as color setter
LONG COLOR col%,col%,col% 'grayscale
PLOT nuX!+100,y!+100 'translate more to center
col%=65535-col% 'invert parallel color
LONG COLOR col%,col%,col% 'grayscale
PLOT nuX!+101,y!+100 'translate more to center, 1 extra for parallel line.
NEXT y!
END IF
END FN
'========MAIN=======
DIM start
DIM time
WINDOW 1,"Anti-Aliased Line Tester",(0,0)-(400,400),_docNoGrow
start%=FN TICKCOUNT 'start time for speed test
FOR j= 1 TO 100 ' draw 100 lines from various points
' Set coordinates for testing, Randomly.
x1=RND(200)
x2=RND(200)
y1=RND(200)
y2=RND(200)
FN ALine(x1,y1,x2,y2) ' call the FN
NEXT j
time=FN TICKCOUNT-start 'end time for test
COLOR _zBlack ' set back to printable color, might be near white from pixel plotting
CALL MOVETO (20,20) ' set pen placement
PRINT time%;" ticks" ' How long...220-260 ticks on 240 Mhz 603e in FB II,OS 8.1
PRINT "Mouse or any key to exit..."
DO
UNTIL FN BUTTON OR LEN(INKEY$)