Code archives/Graphics/Gradient Line

This code has been declared by its author to be Public Domain code.

Download source code

Gradient Line by DarkEagle
(Posted 24 years ago)
gradient line
Function GradLine(x1,y1,x2,y2,r1,g1,b1,r2,g2,b2)

dx# = x2-x1
dy# = y2-y1

m# = dy/dx

l# = Sqr(dx^2 + dy^2)

rinc# = (r2-r1)/l
ginc# = (g2-g1)/l
binc# = (b2-b1)/l

r# = r1
g# = g1
b# = b1

For x = x1 To x2
	argb = (b Or (g Shl 8) Or (r Shl 16) Or ($ff000000))
	WritePixel x,(m*x)+y1,argb
	r = r + rinc
	g = g + ginc
	b = b + binc
Next

End Function

Comments

Sir Gak
(Posted 23 years ago)
This can be speeded up phenomenally by using Lockbuffer and UnlockBuffer. Using a 1000 reps loop, I measured over 32000 millseconds without LockBuffer. When utilizing LockBuffer, the time drops to about 74 millseconds.

Graphics 800,600
GradLine(100,30,799,530,0,0,0,128,128,255)
MouseWait()
End

Function GradLine(x1,y1,x2,y2,r1,g1,b1,r2,g2,b2)

dx# = x2-x1
dy# = y2-y1

m# = dy/dx

l# = Sqr(dx^2 + dy^2)

rinc# = (r2-r1)/l
ginc# = (g2-g1)/l
binc# = (b2-b1)/l

r# = r1
g# = g1
b# = b1
timer#=MilliSecs()
LockBuffer FrontBuffer()
For loop=1 To 1000
For x = x1 To x2
argb = (b Or (g Shl 8) Or (r Shl 16) Or ($ff000000))
WritePixel x,(m*x)+y1,argb
r = r + rinc
g = g + ginc
b = b + binc
Next
Next
UnlockBuffer FrontBuffer()
Print MilliSecs()-timer
End Function

Code Archives Forum