Bitmap/Bresenham's line algorithm: Difference between revisions

→‎version 2: Rexx version 2 corrected
(→‎version 1: redid the whole program, the program that it was previously based on was faulty.)
(→‎version 2: Rexx version 2 corrected)
Line 2,034:
===version 2===
<lang>/* REXX ***************************************************************
* 1921.05.2014 Walter Pachl
* Implementing the pseudo code of
* http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
* under 'Simplification'
**********************************************************************/
grid.='.'
Do i=-2 To 7; grid.i.0='-' ; End
Do j=-4 To 11; grid.0.j='|'; End
grid.0.0=0'+'
Call draw_lineline -1,6,-3,6,10
Do j=11 To -4 By -1;
ol=rightformat(j,2)' '
Do i=-2 To 7
ol=ol||grid.i.j
End;
Say ol
End;
Say ' 2101234567'
Exit
line: Procedure Expose grid.
draw_line:
Parse Arg x0,x1, y0, x1, y1
deltaxdx = abs(x1-x0;)
deltaydy = abs(y1-y0;)
if x0 < x1 then sx = 1
error=0;
else sx = -1
deltaerr=abs(deltay/deltax);
if y0 < y1 then sy = 1
y=y0;
else sy = -1
Do x=x0 to x1;
err = dx-dy
grid.x.y='X';
 
error=error+deltaerr;
Do Forever
do while error>=0.5
grid.xx0.yy0='X';
y=y+1;
if x0 = x1 & y0 = y1 Then Leave
error=error-1.0;
e2 = End;2*err
if e2 > -dy then do
End;
err = err - dy
x0 = x0 + sx
end
if e2 < dx then do
err = err + dx
y0 = y0 + sy
end
end
Return</lang>
'''output'''
<pre>11 ..|.......
10 ..|.....X.
9 ..|.....X..
8 ..|....X..
7 ..|....X...
6 ..|...X...
5 ..|...X....
4 ..|..X....
3 ..|.X.....
2 ..|..X.....
1 ..|X......
0 --0-+X------
-1 ..X.......
-2 ..|X.......
-3 .X|.......
-4 ..|.......
2,298

edits