February 25, 2021, 02:52:13 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bmx] Besenham's LineDraw routine (integer math only) by ImaginaryHuman [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
Author
Topic: [bmx] Besenham's LineDraw routine (integer math only) by ImaginaryHuman [ 1+ years ago ] (Read 1390 times)
BlitzBot
Jr. Member
Posts: 1
[bmx] Besenham's LineDraw routine (integer math only) by ImaginaryHuman [ 1+ years ago ]
«
on:
June 29, 2017, 12:28:43 AM »
Title :
Besenham's LineDraw routine (integer math only)
Author :
ImaginaryHuman
Posted :
1+ years ago
Description :
This program is a conversion from a C program taken from the Wikipedia, based on the line-drawing algorithm by Mr Bresenham. It uses purely Integer math only - absolutely no floats. It plots one pixel at a time. Clipping is handled by the standard clipping of the backbuffer, by the graphics hardware for each pixel plotted (which is a Vertex in OpenGL). This isn't an OpenGL-dependent routine, you can easily use some other Plot routine. Each pixel can be a different color, or you could skip pixels, do dotted/dashed lines, marching ants, etc
Code :
Code: BlitzMax
'Bresenham linedraw in BlitzMax, adapted from C code
Strict
Graphics
640
,
480
,
0
Repeat
Cls
Line
(
320
,
240
,MouseX
(
)
,MouseY
(
)
)
Flip
Until
KeyHit
(
KEY_ESCAPE
)
End
Function
Line
(
X1:
Int
,Y1:
Int
,X2:
Int
,Y2:
Int
)
'Draws a line of individual pixels from X1,Y1 to X2,Y2 at any angle
Local
Steep:
Int
=
Abs
(
Y2-Y1
)
>
Abs
(
X2-X1
)
'Boolean
If
Steep
Local
Temp:
Int
=X1; X1=Y1; Y1=Temp
'Swap X1,Y1
Temp=X2; X2=Y2; Y2=Temp
'Swap X2,Y2
EndIf
Local
DeltaX:
Int
=
Abs
(
X2-X1
)
'X Difference
Local
DeltaY:
Int
=
Abs
(
Y2-Y1
)
'Y Difference
Local
Error:
Int
=
0
'Overflow counter
Local
DeltaError:
Int
=DeltaY
'Counter adder
Local
X:
Int
=X1
'Start at X1,Y1
Local
Y:
Int
=Y1
Local
XStep:
Int
Local
YStep:
Int
If
X1<X2
Then
XStep=
1
Else
XStep=-
1
'Direction
If
Y1<Y2
Then
YStep=
1
Else
YStep=-
1
'Direction
If
Steep
Then
Plot
(
Y,X
)
Else
Plot
(
X,Y
)
'Draw
While
X<>X2
X:+XStep
'Move in X
Error:+DeltaError
'Add to counter
If
(
Error
Shl
1
)
>DeltaX
'Would it overflow?
Y:+YStep
'Move in Y
Error=Error-DeltaX
'Overflow/wrap the counter
EndIf
If
Steep
Then
Plot
(
Y,X
)
Else
Plot
(
X,Y
)
'Draw
Wend
End
Function
Comments :
none...
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bmx] Besenham's LineDraw routine (integer math only) by ImaginaryHuman [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal