Bitmap/Bresenham's line algorithm: Difference between revisions

Content added Content deleted
(Applesoft BASIC)
m (syntax highlighting fixup automation)
Line 10: Line 10:
{{trans|Python}}
{{trans|Python}}


<lang 11l>T Colour
<syntaxhighlight lang=11l>T Colour
Byte r, g, b
Byte r, g, b


Line 85: Line 85:
L(x0, y0, x1, y1) ((1, 8, 8, 16), (8, 16, 16, 8), (16, 8, 8, 1), (8, 1, 1, 8))
L(x0, y0, x1, y1) ((1, 8, 8, 16), (8, 16, 16, 8), (16, 8, 8, 1), (8, 1, 1, 8))
bitmap.line(x0, y0, x1, y1)
bitmap.line(x0, y0, x1, y1)
bitmap.chardisplay()</lang>
bitmap.chardisplay()</syntaxhighlight>


{{out}}
{{out}}
Line 112: Line 112:
=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
{{trans|Rexx}}
{{trans|Rexx}}
<lang 360asm>* Bitmap/Bresenham's line algorithm - 13/05/2019
<syntaxhighlight lang=360asm>* Bitmap/Bresenham's line algorithm - 13/05/2019
BRESENH CSECT
BRESENH CSECT
USING BRESENH,R13 base register
USING BRESENH,R13 base register
Line 299: Line 299:
PG DC CL80' ' buffer
PG DC CL80' ' buffer
REGEQU
REGEQU
END BRESENH </lang>
END BRESENH </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 327: Line 327:
Part of the task is available in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBLINE.ACT RGBLINE.ACT].
Part of the task is available in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBLINE.ACT RGBLINE.ACT].
{{libheader|Action! Bitmap tools}}
{{libheader|Action! Bitmap tools}}
<lang Action!>INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm
<syntaxhighlight lang=Action!>INCLUDE "H6:RGBLINE.ACT" ;from task Bresenham's line algorithm


RGB black,yellow,violet,blue
RGB black,yellow,violet,blue
Line 400: Line 400:
DO UNTIL CH#$FF OD
DO UNTIL CH#$FF OD
CH=$FF
CH=$FF
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bresenham's_line_algorithm.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bresenham's_line_algorithm.png Screenshot from Atari 8-bit computer]


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>procedure Line (Picture : in out Image; Start, Stop : Point; Color : Pixel) is
<syntaxhighlight lang=ada>procedure Line (Picture : in out Image; Start, Stop : Point; Color : Pixel) is
DX : constant Float := abs Float (Stop.X - Start.X);
DX : constant Float := abs Float (Stop.X - Start.X);
DY : constant Float := abs Float (Stop.Y - Start.Y);
DY : constant Float := abs Float (Stop.Y - Start.Y);
Line 444: Line 444:
end if;
end if;
Picture (X, Y) := Color; -- Ensure dots to be drawn
Picture (X, Y) := Color; -- Ensure dots to be drawn
end Line;</lang>
end Line;</syntaxhighlight>
The test program's
The test program's
<lang ada> X : Image (1..16, 1..16);
<syntaxhighlight lang=ada> X : Image (1..16, 1..16);
begin
begin
Fill (X, White);
Fill (X, White);
Line 453: Line 453:
Line (X, (16, 8), ( 8, 1), Black);
Line (X, (16, 8), ( 8, 1), Black);
Line (X, ( 8, 1), ( 1, 8), Black);
Line (X, ( 8, 1), ( 1, 8), Black);
Print (X);</lang>
Print (X);</syntaxhighlight>
sample output
sample output
<pre>
<pre>
Line 479: Line 479:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.6 algol68g-2.6].}}
{{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''.}}
{{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'''<lang algol68># -*- coding: utf-8 -*- #
'''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 OF class image := (REF IMAGE picture, POINT start, stop, PIXEL color)VOID:
Line 520: Line 520:
END # line #;
END # line #;


SKIP</lang>'''File: test/Bitmap/Bresenhams_line_algorithm.a68'''<lang algol68>#!/usr/bin/a68g --script #
SKIP</syntaxhighlight>'''File: test/Bitmap/Bresenhams_line_algorithm.a68'''<syntaxhighlight lang=algol68>#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
# -*- coding: utf-8 -*- #
Line 535: Line 535:
(line OF class image)(x, ( 8, 1), ( 1, 8), black OF class image);
(line OF class image)(x, ( 8, 1), ( 1, 8), black OF class image);
(print OF class image)(x)
(print OF class image)(x)
)</lang>'''Output:'''
)</syntaxhighlight>'''Output:'''
<pre>
<pre>
ffffffffffffffffffffffffffffffffffffffffff000000ffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffff000000ffffffffffffffffffffffffffffffffffffffffffffffff
Line 556: Line 556:


=={{header|Applesoft BASIC}}==
=={{header|Applesoft BASIC}}==
<lang gwbasic> 10 HGR :FULLSCREEN = PEEK (49234)
<syntaxhighlight lang=gwbasic> 10 HGR :FULLSCREEN = PEEK (49234)
20 HCOLOR= 3
20 HCOLOR= 3
30 FOR N = 3 TO 279 STEP 4
30 FOR N = 3 TO 279 STEP 4
Line 578: Line 578:
210 IF E2 < = DX AND Y1 = Y2 THEN RETURN
210 IF E2 < = DX AND Y1 = Y2 THEN RETURN
220 IF E2 < = DX THEN ERR = ERR + DX:Y1 = Y1 + SY
220 IF E2 < = DX THEN ERR = ERR + DX:Y1 = Y1 + SY
230 NEXT WHILE</lang>
230 NEXT WHILE</syntaxhighlight>
=={{header|Assembly}}==
=={{header|Assembly}}==
16 bit Intel 8086\80486 Assembly for dos, see [http://en.wikipedia.org/wiki/X86_assembly_language x86 assembly language].
16 bit Intel 8086\80486 Assembly for dos, see [http://en.wikipedia.org/wiki/X86_assembly_language x86 assembly language].
Line 781: Line 781:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==


<lang AutoHotkey>Blue := Color(0,0,255)
<syntaxhighlight lang=AutoHotkey>Blue := Color(0,0,255)
White := Color(255,255,255)
White := Color(255,255,255)
Bitmap := Bitmap(100,100,Blue) ;create a 100*100 blue bitmap
Bitmap := Bitmap(100,100,Blue) ;create a 100*100 blue bitmap
Line 799: Line 799:
Temp1 := ErrorValue << 1, ((Temp1 > DeltaY) ? (ErrorValue += DeltaY, PosX1 += StepX) : ""), ((Temp1 < DeltaX) ? (ErrorValue += DeltaX, PosY1 += StepY) : "") ;move forward
Temp1 := ErrorValue << 1, ((Temp1 > DeltaY) ? (ErrorValue += DeltaY, PosX1 += StepX) : ""), ((Temp1 < DeltaX) ? (ErrorValue += DeltaX, PosY1 += StepY) : "") ;move forward
}
}
}</lang>
}</syntaxhighlight>


=={{header|AutoIt}}==
=={{header|AutoIt}}==


<lang AutoHotkey>Local $var = drawBresenhamLine(2, 3, 2, 6)
<syntaxhighlight lang=AutoHotkey>Local $var = drawBresenhamLine(2, 3, 2, 6)


Func drawBresenhamLine($iX0, $iY0, $iX1, $iY1)
Func drawBresenhamLine($iX0, $iY0, $iX1, $iY1)
Line 825: Line 825:
EndIf
EndIf
WEnd
WEnd
EndFunc ;==>drawBresenhamLine</lang>
EndFunc ;==>drawBresenhamLine</syntaxhighlight>


=={{header|bash}}==
=={{header|bash}}==
<lang bash>#! /bin/bash
<syntaxhighlight lang=bash>#! /bin/bash


function line {
function line {
Line 890: Line 890:
line $((COLS/2)) $LINS $((COLS/4*3)) $((LINS/2))
line $((COLS/2)) $LINS $((COLS/4*3)) $((LINS/2))
line $((COLS/4*3)) $((LINS/2)) $((COLS/2)) 1
line $((COLS/4*3)) $((LINS/2)) $((COLS/2)) 1
echo -e "\e[${LINS}H"</lang>
echo -e "\e[${LINS}H"</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang qbasic> 1500 REM === Draw a line. Ported from C version
<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
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
1520 DX = ABS(X2 - X1):SX = -1:IF X1 < X2 THEN SX = 1
Line 904: Line 904:
1590 IF E2 > -DX THEN ER = ER - DY:X1 = X1 + SX
1590 IF E2 > -DX THEN ER = ER - DY:X1 = X1 + SX
1600 IF E2 < DY THEN ER = ER + DX:Y1 = Y1 + SY
1600 IF E2 < DY THEN ER = ER + DX:Y1 = Y1 + SY
1610 GOTO 1560</lang>
1610 GOTO 1560</syntaxhighlight>


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>@echo off
<syntaxhighlight lang=dos>@echo off
setlocal enabledelayedexpansion
setlocal enabledelayedexpansion


Line 1,034: Line 1,034:
)
)
)
)
goto :eof</lang>
goto :eof</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> Width% = 200
<syntaxhighlight lang=bbcbasic> Width% = 200
Height% = 200
Height% = 200
Line 1,071: Line 1,071:
GCOL 1
GCOL 1
LINE x%*2,y%*2,x%*2,y%*2
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</lang>
ENDPROC</syntaxhighlight>
[[File:bresenham_bbc.gif]]
[[File:bresenham_bbc.gif]]


Line 1,077: Line 1,077:


Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
<lang C>void line(int x0, int y0, int x1, int y1) {
<syntaxhighlight lang=C>void line(int x0, int y0, int x1, int y1) {


int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
Line 1,090: Line 1,090:
if (e2 < dy) { err += dx; y0 += sy; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Port of the C version.
Port of the C version.
<lang csharp>using System;
<syntaxhighlight lang=csharp>using System;
using System.Drawing;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Imaging;
Line 1,120: Line 1,120:
return bitmap;
return bitmap;
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<syntaxhighlight lang=cpp>
void Line( float x1, float y1, float x2, float y2, const Color& color )
void Line( float x1, float y1, float x2, float y2, const Color& color )
{
{
Line 1,168: Line 1,168:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==


<lang clojure>
<syntaxhighlight lang=clojure>


(defn draw-line
(defn draw-line
Line 1,197: Line 1,197:
(recur (inc x) (+ y y-step) (+ error (- delta-x delta-y)))
(recur (inc x) (+ y y-step) (+ error (- delta-x delta-y)))
(recur (inc x) y (- error delta-y)))))))))))
(recur (inc x) y (- error delta-y)))))))))))
</syntaxhighlight>
</lang>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==


<lang coffeescript>
<syntaxhighlight lang=coffeescript>
drawBresenhamLine = (x0, y0, x1, y1) ->
drawBresenhamLine = (x0, y0, x1, y1) ->
dx = Math.abs(x1 - x0)
dx = Math.abs(x1 - x0)
Line 1,220: Line 1,220:
y0 += sy
y0 += sy
null
null
</syntaxhighlight>
</lang>


=={{header|Commodore BASIC}}==
=={{header|Commodore BASIC}}==
<lang Basic>
<syntaxhighlight lang=Basic>
10 rem bresenham line algorihm
10 rem bresenham line algorihm
20 rem translated from purebasic
20 rem translated from purebasic
Line 1,262: Line 1,262:
2050 if sl<(sw*sh) then poke sc+sl,pc
2050 if sl<(sw*sh) then poke sc+sl,pc
2060 return
2060 return
</syntaxhighlight>
</lang>
[https://www.worldofchris.com/assets/c64-bresenham-line.png C64 Example screenshot]
[https://www.worldofchris.com/assets/c64-bresenham-line.png C64 Example screenshot]


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


<lang lisp>(defun draw-line (buffer x1 y1 x2 y2 pixel)
<syntaxhighlight lang=lisp>(defun draw-line (buffer x1 y1 x2 y2 pixel)
(declare (type rgb-pixel-buffer buffer))
(declare (type rgb-pixel-buffer buffer))
(declare (type integer x1 y1 x2 y2))
(declare (type integer x1 y1 x2 y2))
Line 1,294: Line 1,294:
(incf y y-step)
(incf y y-step)
(incf error delta-x))))
(incf error delta-x))))
buffer))</lang>
buffer))</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
This code uses the Image defined in [[Bitmap]] Task.
This code uses the Image defined in [[Bitmap]] Task.


<lang d>module bitmap_bresenhams_line_algorithm;
<syntaxhighlight lang=d>module bitmap_bresenhams_line_algorithm;


import std.algorithm, std.math, bitmap;
import std.algorithm, std.math, bitmap;
Line 1,350: Line 1,350:
img.textualShow();
img.textualShow();
}
}
}</lang>
}</syntaxhighlight>
To run the demo code compile with <code>-version=bitmap_bresenhams_line_algorithm_main</code>.
To run the demo code compile with <code>-version=bitmap_bresenhams_line_algorithm_main</code>.
{{out}}
{{out}}
Line 1,378: Line 1,378:
=={{header|Delphi}}==
=={{header|Delphi}}==


<lang delphi>
<syntaxhighlight lang=delphi>
procedure drawLine (bitmap : TBitmap; xStart, yStart, xEnd, yEnd : integer; color : TAlphaColor);
procedure drawLine (bitmap : TBitmap; xStart, yStart, xEnd, yEnd : integer; color : TAlphaColor);
// Bresenham's Line Algorithm. Byte, March 1988, pp. 249-253.
// Bresenham's Line Algorithm. Byte, March 1988, pp. 249-253.
Line 1,451: Line 1,451:
end;
end;
end;
end;
</syntaxhighlight>
</lang>


=={{header|E}}==
=={{header|E}}==
Line 1,457: Line 1,457:
{{trans|C}}
{{trans|C}}


<lang e>def swap(&left, &right) { # From [[Generic swap]]
<syntaxhighlight lang=e>def swap(&left, &right) { # From [[Generic swap]]
def t := left
def t := left
left := right
left := right
Line 1,486: Line 1,486:
}
}
}
}
}</lang>
}</syntaxhighlight>


<lang e>def i := makeImage(5, 20)
<syntaxhighlight lang=e>def i := makeImage(5, 20)
drawLine(i, 1, 1, 3, 18, makeColor.fromFloat(0,1,1))
drawLine(i, 1, 1, 3, 18, makeColor.fromFloat(0,1,1))
i.writePPM(<import:java.io.makeFileOutputStream>(<file:~/Desktop/Bresenham.ppm>))</lang>
i.writePPM(<import:java.io.makeFileOutputStream>(<file:~/Desktop/Bresenham.ppm>))</syntaxhighlight>


=={{header|Elm}}==
=={{header|Elm}}==


<lang elm>
<syntaxhighlight lang=elm>




Line 1,551: Line 1,551:
bresenhamLineLoop statics error_ (Position x y) positions_
bresenhamLineLoop statics error_ (Position x y) positions_


</syntaxhighlight>
</lang>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>
<syntaxhighlight lang=erlang>
build_path({Sx, Sy}, {Tx, Ty}) ->
build_path({Sx, Sy}, {Tx, Ty}) ->
if
if
Line 1,600: Line 1,600:
F2 = F0 + Dx,
F2 = F0 + Dx,
through_y({Nx, Ny}, {Tx, Ty}, {StepX, StepY}, {Dx, Dy}, F2, [{Nx, Ny}|P]).
through_y({Nx, Ny}, {Tx, Ty}, {StepX, StepY}, {Dx, Dy}, F2, [{Nx, Ny}|P]).
</syntaxhighlight>
</lang>
OR
OR
<lang erlang>
<syntaxhighlight lang=erlang>
line({X0, Y0}, {X1, Y1}) ->
line({X0, Y0}, {X1, Y1}) ->
SX = step(X0, X1),
SX = step(X0, X1),
Line 1,633: Line 1,633:
next_y(Y, _SY, _DX, E, _DE) ->
next_y(Y, _SY, _DX, E, _DE) ->
{Y, E}.
{Y, E}.
</syntaxhighlight>
</lang>


=={{header|ERRE}}==
=={{header|ERRE}}==
<lang ERRE>
<syntaxhighlight lang=ERRE>
PROGRAM BRESENHAM
PROGRAM BRESENHAM


Line 1,667: Line 1,667:
SCREEN(0)
SCREEN(0)
END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>


=={{header|Euphoria}}==
=={{header|Euphoria}}==
Line 1,673: Line 1,673:
{{trans|C}}
{{trans|C}}


<lang euphoria>include std/console.e
<syntaxhighlight lang=euphoria>include std/console.e
include std/graphics.e
include std/graphics.e
include std/math.e
include std/math.e
Line 1,787: Line 1,787:
--
--
--,respectively in the last if check.
--,respectively in the last if check.
--*/</lang>
--*/</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,811: Line 1,811:


=={{header|F Sharp|F#}}==
=={{header|F Sharp|F#}}==
<lang fsharp>let inline bresenham fill (x0, y0) (x1, y1) =
<syntaxhighlight lang=fsharp>let inline bresenham fill (x0, y0) (x1, y1) =
let steep = abs(y1 - y0) > abs(x1 - x0)
let steep = abs(y1 - y0) > abs(x1 - x0)
let x0, y0, x1, y1 =
let x0, y0, x1, y1 =
Line 1,826: Line 1,826:
else
else
loop (e-dy) (x+1) y
loop (e-dy) (x+1) y
loop (dx/2) x0 y0</lang>
loop (dx/2) x0 y0</syntaxhighlight>
The following program tests the above bresenham function by drawing 100 lines into an image and visualizing the result using
The following program tests the above bresenham function by drawing 100 lines into an image and visualizing the result using
{{libheader|Windows Presentation Foundation}}:
{{libheader|Windows Presentation Foundation}}:
<lang fsharp>open System.Windows
<syntaxhighlight lang=fsharp>open System.Windows
open System.Windows.Media.Imaging
open System.Windows.Media.Imaging


Line 1,845: Line 1,845:
BitmapSource.Create(n, n, 1.0, 1.0, format, null, pixel, n)
BitmapSource.Create(n, n, 1.0, 1.0, format, null, pixel, n)
Window(Content=image, Title="Bresenham's line algorithm")
Window(Content=image, Title="Bresenham's line algorithm")
|> (Application()).Run |> ignore</lang>
|> (Application()).Run |> ignore</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
A very ugly imperative implementation similar to the wikipedia pseudocode..
A very ugly imperative implementation similar to the wikipedia pseudocode..
<lang factor>USING: accessors arrays kernel locals math math.functions
<syntaxhighlight lang=factor>USING: accessors arrays kernel locals math math.functions
math.ranges math.vectors rosettacode.raster.display
math.ranges math.vectors rosettacode.raster.display
rosettacode.raster.storage sequences ui.gadgets ;
rosettacode.raster.storage sequences ui.gadgets ;
Line 1,885: Line 1,885:
: draw-line ( {R,G,B} pt1 pt2 image -- )
: draw-line ( {R,G,B} pt1 pt2 image -- )
[ line-points ] dip
[ line-points ] dip
[ set-pixel ] curry with each ;</lang>
[ set-pixel ] curry with each ;</syntaxhighlight>


=={{header|FBSL}}==
=={{header|FBSL}}==
Line 1,893: Line 1,893:


'''Using pure FBSL's built-in graphics functions:'''
'''Using pure FBSL's built-in graphics functions:'''
<lang qbasic>#DEFINE WM_LBUTTONDOWN 513
<syntaxhighlight lang=qbasic>#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
#DEFINE WM_CLOSE 16


Line 1,927: Line 1,927:
WEND
WEND
END SUB
END SUB
END SUB</lang>
END SUB</syntaxhighlight>
'''Output:''' [[File:FBSLBresenham.PNG]]
'''Output:''' [[File:FBSLBresenham.PNG]]


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>defer steep \ noop or swap
<syntaxhighlight lang=forth>defer steep \ noop or swap
defer ystep \ 1+ or 1-
defer ystep \ 1+ or 1-


Line 1,978: Line 1,978:
** *
** *
**
**
ok</lang>
ok</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
{{trans|C}}
{{trans|C}}
<lang fortran>module RCImagePrimitive
<syntaxhighlight lang=fortran>module RCImagePrimitive
use RCImageBasic
use RCImageBasic


Line 2,052: Line 2,052:
end subroutine draw_line
end subroutine draw_line


end module RCImagePrimitive</lang>
end module RCImagePrimitive</syntaxhighlight>


Usage example:
Usage example:


<lang fortran>program BasicImageTests
<syntaxhighlight lang=fortran>program BasicImageTests
use RCImageBasic
use RCImageBasic
use RCImageIO
use RCImageIO
Line 2,082: Line 2,082:
call free_img(animage)
call free_img(animage)


end program BasicImageTests</lang>
end program BasicImageTests</syntaxhighlight>


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' version 16-09-2015
<syntaxhighlight lang=freebasic>' version 16-09-2015
' compile with: fbc -s console
' compile with: fbc -s console
' OR compile with: fbc -s gui
' OR compile with: fbc -s gui
Line 2,123: Line 2,123:
Loop Until InKey <> "" ' loop until a key is pressed
Loop Until InKey <> "" ' loop until a key is pressed


End</lang>
End</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package raster
<syntaxhighlight lang=go>package raster


// Line draws line by Bresenham's algorithm.
// Line draws line by Bresenham's algorithm.
Line 2,171: Line 2,171:
func (b *Bitmap) LineRgb(x0, y0, x1, y1 int, c Rgb) {
func (b *Bitmap) LineRgb(x0, y0, x1, y1 int, c Rgb) {
b.Line(x0, y0, x1, y1, c.Pixel())
b.Line(x0, y0, x1, y1, c.Pixel())
}</lang>
}</syntaxhighlight>
A demonstration program:
A demonstration program:
<lang go>package main
<syntaxhighlight lang=go>package main


// Files required to build supporting package raster are found in:
// Files required to build supporting package raster are found in:
Line 2,195: Line 2,195:
fmt.Println(err)
fmt.Println(err)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>module Bitmap.Line(line) where
<syntaxhighlight lang=haskell>module Bitmap.Line(line) where


import Bitmap
import Bitmap
Line 2,230: Line 2,230:
deltax = x2 - x1
deltax = x2 - x1
deltay = abs $ y2 - y1
deltay = abs $ y2 - y1
ystep = if y1 < y2 then 1 else -1</lang>
ystep = if y1 < y2 then 1 else -1</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
Line 2,236: Line 2,236:


Using definitions from [[Basic bitmap storage#J|Basic bitmap storage]].
Using definitions from [[Basic bitmap storage#J|Basic bitmap storage]].
<lang j>thru=: <./ + -~ i.@+ _1 ^ > NB. integers from x through y
<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
NB.*getBresenhamLine v Returns points for a line given start and end points
Line 2,251: Line 2,251:
NB.*drawLines v Draws lines (x) on image (y)
NB.*drawLines v Draws lines (x) on image (y)
NB. x is: 2-item list (start and end points) ; (color)
NB. x is: 2-item list (start and end points) ; (color)
drawLines=: (1&{:: ;~ [: ; [: <@getBresenhamLine"2 (0&{::))@[ setPixels ]</lang>
drawLines=: (1&{:: ;~ [: ; [: <@getBresenhamLine"2 (0&{::))@[ setPixels ]</syntaxhighlight>


'''Example Usage:'''
'''Example Usage:'''
<lang j> myimg=: 0 255 0 makeRGB 20 32 NB. 32 by 20 green image
<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
myimg=: ((1 1 ,: 5 11) ; 255 0 0 ) drawLines myimg NB. draw red line from xy point 1 1 to 11 5


Line 2,262: Line 2,262:
viewRGB myimg=: (Diamond;255 0 0) drawLines myimg NB. draw 4 red lines to form a diamond
viewRGB myimg=: (Diamond;255 0 0) drawLines myimg NB. draw 4 red lines to form a diamond
viewRGB myimg=: (Square;0 0 255) drawLines myimg NB. draw 4 blue lines to form a square
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</lang>
viewRGB (Diamond;255 0 0) drawLines (Square;0 0 255) drawLines myimg</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.awt.Color;
<syntaxhighlight lang=java>import java.awt.Color;
import java.awt.Dimension;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics;
Line 2,379: Line 2,379:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
<lang javascript>function bline(x0, y0, x1, y1) {
<syntaxhighlight lang=javascript>function bline(x0, y0, x1, y1) {


var dx = Math.abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
var dx = Math.abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
Line 2,396: Line 2,396:
if (e2 < dy) { err += dx; y0 += sy; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}
}</lang>
}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
{{works with|Julia|0.6}}
<lang Julia>function drawline!(img::Matrix{T}, x0::Int, y0::Int, x1::Int, y1::Int, col::T) where T
<syntaxhighlight lang=Julia>function drawline!(img::Matrix{T}, x0::Int, y0::Int, x1::Int, y1::Int, col::T) where T
δx = abs(x1 - x0)
δx = abs(x1 - x0)
δy = abs(y1 - y0)
δy = abs(y1 - y0)
Line 2,426: Line 2,426:
drawline!(img, 1, 1, 5, 5, Gray(0.0));
drawline!(img, 1, 1, 5, 5, Gray(0.0));
println("\nModified image:")
println("\nModified image:")
display(img); println()</lang>
display(img); println()</syntaxhighlight>


{{out}}
{{out}}
Line 2,448: Line 2,448:
=={{header|Korn Shell}}==
=={{header|Korn Shell}}==


<lang>function line {
<syntaxhighlight lang=text>function line {
x0=$1; y0=$2 x1=$3; y1=$4
x0=$1; y0=$2 x1=$3; y1=$4


Line 2,482: Line 2,482:


done
done
}</lang>
}</syntaxhighlight>


Output from the statement:-
Output from the statement:-
line 0 0 3 4
line 0 0 3 4
(which could be piped to another program)
(which could be piped to another program)
<lang>0 0
<syntaxhighlight lang=text>0 0
1 1
1 1
1 2
1 2
2 3
2 3
3 4</lang>
3 4</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C}}
{{trans|C}}
{{works with|Lua 5.1 (or above, tested on: 5.1.5, 5.2.3, 5.3.5)}}
{{works with|Lua 5.1 (or above, tested on: 5.1.5, 5.2.3, 5.3.5)}}
<lang Lua>
<syntaxhighlight lang=Lua>
-----------------------------------------------
-----------------------------------------------
-- Bitmap replacement
-- Bitmap replacement
Line 2,582: Line 2,582:
bitmap:line(60,10,30,20)
bitmap:line(60,10,30,20)
bitmap:line(30,20,0,10)
bitmap:line(30,20,0,10)
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</lang>
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</syntaxhighlight>
{{out}}<pre>.............................XXX.............................
{{out}}<pre>.............................XXX.............................
..........................XXX...XXX..........................
..........................XXX...XXX..........................
Line 2,608: Line 2,608:
=={{header|Maple}}==
=={{header|Maple}}==


<lang maple>SegmentBresenham := proc (img, x0, y0, x1, y1)
<syntaxhighlight lang=maple>SegmentBresenham := proc (img, x0, y0, x1, y1)
local deltax, deltay, x, y, ystep, steep, err, img2, x02, y02, x12, y12;
local deltax, deltay, x, y, ystep, steep, err, img2, x02, y02, x12, y12;
x02, x12, y02, y12 := y0, y1, x0, x1;
x02, x12, y02, y12 := y0, y1, x0, x1;
Line 2,643: Line 2,643:
end do;
end do;
return img2;
return img2;
end proc:</lang>
end proc:</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Rasterize[Style[Graphics[Line[{{0, 0}, {20, 10}}]], Antialiasing -> False]]</lang>
<syntaxhighlight lang=Mathematica>Rasterize[Style[Graphics[Line[{{0, 0}, {20, 10}}]], Antialiasing -> False]]</syntaxhighlight>


=={{header|MATLAB}}==
=={{header|MATLAB}}==
Note: Store this function in a file named "bresenhamLine.m" in the @Bitmap folder for the Bitmap class defined [[Bitmap#MATLAB|here]].
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.]]
[[File:Bresenham.png|thumb|MATLAB sample usage output.]]
<lang MATLAB>
<syntaxhighlight lang=MATLAB>
%screen = Bitmap object
%screen = Bitmap object
%startPoint = [x0,y0]
%startPoint = [x0,y0]
Line 2,713: Line 2,713:
assignin('caller',inputname(1),screen); %saves the changes to the object
assignin('caller',inputname(1),screen); %saves the changes to the object
end
end
</syntaxhighlight>
</lang>


Sample Usage:
Sample Usage:
<lang MATLAB>
<syntaxhighlight lang=MATLAB>
>> img = Bitmap(800,600);
>> img = Bitmap(800,600);
>> img.bresenhamLine([400 550],[200 400],[255 255 255]);
>> img.bresenhamLine([400 550],[200 400],[255 255 255]);
Line 2,725: Line 2,725:
>> img.bresenhamLine([400 550],[400 150],[255 255 255]);
>> img.bresenhamLine([400 550],[400 150],[255 255 255]);
>> disp(img)
>> disp(img)
</syntaxhighlight>
</lang>


=={{header|MAXScript}}==
=={{header|MAXScript}}==


<lang maxscript>fn plot img coord steep col =
<syntaxhighlight lang=maxscript>fn plot img coord steep col =
(
(
if steep then
if steep then
Line 2,780: Line 2,780:
myBitmap = bitmap 512 512 color:(color 0 0 0)
myBitmap = bitmap 512 512 color:(color 0 0 0)
myBitmap = drawLine myBitmap [0, 511] [511, 0] #((color 255 255 255))
myBitmap = drawLine myBitmap [0, 511] [511, 0] #((color 255 255 255))
display myBitmap</lang>
display myBitmap</syntaxhighlight>


=={{header|Metal}}==
=={{header|Metal}}==
Line 2,786: Line 2,786:
For drawing lines between points in an Apple Metal compute shader.
For drawing lines between points in an Apple Metal compute shader.


<lang metal>void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end);
<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)
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end)
Line 2,824: Line 2,824:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import bitmap
<syntaxhighlight lang=nim>import bitmap


proc drawLine*(img: Image; p, q: Point; color: Color) =
proc drawLine*(img: Image; p, q: Point; color: Color) =
Line 2,861: Line 2,861:
img.drawLine((15, 7), (7, 0), Black)
img.drawLine((15, 7), (7, 0), Black)
img.drawLine((7, 0), (0, 7), Black)
img.drawLine((7, 0), (0, 7), Black)
img.print()</lang>
img.print()</syntaxhighlight>


{{out}}
{{out}}
Line 2,883: Line 2,883:
=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>let draw_line ~img ~color ~p0:(x0,y0) ~p1:(x1,y1) =
<syntaxhighlight lang=ocaml>let draw_line ~img ~color ~p0:(x0,y0) ~p1:(x1,y1) =


let steep = abs(y1 - y0) > abs(x1 - x0) in
let steep = abs(y1 - y0) > abs(x1 - x0) in
Line 2,922: Line 2,922:
in
in
loop x0 y0 error
loop x0 y0 error
;;</lang>
;;</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 2,932: Line 2,932:
{{libheader|Imlib2}}
{{libheader|Imlib2}}


<lang perl>#! /usr/bin/perl
<syntaxhighlight lang=perl>#! /usr/bin/perl
use strict;
use strict;
use Image::Imlib2;
use Image::Imlib2;
Line 2,994: Line 2,994:
$img->save("test1.png");
$img->save("test1.png");


exit 0;</lang>
exit 0;</syntaxhighlight>


Images <tt>test0.png</tt> and <tt>test1.png</tt> look different since Imlib2 draw lines with antialiasing.
Images <tt>test0.png</tt> and <tt>test1.png</tt> look different since Imlib2 draw lines with antialiasing.
Line 3,002: Line 3,002:
Requires new_image() from [[Bitmap#Phix|Bitmap]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br>
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
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
<lang Phix>-- demo\rosetta\Bresenham_line.exw (runnable version)
<syntaxhighlight lang=Phix>-- demo\rosetta\Bresenham_line.exw (runnable version)


global function bresLine(sequence image, integer x0, y0, x1, y1, colour)
global function bresLine(sequence image, integer x0, y0, x1, y1, colour)
Line 3,041: Line 3,041:
screenData = bresLine(screenData,200,1,400,150,white)
screenData = bresLine(screenData,200,1,400,150,white)
screenData = bresLine(screenData,195,1,205,300,blue)
screenData = bresLine(screenData,195,1,205,300,blue)
write_ppm("bresenham.ppm",screenData)</lang>
write_ppm("bresenham.ppm",screenData)</syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de brez (Img X Y DX DY)
<syntaxhighlight lang=PicoLisp>(de brez (Img X Y DX DY)
(let SX
(let SX
(cond
(cond
Line 3,082: Line 3,082:
(prinl "P1")
(prinl "P1")
(prinl 120 " " 90)
(prinl 120 " " 90)
(mapc prinl Img) ) )</lang>
(mapc prinl Img) ) )</syntaxhighlight>


=={{header|PL/I}}==
=={{header|PL/I}}==
===version 1===
===version 1===
{{incorrect|PL/I|The sample output does not start at -1/-3!?! Pls show the complete program producing this output.}}
{{incorrect|PL/I|The sample output does not start at -1/-3!?! Pls show the complete program producing this output.}}
<lang PL/I>
<syntaxhighlight lang=PL/I>
/* Draw a line from (x0, y0) to (x1, y1). 13 May 2010 */
/* Draw a line from (x0, y0) to (x1, y1). 13 May 2010 */
/* Based on Rosetta code proforma. */
/* Based on Rosetta code proforma. */
Line 3,124: Line 3,124:


end draw_line;
end draw_line;
</syntaxhighlight>
</lang>


Output from the statement:-
Output from the statement:-
call draw_line(-1, -3, 6, 10);
call draw_line(-1, -3, 6, 10);
for a -10:10 x -10:10 grid:
for a -10:10 x -10:10 grid:
<lang>
<syntaxhighlight lang=text>
..........|..........
..........|..........
..........|..........
..........|..........
Line 3,151: Line 3,151:
..........|..........
..........|..........
..........|..........
..........|..........
</syntaxhighlight>
</lang>


===version 2===
===version 2===
<lang PL/I>*process source xref or(!);
<syntaxhighlight lang=PL/I>*process source xref or(!);
brbn:Proc Options(main);
brbn:Proc Options(main);
/*********************************************************************
/*********************************************************************
Line 3,204: Line 3,204:
image(x0,y0)='X';
image(x0,y0)='X';
end;
end;
end;</lang>
end;</syntaxhighlight>
'''output'''
'''output'''
<pre>11 ..|.......
<pre>11 ..|.......
Line 3,227: Line 3,227:




<lang Prolog>
<syntaxhighlight lang=Prolog>
:- use_module(bitmap).
:- use_module(bitmap).
:- use_module(bitmapIO).
:- use_module(bitmapIO).
Line 3,264: Line 3,264:
draw_line(NB,B,[0,0,0],2,2,10,90),
draw_line(NB,B,[0,0,0],2,2,10,90),
write_ppm_p6('line.ppm',NB).
write_ppm_p6('line.ppm',NB).
</syntaxhighlight>
</lang>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>Procedure BresenhamLine(x0 ,y0 ,x1 ,y1)
<syntaxhighlight lang=PureBasic>Procedure BresenhamLine(x0 ,y0 ,x1 ,y1)
If Abs(y1 - y0) > Abs(x1 - x0);
If Abs(y1 - y0) > Abs(x1 - x0);
steep =#True
steep =#True
Line 3,325: Line 3,325:
Until Event = #PB_Event_CloseWindow
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
Line 3,332: Line 3,332:
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]] and using the algorithm from the Ada solution:
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]] and using the algorithm from the Ada solution:


<lang python>def line(self, x0, y0, x1, y1):
<syntaxhighlight lang=python>def line(self, x0, y0, x1, y1):
"Bresenham's line algorithm"
"Bresenham's line algorithm"
dx = abs(x1 - x0)
dx = abs(x1 - x0)
Line 3,389: Line 3,389:
| |
| |
+-----------------+
+-----------------+
'''</lang>
'''</syntaxhighlight>


===Not relying on floats===
===Not relying on floats===
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]].
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]].


<lang python>
<syntaxhighlight lang=python>
from fractions import Fraction
from fractions import Fraction


Line 3,411: Line 3,411:


# see test code above
# see test code above
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==
Port of the Python version.
Port of the Python version.
<lang racket>
<syntaxhighlight lang=racket>
#lang racket
#lang racket
(require racket/draw)
(require racket/draw)
Line 3,449: Line 3,449:
(apply draw-line (cons dc points)))
(apply draw-line (cons dc points)))
bm
bm
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
Line 3,455: Line 3,455:
{{works with|Rakudo|2018.03}}
{{works with|Rakudo|2018.03}}
Bitmap class from [[Bitmap#Raku|Bitmap]] task.
Bitmap class from [[Bitmap#Raku|Bitmap]] task.
<lang perl6>class Pixel { has UInt ($.R, $.G, $.B) }
<syntaxhighlight lang=raku line>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
class Bitmap {
has UInt ($.width, $.height);
has UInt ($.width, $.height);
Line 3,506: Line 3,506:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|RapidQ}}==
=={{header|RapidQ}}==
Line 3,512: Line 3,512:
Use this routine together with the code from [[Basic_bitmap_storage#RapidQ|Basic bitmap storage]] to create a full application.
Use this routine together with the code from [[Basic_bitmap_storage#RapidQ|Basic bitmap storage]] to create a full application.


<lang rapidq>SUB draw_line(x1, y1, x2, y2, colour)
<syntaxhighlight lang=rapidq>SUB draw_line(x1, y1, x2, y2, colour)
x_dist = abs(x2-x1)
x_dist = abs(x2-x1)
y_dist = abs(y2-y1)
y_dist = abs(y2-y1)
Line 3,546: Line 3,546:
END IF
END IF
END SUB</lang>
END SUB</syntaxhighlight>


Example usage:
Example usage:


<lang rapidq>SUB PaintCanvas
<syntaxhighlight lang=rapidq>SUB PaintCanvas
draw_line 200, 10, 100, 200, &H00ff00
draw_line 200, 10, 100, 200, &H00ff00
draw_line 100, 200, 200, 400, &H00ff00
draw_line 100, 200, 200, 400, &H00ff00
draw_line 200, 400, 300, 200, &H00ff00
draw_line 200, 400, 300, 200, &H00ff00
draw_line 300, 200, 200, 10, &H00ff00
draw_line 300, 200, 200, 10, &H00ff00
END SUB</lang>
END SUB</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
Line 3,563: Line 3,563:
<br>command line, &nbsp; displays a (background) plot field, &nbsp; uses an infinite field, &nbsp; and
<br>command line, &nbsp; displays a (background) plot field, &nbsp; uses an infinite field, &nbsp; and
it also handles multiple line segments.
it also handles multiple line segments.
<lang rexx>/*REXX program plots/draws line segments using the Bresenham's line (2D) algorithm. */
<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*/
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.*/
if data='' then data= "(1,8) (8,16) (16,8) (8,1) (1,8)" /* ◄──── a rhombus.*/
Line 3,602: Line 3,602:
if err2 > -dy then do; err= err - dy; x= x + sx; end
if err2 > -dy then do; err= err - dy; x= x + sx; end
if err2 < dx then do; err= err + dx; y= y + sy; end
if err2 < dx then do; err= err + dx; y= y + sy; end
end /*forever*/</lang>
end /*forever*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 3,628: Line 3,628:


=== version 2 ===
=== version 2 ===
<lang rexx>/* REXX ***************************************************************
<syntaxhighlight lang=rexx>/* REXX ***************************************************************
* 21.05.2014 Walter Pachl
* 21.05.2014 Walter Pachl
* Implementing the pseudo code of
* Implementing the pseudo code of
Line 3,671: Line 3,671:
end
end
end
end
Return</lang>
Return</syntaxhighlight>
'''output'''
'''output'''
<pre>11 ..|.......
<pre>11 ..|.......
Line 3,692: Line 3,692:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang=ring>
load "guilib.ring"
load "guilib.ring"
load "stdlib.ring"
load "stdlib.ring"
Line 3,749: Line 3,749:
}
}
label1 { setpicture(p1) show() }
label1 { setpicture(p1) show() }
</syntaxhighlight>
</lang>
Output :
Output :
[https://lh3.googleusercontent.com/-6uJvON0dAuo/V2LO2zz4zQI/AAAAAAAAALE/IjEGFuhta6oUSeG2QfuxcPBWMmCyNCjdwCLcB/s1600/CalmoSoftBresenham.jpg Bitmap/Bresenham's algorithm]
[https://lh3.googleusercontent.com/-6uJvON0dAuo/V2LO2zz4zQI/AAAAAAAAALE/IjEGFuhta6oUSeG2QfuxcPBWMmCyNCjdwCLcB/s1600/CalmoSoftBresenham.jpg Bitmap/Bresenham's algorithm]


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>Pixel = Struct.new(:x, :y)
<syntaxhighlight lang=ruby>Pixel = Struct.new(:x, :y)


class Pixmap
class Pixmap
Line 3,801: Line 3,801:
bitmap.draw_line(Pixel[10, 10], Pixel[a,490], RGBColour::YELLOW)
bitmap.draw_line(Pixel[10, 10], Pixel[a,490], RGBColour::YELLOW)
end
end
bitmap.draw_line(Pixel[10, 10], Pixel[490,490], RGBColour::YELLOW)</lang>
bitmap.draw_line(Pixel[10, 10], Pixel[490,490], RGBColour::YELLOW)</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
<lang Rust>
<syntaxhighlight lang=Rust>
struct Point {
struct Point {
x: i32,
x: i32,
Line 3,858: Line 3,858:
}
}
}
}
</syntaxhighlight>
</lang>
'''Output:'''
'''Output:'''
<pre>
<pre>
Line 3,895: Line 3,895:
=={{header|Scala}}==
=={{header|Scala}}==
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class.
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class.
<lang scala>object BitmapOps {
<syntaxhighlight lang=scala>object BitmapOps {
def bresenham(bm:RgbBitmap, x0:Int, y0:Int, x1:Int, y1:Int, c:Color)={
def bresenham(bm:RgbBitmap, x0:Int, y0:Int, x1:Int, y1:Int, c:Color)={
val dx=math.abs(x1-x0)
val dx=math.abs(x1-x0)
Line 3,918: Line 3,918:
bm.setPixel(x, y, c)
bm.setPixel(x, y, c)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<lang ruby>func my_draw_line(img, x0, y0, x1, y1) {
<syntaxhighlight lang=ruby>func my_draw_line(img, x0, y0, x1, y1) {


var steep = (abs(y1 - y0) > abs(x1 - x0))
var steep = (abs(y1 - y0) > abs(x1 - x0))
Line 3,974: Line 3,974:
img.draw_line(80, 10, 10, 80)
img.draw_line(80, 10, 10, 80)


img.save("test1.png")</lang>
img.save("test1.png")</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{libheader|Tk}}
{{libheader|Tk}}
ref [[Basic bitmap storage#Tcl]]
ref [[Basic bitmap storage#Tcl]]
<lang tcl>package require Tcl 8.5
<syntaxhighlight lang=tcl>package require Tcl 8.5
package require Tk
package require Tk


Line 4,017: Line 4,017:
fill $img black
fill $img black
drawLine $img yellow {20 20} {180 80}
drawLine $img yellow {20 20} {180 80}
drawLine $img yellow {180 20} {20 80}</lang>
drawLine $img yellow {180 20} {20 80}</syntaxhighlight>


=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==
Line 4,025: Line 4,025:
{{trans|E}}
{{trans|E}}


<lang ti89b>(lx0, ly0, lx1, ly1)
<syntaxhighlight lang=ti89b>(lx0, ly0, lx1, ly1)
Prgm
Prgm
Local steep, x, y, dx, dy, ystep, error, tmp
Local steep, x, y, dx, dy, ystep, error, tmp
Line 4,058: Line 4,058:
EndIf
EndIf
EndFor
EndFor
EndPrgm</lang>
EndPrgm</syntaxhighlight>


=={{header|VBScript}}==
=={{header|VBScript}}==
{{trans|Rexx}}
{{trans|Rexx}}
<lang vb>'Bitmap/Bresenham's line algorithm - VBScript - 13/05/2019
<syntaxhighlight lang=vb>'Bitmap/Bresenham's line algorithm - VBScript - 13/05/2019
Dim map(48,40), list(10), ox, oy
Dim map(48,40), list(10), ox, oy
data=Array(1,8, 8,16, 16,8, 8,1, 1,8)
data=Array(1,8, 8,16, 16,8, 8,1, 1,8)
Line 4,114: Line 4,114:
If err2< dx Then err=err+dx: y=y+sy
If err2< dx Then err=err+dx: y=y+sy
Loop
Loop
End Sub 'draw_line </lang>
End Sub 'draw_line </syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,141: Line 4,141:
=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==


<lang vedit>// Daw a line using Bresenham's line algorithm.
<syntaxhighlight lang=vedit>// Daw a line using Bresenham's line algorithm.
// #1=x1, #2=y1; #3=x2, #4=y2
// #1=x1, #2=y1; #3=x2, #4=y2


Line 4,179: Line 4,179:
}
}
Num_Pop(31,35)
Num_Pop(31,35)
return</lang>
return</syntaxhighlight>


=={{header|Wart}}==
=={{header|Wart}}==
<lang wart># doesn't handle vertical lines
<syntaxhighlight lang=wart># doesn't handle vertical lines
def (line x0 y0 x1 y1)
def (line x0 y0 x1 y1)
let steep ((> abs) y1-y0 x1-x0)
let steep ((> abs) y1-y0 x1-x0)
Line 4,203: Line 4,203:
when (error < 0)
when (error < 0)
y += ystep
y += ystep
error += deltax</lang>
error += deltax</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|DOME}}
{{libheader|DOME}}
Requires version 1.3.0 of DOME or later.
Requires version 1.3.0 of DOME or later.
<lang ecmascript>import "graphics" for Canvas, ImageData, Color
<syntaxhighlight lang=ecmascript>import "graphics" for Canvas, ImageData, Color
import "dome" for Window
import "dome" for Window


Line 4,264: Line 4,264:


static draw(alpha) {}
static draw(alpha) {}
}</lang>
}</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
Bresenham line draw is built-in.
Bresenham line draw is built-in.
<lang XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
<syntaxhighlight lang=XPL0>include c:\cxpl\codes; \intrinsic 'code' declarations
[SetVid($112); \set 640x480 graphics in 24-bit color
[SetVid($112); \set 640x480 graphics in 24-bit color
Move(10, 20); \set start of line segment
Move(10, 20); \set start of line segment
Line 4,274: Line 4,274:
if ChIn(1) then []; \wait for keystroke while viewing graphic screen
if ChIn(1) then []; \wait for keystroke while viewing graphic screen
SetVid(3); \restore normal text mode
SetVid(3); \restore normal text mode
]</lang>
]</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
[[File:Line.zkl.jpg|200px|thumb]]
[[File:Line.zkl.jpg|200px|thumb]]
Algorithm from Wikipedia plus other functions so I can reference this code in other examples.
Algorithm from Wikipedia plus other functions so I can reference this code in other examples.
<lang zkl>ppm:=PPM(200,200,0xFF|FF|FF);
<syntaxhighlight lang=zkl>ppm:=PPM(200,200,0xFF|FF|FF);
ppm.line(50,100, 100,190, 0);
ppm.line(50,100, 100,190, 0);
ppm.line(100,190, 150,100, 0);
ppm.line(100,190, 150,100, 0);
Line 4,285: Line 4,285:
ppm.line(100,10, 50,100, 0);
ppm.line(100,10, 50,100, 0);


ppm.writeJPGFile("line.jpg");</lang>
ppm.writeJPGFile("line.jpg");</syntaxhighlight>
<lang zkl>class PPM{ // (0,0) is logically bottom left
<syntaxhighlight lang=zkl>class PPM{ // (0,0) is logically bottom left
fcn init(width,height,rgb=0){
fcn init(width,height,rgb=0){
sz:=width*height;
sz:=width*height;
Line 4,375: Line 4,375:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{omit from|AWK}}
{{omit from|AWK}}