Hi,
I've wrote a small function for antialiazed line-drawing.
Here some examples:
1. AA-Lines with intersection-calculation (main-code from another thread)
rem General Screen-Settings
set display mode 640,480,32
sync on : sync rate 0
rem Program-Settings
Line_Start()
x = rnd(640)
y = rnd(480)
rem Main-Loop
do
rem clear screen
cls
rem Show lines
AA_line(50,50,300,200,rgb(255,255,255),255)
AA_line(x,y,mousex(),mousey(),rgb(255,255,255),255)
rem Reposition line
if mouseclick() then x = mousex() : y = mousey()
rem Calculation
mx = mousex()
my = mousey()
t = timer()
for run = 1 to 10000
col = LineIntersection(50,50,300,200, x,y,mx,my)
next run
t = timer()-t
rem Show Collision-Point
if col = 1 then ink rgb(255,0,0),0 else ink rgb(128,128,128),0
line Line_ColX()-5,Line_ColY()-5,Line_ColX()+5,Line_ColY()+5
line Line_ColX()-5,Line_ColY()+5,Line_ColX()+5,Line_ColY()-5
ink rgb(255,255,255),0
rem Show Information
print "Move mouse or click to change line-coordinates!"
print "Collision: ", col
print "Calculation-time: ", t, "/10,000 ms"
rem Update screen
sync
loop
rem This function should be called before the other 3 functions
function Line_Start()
global Line_Col_X as integer
global Line_Col_Y as integer
endfunction
rem This function returns the X-Coordinate of the last collision (so LineIntersection(...) has to be called before this function)
function Line_ColX()
endfunction Line_Col_X
rem This function returns the collisions Y-Coordinate
function Line_ColY()
endfunction Line_Col_Y
rem This function calculates
function LineIntersection(XS1,YS1,XE1,YE1, XS2,YS2,XE2,YE2)
rem Debugging: Vertical lines are not allowed!
if XS1 = XE1 then XE1 = XS1+1
if XS2 = XE2 then XE2 = XS2+1
rem First line
rem y=mx+b
rem Calculate m
xdif1 = (XE1-XS1)
ydif1 = (YE1-YS1)
m1# = ydif1/(1.0*xdif1)
rem Calculate b
b1# = YS1-XS1*m1#
rem Second line
rem Calculate m
xdif2 = (XE2-XS2)
ydif2 = (YE2-YS2)
m2# = ydif2/(1.0*xdif2)
rem Calculate b
b2# = YS2-XS2*m2#
rem Collision-Detection
rem m1#*x+b1# = m2#*x+b2# | -(m2#*x)-b1#
rem <=> (m1#-m2#)*x = b2#-b1# | /(m1#-m2#)
rem <=> x = (b2#-b1#)/(m1#-m2#)
rem Calculate X-Pos
x# = (b2#-b1#)/(m1#-m2#)
Line_Col_X = x#
rem Calculate Y-Pos
rem y=mx+b
rem y# = m1#*x#+b1#
y# = m1#*x#+b1#
Line_Col_Y = y#
rem Make sure that XS<XE, YS<YE
if XS1 > XE1 then XT1 = XS1 : XS1 = XE1 : XE1 = XT1` : YT1 = YS1 : YS1 = YE1 : YE1 = YT1
if YS1 > YE1 then YT1 = YS1 : YS1 = YE1 : YE1 = YT1`XT1 = XS1 : XS1 = XE1 : XE1 = XT1 : YT1 = YS1 : YS1 = YE1 : YE1 = YT1
if XS2 > XE2 then XT2 = XS2 : XS2 = XE2 : XE2 = XT2` : YT2 = YS2 : YS2 = YE2 : YE2 = YT2
if YS2 > YE2 then YT2 = YS2 : YS2 = YE2 : YE2 = YT2`XT2 = XS2 : XS2 = XE2 : XE2 = XT2 : YT2 = YS2 : YS2 = YE2 : YE2 = YT2
rem Check, if lines collide
if x# => XS1
if x# <= XE1
if x# => XS2
if x# <= XE2
if y# => YS1
if y# <= YE1
if y# => YS2
if y# <= YE2
exitfunction 1
endif
endif
endif
endif
endif
endif
endif
endif
endfunction 0
rem Antialiazed Line-Function
function AA_Line(x1,y1,x2,y2,Color as dword,alpha)
yadd# = 1.0*(y2-y1)/(1.0*(x2-x1))
cr = rgbr(color) : cg = rgbg(color) : cb = rgbb(color)
alpha_f# = (alpha/255.0)
if abs(yadd#) < 1 `x-difference is bigger than y-d
`print alpha_f#
y# = y1 - yadd#
stp = 1 : if x2 < x1 then stp = -1
yadd# = yadd# * stp
for x = x1 to x2 step stp
inc y#, yadd#
y_c = ceil(y#) : y_f = floor(y#)
f1# = 1-(y#-y_f) : f2# = 1-f1#
rem Top dot
actcol = point(x,y_f)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f1#*alpha_f#
newg = actg + (cg-actg)*f1#*alpha_f#
newb = actb + (cb-actr)*f1#*alpha_f#
dot x,y_f, rgb(newr,newg,newb)
rem Bottom dot
actcol = point(x,y_c)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f2#*alpha_f#
newg = actg + (cg-actg)*f2#*alpha_f#
newb = actb + (cb-actr)*f2#*alpha_f#
dot x,y_c, rgb(newr,newg,newb)
next x
else `y-difference is bigger than x-d
xadd# = 1.0/yadd#
x# = x1 - xadd#
stp = 1 : if y2 < y1 then stp = -1
xadd# = xadd# * stp
for y = y1 to y2 step stp
inc x#, xadd#
x_c = ceil(x#) : x_f = floor(x#)
f1# = 1-(x#-x_f) : f2# = 1-f1#
rem Top dot
actcol = point(x_f,y)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f1#*alpha_f#
newg = actg + (cg-actg)*f1#*alpha_f#
newb = actb + (cb-actr)*f1#*alpha_f#
dot x_f,y, rgb(newr,newg,newb)
rem Bottom dot
actcol = point(x_c,y)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f2#*alpha_f#
newg = actg + (cg-actg)*f2#*alpha_f#
newb = actb + (cb-actr)*f2#*alpha_f#
dot x_c,y, rgb(newr,newg,newb)
next x
endif
endfunction
2. Lines on different colored backdrop
r = 128
g = 128
b = 128
do
r = r+1-rnd(2) : if r < 0 then r = 0 else if r > 255 then r = 255
g = g+1-rnd(2) : if g < 0 then g = 0 else if g > 255 then g = 255
b = b+1-rnd(2) : if b < 0 then b = 0 else if b > 255 then b = 255
cls rgb(r,g,b)
lock pixels
AA_Line(0,0,640,480, rgb(255,0,0), 255)
AA_Line(640,0,0,480, rgb(200,100,0), 200)
AA_Line(0,240,640,240, rgb(0,0,200), 128)
AA_Line(320,0,mousex(),mousey(), rgb(255-g,b,255-r), 200)
unlock pixels
loop
rem Antialiazed Line-Function
function AA_Line(x1,y1,x2,y2,Color as dword,alpha)
yadd# = 1.0*(y2-y1)/(1.0*(x2-x1))
cr = rgbr(color) : cg = rgbg(color) : cb = rgbb(color)
alpha_f# = (alpha/255.0)
if abs(yadd#) < 1 `x-difference is bigger than y-d
`print alpha_f#
y# = y1 - yadd#
stp = 1 : if x2 < x1 then stp = -1
yadd# = yadd# * stp
for x = x1 to x2 step stp
inc y#, yadd#
y_c = ceil(y#) : y_f = floor(y#)
f1# = 1-(y#-y_f) : f2# = 1-f1#
rem Top dot
actcol = point(x,y_f)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f1#*alpha_f#
newg = actg + (cg-actg)*f1#*alpha_f#
newb = actb + (cb-actr)*f1#*alpha_f#
dot x,y_f, rgb(newr,newg,newb)
rem Bottom dot
actcol = point(x,y_c)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f2#*alpha_f#
newg = actg + (cg-actg)*f2#*alpha_f#
newb = actb + (cb-actr)*f2#*alpha_f#
dot x,y_c, rgb(newr,newg,newb)
next x
else `y-difference is bigger than x-d
xadd# = 1.0/yadd#
x# = x1 - xadd#
stp = 1 : if y2 < y1 then stp = -1
xadd# = xadd# * stp
for y = y1 to y2 step stp
inc x#, xadd#
x_c = ceil(x#) : x_f = floor(x#)
f1# = 1-(x#-x_f) : f2# = 1-f1#
rem Top dot
actcol = point(x_f,y)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f1#*alpha_f#
newg = actg + (cg-actg)*f1#*alpha_f#
newb = actb + (cb-actr)*f1#*alpha_f#
dot x_f,y, rgb(newr,newg,newb)
rem Bottom dot
actcol = point(x_c,y)
actr = rgbr(actcol) : actg = rgbg(actcol) : actb = rgbb(actcol)
newr = actr + (cr-actr)*f2#*alpha_f#
newg = actg + (cg-actg)*f2#*alpha_f#
newb = actb + (cb-actr)*f2#*alpha_f#
dot x_c,y, rgb(newr,newg,newb)
next x
endif
endfunction
Have fun with it..
Visit the DBPro - Speed up your game-Thread. http://forum.thegamecreators.com/?m=forum_view&t=88661&b=1