Bitmap/Bresenham's line algorithm: Difference between revisions

m
m (syntax highlighting fixup automation)
 
(14 intermediate revisions by 7 users not shown)
Line 1:
[[Category:Graphics algorithms]]
[[Category:Geometry]]
{{task|Raster graphics operations}}
[[Category:Graphics algorithms]]
 
;Task:
Line 6 ⟶ 7:
<br>draw a line given two points with [[wp:Bresenham's line algorithm|Bresenham's line algorithm]].
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">T Colour = BVec3
Byte r, g, b
 
F (r, g, b)
.r = r
.g = g
.b = b
 
F ==(other)
R .r == other.r & .g == other.g & .b == other.b
 
V black = Colour(0, 0, 0)
Line 112 ⟶ 103:
=={{header|360 Assembly}}==
{{trans|Rexx}}
<syntaxhighlight lang="360asm">* Bitmap/Bresenham's line algorithm - 13/05/2019
BRESENH CSECT
USING BRESENH,R13 base register
Line 323 ⟶ 314:
...|....................
</pre>
 
=={{header|Action!}}==
Part of the task is available in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBLINE.ACT RGBLINE.ACT].
{{libheader|Action! Bitmap tools}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm
 
RGB black,yellow,violet,blue
Line 403 ⟶ 393:
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bresenham's_line_algorithm.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">procedure Line (Picture : in out Image; Start, Stop : Point; Color : Pixel) is
DX : constant Float := abs Float (Stop.X - Start.X);
DY : constant Float := abs Float (Stop.Y - Start.Y);
Line 446 ⟶ 435:
end Line;</syntaxhighlight>
The test program's
<syntaxhighlight lang="ada"> X : Image (1..16, 1..16);
begin
Fill (X, White);
Line 473 ⟶ 462:
H
</pre>
 
=={{header|ALGOL 68}}==
{{trans|Ada}}
Line 479 ⟶ 467:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
'''File: prelude/Bitmap/Bresenhams_line_algorithm.a68'''<syntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
 
line OF class image := (REF IMAGE picture, POINT start, stop, PIXEL color)VOID:
Line 520 ⟶ 508:
END # line #;
 
SKIP</syntaxhighlight>'''File: test/Bitmap/Bresenhams_line_algorithm.a68'''<syntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
Line 554 ⟶ 542:
ffffffffffffffffffffffffffffffffffffffffff000000ffffffffffffffffffffffffffffffffffffffffffffffff
</pre>
 
=={{header|Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 10 HGR :FULLSCREEN = PEEK (49234)
20 HCOLOR= 3
30 FOR N = 3 TO 279 STEP 4
Line 778 ⟶ 765:
END start
</pre>
=={{header|ATS}}==
See [[Bresenham_tasks_in_ATS]].
 
=={{header|AutoHotkey}}==
 
<syntaxhighlight lang=AutoHotkey"autohotkey">Blue := Color(0,0,255)
White := Color(255,255,255)
Bitmap := Bitmap(100,100,Blue) ;create a 100*100 blue bitmap
Line 803 ⟶ 792:
=={{header|AutoIt}}==
 
<syntaxhighlight lang=AutoHotkey"autohotkey">Local $var = drawBresenhamLine(2, 3, 2, 6)
 
Func drawBresenhamLine($iX0, $iY0, $iX1, $iY1)
Line 826 ⟶ 815:
WEnd
EndFunc ;==>drawBresenhamLine</syntaxhighlight>
 
=={{header|bash}}==
<syntaxhighlight lang="bash">#! /bin/bash
 
function line {
Line 891 ⟶ 879:
line $((COLS/4*3)) $((LINS/2)) $((COLS/2)) 1
echo -e "\e[${LINS}H"</syntaxhighlight>
 
=={{header|BASIC}}==
<syntaxhighlight lang="qbasic"> 1500 REM === Draw a line. Ported from C version
1510 REM Inputs are X1, Y1, X2, Y2: Destroys value of X1, Y1
1520 DX = ABS(X2 - X1):SX = -1:IF X1 < X2 THEN SX = 1
Line 905 ⟶ 892:
1600 IF E2 < DY THEN ER = ER + DX:Y1 = Y1 + SY
1610 GOTO 1560</syntaxhighlight>
 
=={{header|Batch File}}==
<syntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
 
Line 1,035 ⟶ 1,021:
)
goto :eof</syntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> Width% = 200
Height% = 200
Line 1,073 ⟶ 1,058:
ENDPROC</syntaxhighlight>
[[File:bresenham_bbc.gif]]
 
=={{header|C}}==
 
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
<syntaxhighlight lang=C"c">void line(int x0, int y0, int x1, int y1) {
 
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
Line 1,091 ⟶ 1,075:
}
}</syntaxhighlight>
 
=={{header|C sharp|C#}}==
Port of the C version.
<syntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Drawing.Imaging;
Line 1,121 ⟶ 1,104:
}
}</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
void Line( float x1, float y1, float x2, float y2, const Color& color )
{
Line 1,169 ⟶ 1,151:
}
</syntaxhighlight>
 
=={{header|Clojure}}==
 
<syntaxhighlight lang="clojure">
 
(defn draw-line
Line 1,198 ⟶ 1,179:
(recur (inc x) y (- error delta-y)))))))))))
</syntaxhighlight>
 
=={{header|CoffeeScript}}==
 
<syntaxhighlight lang="coffeescript">
drawBresenhamLine = (x0, y0, x1, y1) ->
dx = Math.abs(x1 - x0)
Line 1,221 ⟶ 1,201:
null
</syntaxhighlight>
 
=={{header|Commodore BASIC}}==
<syntaxhighlight lang=Basic"basic">
10 rem bresenham line algorihm
20 rem translated from purebasic
Line 1,264 ⟶ 1,243:
</syntaxhighlight>
[https://www.worldofchris.com/assets/c64-bresenham-line.png C64 Example screenshot]
 
=={{header|Common Lisp}}==
 
<syntaxhighlight lang="lisp">(defun draw-line (buffer x1 y1 x2 y2 pixel)
(declare (type rgb-pixel-buffer buffer))
(declare (type integer x1 y1 x2 y2))
Line 1,295 ⟶ 1,273:
(incf error delta-x))))
buffer))</syntaxhighlight>
 
=={{header|D}}==
This code uses the Image defined in [[Bitmap]] Task.
 
<syntaxhighlight lang="d">module bitmap_bresenhams_line_algorithm;
 
import std.algorithm, std.math, bitmap;
Line 1,375 ⟶ 1,352:
###.###########.#########
#########################</pre>
 
=={{header|Delphi}}==
 
<syntaxhighlight lang="delphi">
procedure drawLine (bitmap : TBitmap; xStart, yStart, xEnd, yEnd : integer; color : TAlphaColor);
// Bresenham's Line Algorithm. Byte, March 1988, pp. 249-253.
Line 1,452 ⟶ 1,428:
end;
</syntaxhighlight>
 
=={{header|E}}==
 
{{trans|C}}
 
<syntaxhighlight lang="e">def swap(&left, &right) { # From [[Generic swap]]
def t := left
left := right
Line 1,488 ⟶ 1,463:
}</syntaxhighlight>
 
<syntaxhighlight lang="e">def i := makeImage(5, 20)
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}}==
 
<syntaxhighlight lang="elm">
 
 
Line 1,554 ⟶ 1,575:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
build_path({Sx, Sy}, {Tx, Ty}) ->
if
Line 1,602 ⟶ 1,623:
</syntaxhighlight>
OR
<syntaxhighlight lang="erlang">
line({X0, Y0}, {X1, Y1}) ->
SX = step(X0, X1),
Line 1,634 ⟶ 1,655:
{Y, E}.
</syntaxhighlight>
 
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE"erre">
PROGRAM BRESENHAM
 
Line 1,668 ⟶ 1,688:
END PROGRAM
</syntaxhighlight>
 
=={{header|Euphoria}}==
 
{{trans|C}}
 
<syntaxhighlight lang="euphoria">include std/console.e
include std/graphics.e
include std/math.e
Line 1,809 ⟶ 1,828:
Press Any Key to continue...
</pre>
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">let inline bresenham fill (x0, y0) (x1, y1) =
let steep = abs(y1 - y0) > abs(x1 - x0)
let x0, y0, x1, y1 =
Line 1,829 ⟶ 1,847:
The following program tests the above bresenham function by drawing 100 lines into an image and visualizing the result using
{{libheader|Windows Presentation Foundation}}:
<syntaxhighlight lang="fsharp">open System.Windows
open System.Windows.Media.Imaging
 
Line 1,846 ⟶ 1,864:
Window(Content=image, Title="Bresenham's line algorithm")
|> (Application()).Run |> ignore</syntaxhighlight>
 
=={{header|Factor}}==
A very ugly imperative implementation similar to the wikipedia pseudocode..
<syntaxhighlight lang="factor">USING: accessors arrays kernel locals math math.functions
math.ranges math.vectors rosettacode.raster.display
rosettacode.raster.storage sequences ui.gadgets ;
Line 1,886 ⟶ 1,903:
[ line-points ] dip
[ set-pixel ] curry with each ;</syntaxhighlight>
 
=={{header|FBSL}}==
1. In FBSL, successive calls to one and the same subprocedure may be concatenated to a series of argument sets as in Sub Rhombus() below.
Line 1,893 ⟶ 1,909:
 
'''Using pure FBSL's built-in graphics functions:'''
<syntaxhighlight lang="qbasic">#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
 
Line 1,929 ⟶ 1,945:
END SUB</syntaxhighlight>
'''Output:''' [[File:FBSLBresenham.PNG]]
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">defer steep \ noop or swap
defer ystep \ 1+ or 1-
 
Line 1,979 ⟶ 1,994:
**
ok</syntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{trans|C}}
<syntaxhighlight lang="fortran">module RCImagePrimitive
use RCImageBasic
 
Line 2,056 ⟶ 2,070:
Usage example:
 
<syntaxhighlight lang="fortran">program BasicImageTests
use RCImageBasic
use RCImageIO
Line 2,083 ⟶ 2,097:
 
end program BasicImageTests</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 16-09-2015
' compile with: fbc -s console
' OR compile with: fbc -s gui
Line 2,124 ⟶ 2,137:
 
End</syntaxhighlight>
 
=={{header|Go}}==
<syntaxhighlight lang="go">package raster
 
// Line draws line by Bresenham's algorithm.
Line 2,173 ⟶ 2,185:
}</syntaxhighlight>
A demonstration program:
<syntaxhighlight lang="go">package main
 
// Files required to build supporting package raster are found in:
Line 2,196 ⟶ 2,208:
}
}</syntaxhighlight>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">module Bitmap.Line(line) where
 
import Bitmap
Line 2,231 ⟶ 2,242:
deltay = abs $ y2 - y1
ystep = if y1 < y2 then 1 else -1</syntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
 
Using definitions from [[Basic bitmap storage#J|Basic bitmap storage]].
<syntaxhighlight lang="j">thru=: <./ + -~ i.@+ _1 ^ > NB. integers from x through y
 
NB.*getBresenhamLine v Returns points for a line given start and end points
Line 2,254 ⟶ 2,264:
 
'''Example Usage:'''
<syntaxhighlight lang="j"> myimg=: 0 255 0 makeRGB 20 32 NB. 32 by 20 green image
myimg=: ((1 1 ,: 5 11) ; 255 0 0 ) drawLines myimg NB. draw red line from xy point 1 1 to 11 5
 
Line 2,263 ⟶ 2,273:
viewRGB myimg=: (Square;0 0 255) drawLines myimg NB. draw 4 blue lines to form a square
viewRGB (Diamond;255 0 0) drawLines (Square;0 0 255) drawLines myimg</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
Line 2,380 ⟶ 2,389:
}
}</syntaxhighlight>
 
=={{header|JavaScript}}==
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
<syntaxhighlight lang="javascript">function bline(x0, y0, x1, y1) {
 
var dx = Math.abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
Line 2,397 ⟶ 2,405:
}
}</syntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<syntaxhighlight lang=Julia"julia">function drawline!(img::Matrix{T}, x0::Int, y0::Int, x1::Int, y1::Int, col::T) where T
δx = abs(x1 - x0)
δy = abs(y1 - y0)
Line 2,446 ⟶ 2,453:
Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(255.0) Gray{Float64}(0.0) </pre>
 
=={{header|Korn Shellksh}}==
<syntaxhighlight lang="ksh">function line {
typeset x0=$1 y0=$2 x1=$3 y1=$4
 
if ((x0 > x1))
<syntaxhighlight lang=text>function line {
x0=$1; y0=$2 x1=$3; y1=$4
 
if (( x0 > x1 ))
then
((dx = x0 - x1)); ((sx = -1))
Line 2,458 ⟶ 2,464:
fi
 
if (( y0 > y1 ))
then
((dy = y0 - y1)); ((sy = -1))
Line 2,465 ⟶ 2,471:
fi
 
if (( dx > dy ))
then
((err = dx))
Line 2,473 ⟶ 2,479:
((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}}
{{works with|Lua 5.1 (or above, tested on: 5.1.5, 5.2.3, 5.3.5)}}
<syntaxhighlight lang=Lua"lua">
-----------------------------------------------
-- Bitmap replacement
Line 2,605 ⟶ 2,609:
.............................XXX.............................
</pre>
 
=={{header|Maple}}==
 
<syntaxhighlight lang="maple">SegmentBresenham := proc (img, x0, y0, x1, y1)
local deltax, deltay, x, y, ystep, steep, err, img2, x02, y02, x12, y12;
x02, x12, y02, y12 := y0, y1, x0, x1;
Line 2,644 ⟶ 2,647:
return img2;
end proc:</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">Rasterize[Style[Graphics[Line[{{0, 0}, {20, 10}}]], Antialiasing -> False]]</syntaxhighlight>
 
=={{header|MATLAB}}==
Note: Store this function in a file named "bresenhamLine.m" in the @Bitmap folder for the Bitmap class defined [[Bitmap#MATLAB|here]].
[[File:Bresenham.png|thumb|MATLAB sample usage output.]]
<syntaxhighlight lang=MATLAB"matlab">
%screen = Bitmap object
%startPoint = [x0,y0]
Line 2,716 ⟶ 2,717:
 
Sample Usage:
<syntaxhighlight lang=MATLAB"matlab">
>> img = Bitmap(800,600);
>> img.bresenhamLine([400 550],[200 400],[255 255 255]);
Line 2,726 ⟶ 2,727:
>> disp(img)
</syntaxhighlight>
 
=={{header|MAXScript}}==
 
<syntaxhighlight lang="maxscript">fn plot img coord steep col =
(
if steep then
Line 2,781:
myBitmap = drawLine myBitmap [0, 511] [511, 0] #((color 255 255 255))
display myBitmap</syntaxhighlight>
 
=={{header|Metal}}==
 
For drawing lines between points in an Apple Metal compute shader.
 
<syntaxhighlight lang="metal">void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end);
 
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end)
Line 2,825 ⟶ 2,824:
}
}</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
 
proc drawLine*(img: Image; p, q: Point; color: Color) =
Line 2,880 ⟶ 2,922:
......H.H.......
.......H........</pre>
 
=={{header|OCaml}}==
 
<syntaxhighlight lang="ocaml">let draw_line ~img ~color ~p0:(x0,y0) ~p1:(x1,y1) =
 
let steep = abs(y1 - y0) > abs(x1 - x0) in
Line 2,923 ⟶ 2,964:
loop x0 y0 error
;;</syntaxhighlight>
 
=={{header|Pascal}}==
 
[[Bresenham's_line_algorithm#Delphi | Delphi]]
 
=={{header|Perl}}==
 
{{libheader|Imlib2}}
 
<syntaxhighlight lang="perl">#! /usr/bin/perl
use strict;
use Image::Imlib2;
Line 2,997 ⟶ 3,036:
 
Images <tt>test0.png</tt> and <tt>test1.png</tt> look different since Imlib2 draw lines with antialiasing.
 
=={{header|Phix}}==
Modified copy of [[Bitmap/Bresenham%27s_line_algorithm#Euphoria|Euphoria]], with a bigger bitmap and a simpler pattern.
Requires new_image() from [[Bitmap#Phix|Bitmap]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br>
Note that demo\rosetta\Bresenham_line.exw is just the last 6 lines below preceded by include ppm.e since that contains bresLine() which is also used by several other examples, and covers the above requirements, as shown commented out. Results may be verified with demo\rosetta\viewppm.exw
<syntaxhighlight lang=Phix"phix">-- demo\rosetta\Bresenham_line.exw (runnable version)
 
global function bresLine(sequence image, integer x0, y0, x1, y1, colour)
Line 3,042 ⟶ 3,080:
screenData = bresLine(screenData,195,1,205,300,blue)
write_ppm("bresenham.ppm",screenData)</syntaxhighlight>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang=PicoLisp"picolisp">(de brez (Img X Y DX DY)
(let SX
(cond
Line 3,083 ⟶ 3,120:
(prinl 120 " " 90)
(mapc prinl Img) ) )</syntaxhighlight>
 
=={{header|PL/I}}==
===version 1===
{{incorrect|PL/I|The sample output does not start at -1/-3!?! Pls show the complete program producing this output.}}
<syntaxhighlight lang=PL"pl/Ii">
/* Draw a line from (x0, y0) to (x1, y1). 13 May 2010 */
/* Based on Rosetta code proforma. */
Line 3,129 ⟶ 3,165:
call draw_line(-1, -3, 6, 10);
for a -10:10 x -10:10 grid:
<syntaxhighlight lang="text">
..........|..........
..........|..........
Line 3,154 ⟶ 3,190:
 
===version 2===
<syntaxhighlight lang=PL"pl/Ii">*process source xref or(!);
brbn:Proc Options(main);
/*********************************************************************
Line 3,223 ⟶ 3,259:
-4 ..|.......
2101234567</pre>
 
=={{header|Prolog}}==
 
 
<syntaxhighlight lang=Prolog"prolog">
:- use_module(bitmap).
:- use_module(bitmapIO).
Line 3,265 ⟶ 3,300:
write_ppm_p6('line.ppm',NB).
</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang=PureBasic"purebasic">Procedure BresenhamLine(x0 ,y0 ,x1 ,y1)
If Abs(y1 - y0) > Abs(x1 - x0);
steep =#True
Line 3,326 ⟶ 3,360:
EndIf
EndIf</syntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|3.1}}
Line 3,332 ⟶ 3,365:
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]] and using the algorithm from the Ada solution:
 
<syntaxhighlight lang="python">def line(self, x0, y0, x1, y1):
"Bresenham's line algorithm"
dx = abs(x1 - x0)
Line 3,394 ⟶ 3,427:
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]].
 
<syntaxhighlight lang="python">
from fractions import Fraction
 
Line 3,412 ⟶ 3,445:
# see test code above
</syntaxhighlight>
 
=={{header|Racket}}==
Port of the Python version.
<syntaxhighlight lang="racket">
#lang racket
(require racket/draw)
Line 3,450 ⟶ 3,482:
bm
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.03}}
Bitmap class from [[Bitmap#Raku|Bitmap]] task.
<syntaxhighlight lang="raku" line>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
Line 3,507 ⟶ 3,538:
}
}</syntaxhighlight>
 
=={{header|RapidQ}}==
 
Use this routine together with the code from [[Basic_bitmap_storage#RapidQ|Basic bitmap storage]] to create a full application.
 
<syntaxhighlight lang="rapidq">SUB draw_line(x1, y1, x2, y2, colour)
x_dist = abs(x2-x1)
y_dist = abs(y2-y1)
Line 3,550 ⟶ 3,580:
Example usage:
 
<syntaxhighlight lang="rapidq">SUB PaintCanvas
draw_line 200, 10, 100, 200, &H00ff00
draw_line 100, 200, 200, 400, &H00ff00
Line 3,556 ⟶ 3,586:
draw_line 300, 200, 200, 10, &H00ff00
END SUB</syntaxhighlight>
 
=={{header|REXX}}==
=== version 1 ===
Line 3,563 ⟶ 3,592:
<br>command line, &nbsp; displays a (background) plot field, &nbsp; uses an infinite field, &nbsp; and
it also handles multiple line segments.
<syntaxhighlight lang="rexx">/*REXX program plots/draws line segments using the Bresenham's line (2D) algorithm. */
parse arg data /*obtain optional arguments from the CL*/
if data='' then data= "(1,8) (8,16) (16,8) (8,1) (1,8)" /* ◄──── a rhombus.*/
Line 3,628 ⟶ 3,657:
 
=== version 2 ===
<syntaxhighlight lang="rexx">/* REXX ***************************************************************
* 21.05.2014 Walter Pachl
* Implementing the pseudo code of
Line 3,690 ⟶ 3,719:
-4 ..|.......
2101234567</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
load "guilib.ring"
load "stdlib.ring"
Line 3,752 ⟶ 3,780:
Output :
[https://lh3.googleusercontent.com/-6uJvON0dAuo/V2LO2zz4zQI/AAAAAAAAALE/IjEGFuhta6oUSeG2QfuxcPBWMmCyNCjdwCLcB/s1600/CalmoSoftBresenham.jpg Bitmap/Bresenham's algorithm]
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">Pixel = Struct.new(:x, :y)
 
class Pixmap
Line 3,802 ⟶ 3,829:
end
bitmap.draw_line(Pixel[10, 10], Pixel[490,490], RGBColour::YELLOW)</syntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang=Rust"rust">
struct Point {
x: i32,
Line 3,892 ⟶ 3,918:
......................................................................
</pre>
 
=={{header|Scala}}==
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class.
<syntaxhighlight lang="scala">object BitmapOps {
def bresenham(bm:RgbBitmap, x0:Int, y0:Int, x1:Int, y1:Int, c:Color)={
val dx=math.abs(x1-x0)
Line 3,919 ⟶ 3,944:
}
}</syntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl}}
<syntaxhighlight lang="ruby">func my_draw_line(img, x0, y0, x1, y1) {
 
var steep = (abs(y1 - y0) > abs(x1 - x0))
Line 3,975 ⟶ 3,999:
 
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}}
ref [[Basic bitmap storage#Tcl]]
<syntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 4,018 ⟶ 4,143:
drawLine $img yellow {20 20} {180 80}
drawLine $img yellow {180 20} {20 80}</syntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
Line 4,025 ⟶ 4,149:
{{trans|E}}
 
<syntaxhighlight lang="ti89b">(lx0, ly0, lx1, ly1)
Prgm
Local steep, x, y, dx, dy, ystep, error, tmp
Line 4,059 ⟶ 4,183:
EndFor
EndPrgm</syntaxhighlight>
 
=={{header|VBScript}}==
{{trans|Rexx}}
<syntaxhighlight lang="vb">'Bitmap/Bresenham's line algorithm - VBScript - 13/05/2019
Dim map(48,40), list(10), ox, oy
data=Array(1,8, 8,16, 16,8, 8,1, 1,8)
Line 4,138 ⟶ 4,261:
...|....................
</pre>
 
=={{header|Vedit macro language}}==
 
<syntaxhighlight lang="vedit">// Daw a line using Bresenham's line algorithm.
// #1=x1, #2=y1; #3=x2, #4=y2
 
Line 4,180 ⟶ 4,302:
Num_Pop(31,35)
return</syntaxhighlight>
 
=={{header|Wart}}==
<syntaxhighlight lang="wart"># doesn't handle vertical lines
def (line x0 y0 x1 y1)
let steep ((> abs) y1-y0 x1-x0)
Line 4,204 ⟶ 4,325:
y += ystep
error += deltax</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
Requires version 1.3.0 of DOME or later.
<syntaxhighlight lang=ecmascript"wren">import "graphics" for Canvas, ImageData, Color
import "dome" for Window
 
Line 4,268 ⟶ 4,388:
=={{header|XPL0}}==
Bresenham line draw is built-in.
<syntaxhighlight lang=XPL0"xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
[SetVid($112); \set 640x480 graphics in 24-bit color
Move(10, 20); \set start of line segment
Line 4,275 ⟶ 4,395:
SetVid(3); \restore normal text mode
]</syntaxhighlight>
 
=={{header|zkl}}==
[[File:Line.zkl.jpg|200px|thumb]]
Algorithm from Wikipedia plus other functions so I can reference this code in other examples.
<syntaxhighlight lang="zkl">ppm:=PPM(200,200,0xFF|FF|FF);
ppm.line(50,100, 100,190, 0);
ppm.line(100,190, 150,100, 0);
Line 4,286 ⟶ 4,405:
 
ppm.writeJPGFile("line.jpg");</syntaxhighlight>
<syntaxhighlight lang="zkl">class PPM{ // (0,0) is logically bottom left
fcn init(width,height,rgb=0){
sz:=width*height;
Line 4,376 ⟶ 4,495:
}
}</syntaxhighlight>
 
{{omit from|AWK}}
{{omit from|PARI/GP}}
 
[[Category:Geometry]]
1,481

edits