AntiAliasing Drawling line
Posted: Sat Dec 29, 2007 10:49 am
Here is a code to make an Anti-Aliasing Line, i found the original code on blitz forum

Code: Select all
; Anti-aliased lines demo & routine.
; flaith 28/12/07
;
; Blitz Basic version by By Mike Keith, Feb 2002
; http://www.blitzbasic.com/codearcs/codearcs.php?code=227
InitSprite() : InitKeyboard()
#Screen_width = 800
#Screen_height = 600
#depth = 32
Global Dim AlineTable.l(64)
;==================================================
Declare DrawAALine(x1.l, y1.l, x2.l, y2.l, cr.l, cg.l, cb.l)
Declare DrawAACircle(x.l, y.l, r.l, cr.l, cg.l, cb.l)
Declare MergePixel(x,y,r,g,b,w)
Declare InitAlineTable()
;==================================================
If OpenWindow(0, 0, 0, #Screen_width, #Screen_height, "")
If OpenWindowedScreen(WindowID(0), 0, 0, #Screen_width, #Screen_height, 0, 0, 0)
EndIf
EndIf
InitAlineTable()
Repeat
ExamineKeyboard()
StartDrawing(ScreenOutput())
Repeat
Event = WindowEvent()
Select Event
Case #PB_Event_Gadget
If EventGadget() = 0
quit = 1
EndIf
Case #PB_Event_CloseWindow
quit = 1
EndSelect
Until Event = 0
If KeyboardPushed(#PB_Key_Escape) : quit = 1 : EndIf
For y=0 To #Screen_height-1
z = y*255/(#Screen_height-1)
LineXY(0,y,#Screen_width-1,y,RGB(z/2,z/3,0))
Next
t1 = ElapsedMilliseconds()
For i=0 To 360 Step 5
dx = 150*Sin(i)
dy = 150*Cos(i)
LineXY(160+dx,300+dy,160-dx,300-dy,RGB(255,255,255))
Next
t2 = ElapsedMilliseconds()
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(50,535,"Regular lines")
DrawText(50,550,"Took "+Str(t2-t1)+" ms")
If 1
t3 = ElapsedMilliseconds()
For i=0 To 360 Step 5
dx = 150*Sin(i)
dy = 150*Cos(i)
DrawAALine(600+dx,300+dy,600-dx,300-dy,255,255,255)
Next
t4 = ElapsedMilliseconds()
EndIf
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(500,535,"Anti-aliased lines")
DrawText(500,550,"Took "+Str(t4-t3)+" ms")
DrawAACircle(#Screen_width/2,#Screen_height/2, 130, 192, 0, 0)
StopDrawing()
FlipBuffers()
Until quit = 1
End
;=========================================================
Procedure DrawAACircle(x.l, y.l, r.l, cr.l, cg.l, cb.l)
Protected x1.l, y1.l, x2.l, y2.l
For theta= 0 To 359 Step 4
x1 = r*Cos(theta*#PI/180)+x
y1 = -r*Sin(theta*#PI/180)+y
x2 = r*Cos((theta+4)*#PI/180)+x
y2 = -r*Sin((theta+4)*#PI/180)+y
DrawAALine(x1, y1, x2, y2, cr, cg, cb)
Next
EndProcedure
;=========================================================
Procedure DrawAALine(x1.l, y1.l, x2.l, y2.l, r.l, g.l, b.l)
xd = x2-x1
yd = y2-y1
If (xd = 0 Or yd = 0)
LineXY(x1,y1,x2,y2,RGB(r,g,b))
ProcedureReturn
EndIf
Plot(x1,y1,RGB(r,g,b))
Plot(x2,y2,RGB(r,g,b))
If (Abs(xd) > Abs(yd))
If (x1 > x2)
tmp = x1: x1 = x2: x2 = tmp
tmp = y1: y1 = y2: y2 = tmp
xd = x2-x1
yd = y2-y1
EndIf
grad = yd*65536/xd
yf = y1*65536
For x=x1+1 To x2-1
yf = yf + grad
w = (yf >> 10) & $3f
y = yf >> 16
MergePixel(x,y,r,g,b,63-w)
MergePixel(x,y+1,r,g,b,w)
Next
Else
If (y1 > y2)
tmp = x1: x1 = x2: x2 = tmp
tmp = y1: y1 = y2: y2 = tmp
xd = x2-x1
yd = y2-y1
EndIf
grad = xd*65536/yd
xf = x1*65536
For y=y1+1 To y2-1
xf = xf + grad
w = (xf >> 10) & $3f
x = xf >> 16
MergePixel(x,y,r,g,b,63-w)
MergePixel(x+1,y,r,g,b,w)
Next
EndIf
EndProcedure
;--------------------------------------------------------------------------
Procedure MergePixel(x.l,y.l,r.l,g.l,b.l,w.l)
Protected wf.l, pix.l, ro.l, go.l, bo.l, rnew.l, gnew.l, bnew.l
wf = AlineTable(w)
pix = Point(x,y)
ro = Red(pix)
go = Green(pix)
bo = Blue(pix)
rnew = ro + ((wf*(r-ro)) >> 8)
gnew = go + ((wf*(g-go)) >> 8)
bnew = bo + ((wf*(b-bo)) >> 8)
Plot(x,y,RGB(rnew,gnew,bnew))
EndProcedure
;--------------------------------------------------------------------------
Procedure InitAlineTable()
Protected i.l
For i=0 To 63
ALineTable(i) = (Sqr(4*i)*16)*0.4 + (4*i)*0.6
Next
EndProcedure