Bitmap/Bresenham's line algorithm: Difference between revisions

→‎version 1: redid the whole program, the program that it was previously based on was faulty.
m (→‎version 1: changed some comments and indentation, added whitespace.)
(→‎version 1: redid the whole program, the program that it was previously based on was faulty.)
Line 1,964:
=={{header|REXX}}==
===version 1===
This<lang rexx>/*REXX program isplots/draws modeleda afterline using the PL/I versionBresenham's line algorithm.*/
image@. = 'fa'x /*fill the array with middle-dotsmiddle─dots*/
<lang rexx>/*REXX program plots/draws a line using the Bresenham's line algorithm. */
EoE = 1000 /*EOE = End Of Earth, er, plot. */
image. = 'fa'x /*fill the array with middle-dots*/
plotC = 'fe'x /*character used for plotting pts*/
parse arg x0 y0 x1 y1 . do j=-EoE to +EoE /*drawallow specifying gridline─end frompts. lowest──►highest*/
if x0=='' | x0==',' then image.j.0x0 = '─' -1 /*drawif thenot horizontalspecified, axisuse default. */
if y0=='' | y0==',' then y0 = -3 /* " image.0.j =" '│' " /* " " verical " */
if x1=='' | x1==',' then x1 = 6 /* " " " " " end /*j*/
image.0.0if y1=='' | y1==',' then y1 = 10 /* " " " /*"draw" the axis origin." " */
minX=min(x0,x1); minY=min(y0,y1) /*find the min and max X value.*/
parse arg xi yi xf yf . /*allow specifying line-end pts. */
if ximaxX==''max(x0,x1); | xi= maxY='max(y0,'y1) then xi/* = -1" " /*if" not specified, use" " Y " default. */
if yiborder==''2 | yi==',' then yi = -3 /* " " " " " /*border=extra space around plot.*/
minX=minX-border*2; maxX=maxX+border*2 /*find the min and max X borders.*/
if xf=='' | xf==',' then xf = 6 /* " " " " " */
if yfminY=='' | yf==',' minY-border then yf; maxY= 10 maxY+border /* " " " " " " Y " " */
do x=minX to maxX; @.x.0='─'; end /*draw dash from left──► right.*/
minX=0; maxX=0
do y=minY to maxY; @.0.y='│'; end /*draw pipe from lowest──►highest*/
minY=0; maxY=0
call@.0.0='┼' draw_line xi, yi, xf, yf /*call subroutine and /*"draw" linethe axis origin. */
call draw_line x0, y0, x1, y1 /*call subroutine and draw line. */
border=2
EoE = 1000 /*EOE = End Of Earth, er, /* [↓] draw the plot. */
minX=minX-border*2; maxX=maxX+border*2
do y=maxY to minY by -1; _= /*display plot one line at a time*/
minY=minY-border ; maxY=maxY+border
do x=minX to maxX /*traipse throught the X axis. */
 
_=_ || @.x.y /*construct a "line" of the do y=maxY by -1 to minY; aRow=plot.*/
end /*x*/ /*(a line is a "row" of points.) do x=minX to maxX*/
say _ /*display a line of the plot. aRow=aRow || image.x.y*/
end /*y*/ /* [↑] all done ploting the end /*xpts.*/
say aRow
end /*y*/
 
exit /*stick a fork in it, we're done.*/
/*────────────────────────────────DRAW_LINE subroutine──────────────────*/
draw_line: procedure expose image@. minX maxX minY maxY plotC; error=0
parse arg xi 1 x0x, yi 1 y0 1 y,xf,yf; xf 1 x1, dx=abs(xf-x); dy=abs(yf 1 y1-y)
if x<xf then sx=+1
steep= abs(y1-y0) > abs(x1-x0)
else sx=-1
if steep then parse value x0 y0 x1 y1 with y0 x0 y1 x1
if y<yf then sy=+1
if x0>x1 then parse value x0 x1 y0 y1 with x1 x0 y1 y0
else sy=-1
 
err=dx-dy
if y0<y1 then yInc = 1
do else yInc = -1forever
call plotXY x,y,plotC
deltaE=abs(y1-y0) / (x1-x0)
if x=xf & if y0<y1y=yf then yInc = 1leave
 
do xe2=x0 to x1err+err
if steepe2>-dy then call plotXYdo; err=err-dy; y, x,=x+sx; plotCend
if e2< dx then do; err=err+dx; y=y+sy; else call plotXY x, y, plotCend
end error=error+deltaE /*forever*/
if error>=.5 then do; y=y+yInc; error=error-1; end
end /*x*/
return
/*────────────────────────────────PLOTXY subroutine─────────────────────*/
plotXY: procedure expose image@. minX maxX minY maxY; parse arg xx, yy, ppp
image@.xx.yy=ppp; minX=min(minX,xx); maxX=max(maxX,xx)
minY=min(minY,yy); maxY=max(maxY,yy)
return</lang>
'''output''' when using the default input
<pre style="overflow:scroll">
·········│··········
·········│··········
·········│··········
·········│···■······
·········│··■·······
·········│··■·······
·········│·■········
·········│·■········
·········│■·········
·········│■·········
·········■··········
·········■·│■·········
─────┼■─────────
········■│··········
────────■┼──────────
·······■·│··········
·······■·│■│··········
······■··│··········
·········│··········
·········│··········
·········│··········
</pre>