Bitmap/Bresenham's line algorithm: Difference between revisions

m (Automated syntax highlighting fixup (second round - minor fixes))
imported>Chinhouse
(12 intermediate revisions by 6 users not shown)
Line 773:
END start
</pre>
=={{header|ATS}}==
See [[Bresenham_tasks_in_ATS]].
 
=={{header|AutoHotkey}}==
 
Line 794 ⟶ 797:
}
}</syntaxhighlight>
 
=={{header|AutoIt}}==
 
Line 1,470 ⟶ 1,474:
drawLine(i, 1, 1, 3, 18, makeColor.fromFloat(0,1,1))
i.writePPM(<import:java.io.makeFileOutputStream>(<file:~/Desktop/Bresenham.ppm>))</syntaxhighlight>
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=XZHNboMwEITvPMUeG0W4NrSHSnHehQRHQkoJMiRl3z6zePkrAsv7MTNe2118XKnrw0AjMRkyGRH9Pl4B9Sd9gWEUFsN1IGuK72nITNaJs47V371pobbElkZH7OaUeiRP1aWnD+AcioPQXmjuZNrcxHaCS6r531SkAJ4DWAJYA3gbwBLASwDvAkKMokVO3byoUAv6OiNbLUkDtkhM2m4XqkE16Xxkhwqe7dDchXjZctXW0odf+wgFKiRriUVBzuhkVKIL535tBA8Cjx6noMTs7KedVNxH6XtFnNy8dRtc1HJHhbXk5LUyXbmC6St/7N4AQOV/R7lxOJu9AQ== Run it]
 
{{trans|C}}
 
<syntaxhighlight>
proc pset x y . .
move x / 4 y / 4
rect 0.25 0.25
.
proc drawline x0 y0 x1 y1 . .
dx = abs (x1 - x0)
sx = -1
if x0 < x1
sx = 1
.
dy = abs (y1 - y0)
sy = -1
if y0 < y1
sy = 1
.
err = -dy div 2
if dx > dy
err = dx div 2
.
repeat
pset x0 y0
until x0 = x1 and y0 = y1
e2 = err
if e2 > -dx
err -= dy
x0 += sx
.
if e2 < dy
err += dx
y0 += sy
.
.
.
drawline 200 10 100 200
drawline 100 200 200 390
drawline 200 390 300 200
drawline 300 200 200 10
</syntaxhighlight>
 
 
=={{header|Elm}}==
 
Line 1,530 ⟶ 1,581:
 
</syntaxhighlight>
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
Line 2,408 ⟶ 2,460:
Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(0.0) Gray{Float64}(255.0)
Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(0.0) </pre>
=={{header|Korn Shell}}==
 
=={{header|ksh}}==
<syntaxhighlight lang="text">function line {
<syntaxhighlight lang="ksh">function line {
x0=$1; y0=$2 x1=$3; y1=$4
typeset x0=$1 y0=$2 x1=$3 y1=$4
 
if (( x0 > x1 ))
then
((dx = x0 - x1)); ((sx = -1))
Line 2,420 ⟶ 2,472:
fi
 
if (( y0 > y1 ))
then
((dy = y0 - y1)); ((sy = -1))
Line 2,427 ⟶ 2,479:
fi
 
if (( dx > dy ))
then
((err = dx))
Line 2,435 ⟶ 2,487:
((err /= 2)); ((e2 = 0))
 
while /bin/true:
do
echo $x0 $y0
(( x0 == x1 && y0 == y1 )) && return
((e2 = err))
(( e2 > -dx)) && { ((err -= dy )); (( x0 += sx )) }
(( e2 < dy)) && { ((err += dx )); (( y0 += sy )) }
 
done
}</syntaxhighlight>
Output from the statement:
 
Output from the statement:-
line 0 0 3 4
(which could be piped to another program)
<pre>0 0
<syntaxhighlight lang="text">0 0
1 1
1 2
2 3
3 4</syntaxhighlightpre>
 
=={{header|Lua}}==
{{trans|C}}
Line 2,781 ⟶ 2,832:
}
}</syntaxhighlight>
 
=={{header|MiniScript}}==
This GUI implementation is for use with [http://miniscript.org/MiniMicro Mini Micro].
<syntaxhighlight lang="miniscript">
drawLine = function(img, x0, y0, x1, y1, colr)
sign = function(a, b)
if a < b then return 1
return -1
end function
dx = abs(x1 - x0)
sx = sign(x0, x1)
dy = abs(y1 - y0)
sy = sign(y0, y1)
if dx > dy then
err = dx
else
err = -dy
end if
err = floor(err / 2)
while true
img.setPixel x0, y0, colr
if x0 == x1 and y0 == y1 then break
e2 = err
if e2 > -dx then
err -= dy
x0 += sx
end if
if e2 < dy then
err += dx
y0 += sy
end if
end while
end function
 
img= Image.create(320, 320)
drawLine img, 0, 0, 250, 300, color.red
gfx.clear
gfx.drawImage img, 0, 0
</syntaxhighlight>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import bitmap
Line 3,912 ⟶ 4,007:
 
img.save("test1.png")</syntaxhighlight>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "DrawLine" )
@( description, "Draw a line given 2 points with the Bresenham's algorithm." )
@( see_also, "http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure drawline is
 
-- Spar 1.x has only single-dimensional arrays but we can simulate a
-- two dimensional array that has been folded into a 1D array
 
width : constant positive := 20;
height : constant positive := 20;
type image_array is array(1..400) of character;
Picture : image_array;
 
-- Line
-- Draw a line between two coordinates using the given character
 
procedure Line ( Start_X : positive; Start_Y : positive; Stop_X : positive; Stop_Y : positive; Color : character) is
 
-- at this point, formal parameters are defined but the actual values aren't defined!
-- but creating a dummy Line in a test script works?
 
DX : constant float := abs( float( Stop_X ) - float( Start_X ) );
DY : constant float := abs( float( Stop_Y ) - float( Start_Y ) );
Err : float;
X : positive := Start_X;
Y : positive := Start_Y;
Step_X : integer := 1;
Step_Y : integer := 1;
begin
if Start_X > Stop_X then
Step_X := -1;
end if;
if Start_Y > Stop_Y then
Step_Y := -1;
end if;
if DX > DY then
Err := DX / 2.0;
while X /= Stop_X loop
Picture (X + width*(Y-1)) := Color;
Err := @ - DY;
if Err < 0.0 then
Y := positive( integer(@) + Step_Y);
Err := @ + DX;
end if;
X := positive( integer(@) + Step_X );
end loop;
else
Err := DY / 2.0;
while Y /= Stop_Y loop
Picture (X + height*(Y-1)) := Color;
Err := @ - DX;
if Err < 0.0 then
X := positive( integer(@) + Step_X );
Err := @ + DY;
end if;
Y := positive( integer(@) + Step_Y );
end loop;
end if;
Picture (X + width*(Y-1)) := Color;
end Line;
 
-- new_picture
-- Erase the picture by filling it with spaces.
 
procedure new_picture is
begin
for i in arrays.first( Picture )..arrays.last( Picture ) loop
Picture(i) := ' ';
end loop;
end new_picture;
 
-- render
-- Draw the contents of the picture area.
 
procedure render is
begin
for i in arrays.first( Picture )..arrays.last( Picture ) loop
put( Picture(i) );
if i mod width = 0 then
new_line;
end if;
end loop;
end render;
 
begin
new_picture;
Line( 1, 8, 8, 16, 'X' );
Line( 8,16,16, 8, 'X' );
Line(16, 8, 8, 1, 'X' );
Line( 8, 1, 1, 8, 'X' );
render;
end drawline;</syntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
Line 4,139 ⟶ 4,336:
{{libheader|DOME}}
Requires version 1.3.0 of DOME or later.
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, ImageData, Color
import "dome" for Window
 
Line 4,196 ⟶ 4,393:
static draw(alpha) {}
}</syntaxhighlight>
 
=={{header|XPL0}}==
Bresenham line draw is built-in.
Anonymous user