## Antialias Line Drawing

vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

### Antialias Line Drawing

I converted over some code to do antialias line drawing. No reason other than bored.

Code: Select all

`/'Xiaolin Wu's line algorithmAn algorithm for line antialiasing, which was presented in the article An Efficient Antialiasing Technique in the July 1991 issue of Computer Graphics, as well as in the article Fast Antialiasing in the June 1992 issue of Dr. Dobb's Journal.'/'function plot(x, y, c) is'    plot the pixel at (x, y) with brightness c (where 0 = c = 1)'Type Pixel32   Red As UByte   Green As UByte   Blue As UByte   Alpha As UByteEnd Type'// integer part of xfunction ipart(x As Single) As Integer    return int(x)End Functionfunction round(x As Single) As Integer    return ipart(x + 0.5)End Function' fractional part of xfunction fpart(x As Single) As Single    if x < 0 Then return 1 - (x - Fix(x))    return x - fix(x)End Functionfunction rfpart(x As Single) As Single    return 1 - fpart(x)End FunctionSub Plot(x As Single, y As Single,baseclr As UInteger, c As Single)Dim As Pixel32 Ptr _base = Cast(Pixel32 Ptr, @baseclr)       _base->Red   = _base->Red   * c   _base->Green = _base->Green * c   _base->Blue  = _base->Blue  * c      PSet (x,y),baseclr      End Sub   sub DrawAALine(x0 As Single,y0 As Single,x1 As Single,y1 As Single, clr As UInteger) Dim As Integer steep = Abs(y1 - y0) > abs(x1 - x0)Dim As Single dx,dy,gradient,xend,yend,xgap,xpxl1,ypxl1,xpxl2,ypxl2,intery        if steep then        Swap x0, y0        Swap x1, y1    end If        if x0 > x1 then        Swap x0, x1        Swap y0, y1    end if        dx = x1 - x0    dy = y1 - y0    gradient = dy / dx        ' handle first endpoint    xend = round(x0)    yend = y0 + gradient * (xend - x0)    xgap = rfpart(x0 + 0.5)        xpxl1 = xend ' this will be used in the main loop    ypxl1 = ipart(yend)        if steep then        plot(ypxl1,   xpxl1, clr, rfpart(yend) * xgap)        plot(ypxl1+1, xpxl1, clr,  fpart(yend) * xgap)    else        plot(xpxl1, ypxl1  , clr, rfpart(yend) * xgap)        plot(xpxl1, ypxl1+1, clr,  fpart(yend) * xgap)    end if    intery = yend + gradient ' first y-intersection for the main loop        ' handle second endpoint    xend = round(x1)    yend = y1 + gradient * (xend - x1)    xgap = fpart(x1 + 0.5)        xpxl2 = xend 'this will be used in the main loop    ypxl2 = ipart(yend)        if steep then        plot(ypxl2  , xpxl2, clr, rfpart(yend) * xgap)        plot(ypxl2+1, xpxl2, clr,  fpart(yend) * xgap)    else        plot(xpxl2, ypxl2, clr,  rfpart(yend) * xgap)        plot(xpxl2, ypxl2+1, clr, fpart(yend) * xgap)    end if        ' Line loop    for x As Integer = xpxl1 + 1 to xpxl2 - 1       if steep then          plot(ipart(intery)  , x, clr, rfpart(intery))          plot(ipart(intery)+1, x, clr,  fpart(intery))      else          plot(x, ipart(intery), clr,  rfpart(intery))          plot(x, ipart(intery)+1, clr, fpart(intery))      end if      intery = intery + gradient    Next    End sub#Include "fbgfx.bi"Using fbScreenRes 800,600,32,,GFX_FULLSCREEN   DrawAALine (10,10,300,230, &hFFFFFF)   Line (10,20)-(300,240),&hFFFFFFSleep`

-Vince
dafhi
Posts: 1238
Joined: Jun 04, 2005 9:51

### Re: Antialias Line Drawing

these look surprisingly good. I did an animation test and everything holds up
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

### Re: Antialias Line Drawing

Nice!
Roland Chastain
Posts: 851
Joined: Nov 24, 2011 19:49
Location: Dakar, Senegal
Contact:

### Re: Antialias Line Drawing

There is another FreeBASIC implementation of the algorithm here:

http://rosettacode.org/wiki/Xiaolin_Wu' ... #FreeBASIC
Gunslinger
Posts: 12
Joined: Mar 08, 2016 19:10

### Re: Antialias Line Drawing

Thank you,

I tested this line and looks nice.
only when i draw a line on a red screen it will use black for Anti-aliasing.
So i modified the pset to add the color under it and i fount out pset sometimes draws on the same point multiple times
Making the line look to fat.

I will use it any way, just are little pixels on this screen
sancho2
Posts: 547
Joined: May 17, 2015 6:41

### Re: Antialias Line Drawing

Can you post your modifications? Or clarify what you are saying.
Pset'ing the same point is simply changing the same pixel. It can't make the line any fatter.
It also isn't happening in the code posted here.
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

### Re: Antialias Line Drawing

ooo nice routine! Don't understand your 'pset makes fatter' or plots multiple times though.... doesnt seem to do it here under the magnifier...
nimdays
Posts: 219
Joined: May 29, 2014 22:01
Location: ID

### Re: Antialias Line Drawing

I can't see it from xp(1.02), must press ctr+alt+del
How much memory for this ?
leopardpm
Posts: 1791
Joined: Feb 28, 2009 20:58

### Re: Antialias Line Drawing

nimdays wrote:I can't see it from xp(1.02), must press ctr+alt+del
How much memory for this ?

what do you mean? The routine is rather tiny and needs no other memory (no large arrays, etc) - you can tell by looking at it

it hangs your machine which is running xp? con't imagine why... do you have issues with other programs which have :

#Include "fbgfx.bi"

???
nimdays
Posts: 219
Joined: May 29, 2014 22:01
Location: ID

### Re: Antialias Line Drawing

I did it again with fbide ,sorry no hangs, i can press esc to close it but nothing to show up only fbtemp.exe.
I'll check some fbgfx code.