Anonymous user
Bitmap/Bresenham's line algorithm: Difference between revisions
→version 1: re-worked program to handle multiple-line segments.
(→version 1: simplified the program, eliminated the re-calculating of the min and max point values, removed the STYLE from the PRE html tag.) |
(→version 1: re-worked program to handle multiple-line segments.) |
||
Line 1,978:
=={{header|REXX}}==
===version 1===
This REXX version has automatic scaling (for displaying the plot) and handles multiple line segments.
<lang rexx>/*REXX program plots/draws
@.='·' /*fill the array with middle─dots*/
parse arg
if
data=translate(data,,'()[]{}/,:;') /*elide chaff from data points. */
parse var data x y data; !.points=x y /*extract line segments.*/
if points==1 then do; minX=x; maxX=x; minY=y; maxY=y; end /*1st case*/
minX=min(minX,x); maxX=max(maxX,x); minY=min(minY,y); maxY=max(maxY,y)
end /*points*/ /* [↑] data points pairs in !. */
border=2 /*border=extra space around plot.*/
minX=
minY=
do x=minX to maxX; @.x.0='─'; end /*draw dash from left──► right.*/
do y=minY to maxY; @.0.y='│'; end /*draw pipe from lowest──►highest*/
@.0.0='┼' /*define the plot's axis point. */
call draw_line !._, !.seg /*draw (plot) a line segment. */
end /*seg*/ /* [↑] drawing the line segments*/
/* [↓] display the plot to term.*/
do y=maxY to minY by -1; _= /*display plot one line at a time*/
Line 2,002 ⟶ 2,009:
exit /*stick a fork in it, we're done.*/
/*────────────────────────────────DRAW_LINE subroutine──────────────────*/
draw_line: procedure expose @.; parse arg x
dx=abs(xf-x); if x<xf then
else sx=-1▼
dy=abs(yf-y); if y<yf then sy=+1 /*obtain Y range, determine slope*/
▲ else sx=-1
else sy=-1▼
err=dx-dy /*calc error between adjustments.*/
▲ else sy=-1
do forever; @.x.y=plotChar /*plot the points until complete.*/
if x=xf & y=yf then leave /*are plot points at the finish? */
if
if err2 < dx then do; err=err+dx; y=y+sy; end
end /*while ··· */
return</lang>
'''output''' when using the default input:
<pre>
···│··········
···│··········
···│··
···│··
···│··
···│····Θ··
···│···Θ····
···│···Θ······
···│Θ··
···│·Θ·
···│··Θ··········Θ······
···│···Θ·······ΘΘ·······
···│····Θ·····Θ·········
···│·
···│··
···│·······Θ··
───┼────────────────────
···│····················
</pre>
|