Dark Bit Factory & Gravity
PROGRAMMING => Other languages => Blitz => Topic started by: Shockwave on May 11, 2006
-
Taken from the old board, original post by shockwave.
Pixelwise line draw command. I needed to draw lines to an area of memory instead of the screen for somehing I'm working on, the built in line command wouldn't do so I had to write my own line drawing code, here it is modified to draw on the screen but it's easy to change it to work on a bank or an array.
;
; Line Draw Routine By Shockwave.
; You MUST give credit for this linedraw code to Shockwave / DBF if you use it.
;
; For the function, you need to call it like this;
;
; linedraw(X1,Y1,X2,Y2,RED,GREEN,BLUE)
;
; Red green and blue are decimals betweem 0 and 255
;
; DO NOT try and draw outside the screen as there is no clipping.
;
;-------------------------------------------------------------------------------------------------------
Const xres = 640
Const yres = 480
Graphics xres,yres,0,2
SetBuffer BackBuffer()
While Not KeyDown(1)
sinadd=sinadd+5
For a=1 To 200
x1=320+(310*Sin(a+sinadd))
x2=320+(310*Sin(a+(sinadd+50)))
y1=240+(220*Cos((a*2)+sinadd))
y2=240+(220*Cos((a*2)+(sinadd+50)))
linedraw(x1,y1,x2,y2,100+90*Sin(a+sinadd),100+90*Sin((a*2)+sinadd),100+90*Sin((a*3)+sinadd))
Next
Flip
Cls
Wend
;-------------------------------------------------------------------------------------------------------
;Pixel Wise Line Draw Routine (C) Shockwave and (C) DBF!
;-------------------------------------------------------------------------------------------------------
Function linedraw(linex1,liney1,linex2,liney2,red,green,blue)
Local linergb = (((red Shl 8)+green) Shl 8)+blue
Local LINEXDV#; X direction vector
Local LINEYDV#; Y direction vector
Local startx#; X start pos (needs to be a float)
Local starty#; Y start pos (needs to be a float)
Local xdistance#; Distance between x co-ords
Local ydistance#; Distance between y co-ords
Local greaterx,lesserx; Used in X distance calc
Local greatery,lessery; Used in Y distance calc
Local Lineloopsize; How many iterations are needed to draw line?
Local lineloop; Loop variable
;-------------------------------------------------------------------------------------------------------
; First we need to get the greatest distance is (X or Y) It doesn't matter.
;-------------------------------------------------------------------------------------------------------
If linex1 < linex2 Then
lesserx = linex1
greaterx = linex2
Else
lesserx = linex2
greaterx = linex1
End If
;-------------------------------------------------------------------------------------------------------
If liney1 < liney2 Then
lessery = liney1
greatery = liney2
Else
lessery = liney2
greatery = liney1
End If
;-------------------------------------------------------------------------------------------------------
xdistance = (greaterx-lesserx)
ydistance = (greatery-lessery)
If xdistance=0 xdistance=1; Protect against by zero divisions
If ydistance=0 ydistance=1;
;-------------------------------------------------------------------------------------------------------
If xdistance>=ydistance Then
lineloopsize = xdistance
Else
lineloopsize = ydistance
End If
;-------------------------------------------------------------------------------------------------------
LINEXDV#=Float(greaterx-lesserx)/lineloopsize
LINEYDV#=Float(greatery-lessery)/lineloopsize
startx=lesserx
starty=lessery
;-------------------------------------------------------------------------------------------------------
LockBuffer BackBuffer()
For a=0 To lineloopsize
WritePixelFast Int(startx#),Int(starty#),linergb
startx#=startx#+(LINEXDV#)
starty#=starty#+(LINEYDV#)
Next
UnlockBuffer BackBuffer()
End Function
-
Nifty routine, welldone Shockwave.
Cheers,
Clyde.