Bitmap/Bresenham's line algorithm: Difference between revisions
m
syntax highlighting fixup automation
(Applesoft BASIC) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 10:
{{trans|Python}}
<
Byte r, g, b
Line 85:
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.chardisplay()</
{{out}}
Line 112:
=={{header|360 Assembly}}==
{{trans|Rexx}}
<
BRESENH CSECT
USING BRESENH,R13 base register
Line 299:
PG DC CL80' ' buffer
REGEQU
END BRESENH </
{{out}}
<pre>
Line 327:
Part of the task is available in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBLINE.ACT RGBLINE.ACT].
{{libheader|Action! Bitmap tools}}
<
RGB black,yellow,violet,blue
Line 400:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bresenham's_line_algorithm.png Screenshot from Atari 8-bit computer]
=={{header|Ada}}==
<
DX : constant Float := abs Float (Stop.X - Start.X);
DY : constant Float := abs Float (Stop.Y - Start.Y);
Line 444:
end if;
Picture (X, Y) := Color; -- Ensure dots to be drawn
end Line;</
The test program's
<
begin
Fill (X, White);
Line 453:
Line (X, (16, 8), ( 8, 1), Black);
Line (X, ( 8, 1), ( 1, 8), Black);
Print (X);</
sample output
<pre>
Line 479:
{{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'''<
line OF class image := (REF IMAGE picture, POINT start, stop, PIXEL color)VOID:
Line 520:
END # line #;
SKIP</
# -*- coding: utf-8 -*- #
Line 535:
(line OF class image)(x, ( 8, 1), ( 1, 8), black OF class image);
(print OF class image)(x)
)</
<pre>
ffffffffffffffffffffffffffffffffffffffffff000000ffffffffffffffffffffffffffffffffffffffffffffffff
Line 556:
=={{header|Applesoft BASIC}}==
<
20 HCOLOR= 3
30 FOR N = 3 TO 279 STEP 4
Line 578:
210 IF E2 < = DX AND Y1 = Y2 THEN RETURN
220 IF E2 < = DX THEN ERR = ERR + DX:Y1 = Y1 + SY
230 NEXT WHILE</
=={{header|Assembly}}==
16 bit Intel 8086\80486 Assembly for dos, see [http://en.wikipedia.org/wiki/X86_assembly_language x86 assembly language].
Line 781:
=={{header|AutoHotkey}}==
<
White := Color(255,255,255)
Bitmap := Bitmap(100,100,Blue) ;create a 100*100 blue bitmap
Line 799:
Temp1 := ErrorValue << 1, ((Temp1 > DeltaY) ? (ErrorValue += DeltaY, PosX1 += StepX) : ""), ((Temp1 < DeltaX) ? (ErrorValue += DeltaX, PosY1 += StepY) : "") ;move forward
}
}</
=={{header|AutoIt}}==
<
Func drawBresenhamLine($iX0, $iY0, $iX1, $iY1)
Line 825:
EndIf
WEnd
EndFunc ;==>drawBresenhamLine</
=={{header|bash}}==
<
function line {
Line 890:
line $((COLS/2)) $LINS $((COLS/4*3)) $((LINS/2))
line $((COLS/4*3)) $((LINS/2)) $((COLS/2)) 1
echo -e "\e[${LINS}H"</
=={{header|BASIC}}==
<
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 904:
1590 IF E2 > -DX THEN ER = ER - DY:X1 = X1 + SX
1600 IF E2 < DY THEN ER = ER + DX:Y1 = Y1 + SY
1610 GOTO 1560</
=={{header|Batch File}}==
<
setlocal enabledelayedexpansion
Line 1,034:
)
)
goto :eof</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
Height% = 200
Line 1,071:
GCOL 1
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</
[[File:bresenham_bbc.gif]]
Line 1,077:
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
<
int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
Line 1,090:
if (e2 < dy) { err += dx; y0 += sy; }
}
}</
=={{header|C sharp|C#}}==
Port of the C version.
<
using System.Drawing;
using System.Drawing.Imaging;
Line 1,120:
return bitmap;
}
}</
=={{header|C++}}==
<
void Line( float x1, float y1, float x2, float y2, const Color& color )
{
Line 1,168:
}
}
</syntaxhighlight>
=={{header|Clojure}}==
<
(defn draw-line
Line 1,197:
(recur (inc x) (+ y y-step) (+ error (- delta-x delta-y)))
(recur (inc x) y (- error delta-y)))))))))))
</syntaxhighlight>
=={{header|CoffeeScript}}==
<
drawBresenhamLine = (x0, y0, x1, y1) ->
dx = Math.abs(x1 - x0)
Line 1,220:
y0 += sy
null
</syntaxhighlight>
=={{header|Commodore BASIC}}==
<
10 rem bresenham line algorihm
20 rem translated from purebasic
Line 1,262:
2050 if sl<(sw*sh) then poke sc+sl,pc
2060 return
</syntaxhighlight>
[https://www.worldofchris.com/assets/c64-bresenham-line.png C64 Example screenshot]
=={{header|Common Lisp}}==
<
(declare (type rgb-pixel-buffer buffer))
(declare (type integer x1 y1 x2 y2))
Line 1,294:
(incf y y-step)
(incf error delta-x))))
buffer))</
=={{header|D}}==
This code uses the Image defined in [[Bitmap]] Task.
<
import std.algorithm, std.math, bitmap;
Line 1,350:
img.textualShow();
}
}</
To run the demo code compile with <code>-version=bitmap_bresenhams_line_algorithm_main</code>.
{{out}}
Line 1,378:
=={{header|Delphi}}==
<
procedure drawLine (bitmap : TBitmap; xStart, yStart, xEnd, yEnd : integer; color : TAlphaColor);
// Bresenham's Line Algorithm. Byte, March 1988, pp. 249-253.
Line 1,451:
end;
end;
</syntaxhighlight>
=={{header|E}}==
Line 1,457:
{{trans|C}}
<
def t := left
left := right
Line 1,486:
}
}
}</
<
drawLine(i, 1, 1, 3, 18, makeColor.fromFloat(0,1,1))
i.writePPM(<import:java.io.makeFileOutputStream>(<file:~/Desktop/Bresenham.ppm>))</
=={{header|Elm}}==
<
Line 1,551:
bresenhamLineLoop statics error_ (Position x y) positions_
</syntaxhighlight>
=={{header|Erlang}}==
<
build_path({Sx, Sy}, {Tx, Ty}) ->
if
Line 1,600:
F2 = F0 + Dx,
through_y({Nx, Ny}, {Tx, Ty}, {StepX, StepY}, {Dx, Dy}, F2, [{Nx, Ny}|P]).
</syntaxhighlight>
OR
<
line({X0, Y0}, {X1, Y1}) ->
SX = step(X0, X1),
Line 1,633:
next_y(Y, _SY, _DX, E, _DE) ->
{Y, E}.
</syntaxhighlight>
=={{header|ERRE}}==
<
PROGRAM BRESENHAM
Line 1,667:
SCREEN(0)
END PROGRAM
</syntaxhighlight>
=={{header|Euphoria}}==
Line 1,673:
{{trans|C}}
<
include std/graphics.e
include std/math.e
Line 1,787:
--
--,respectively in the last if check.
--*/</
Output:
<pre>
Line 1,811:
=={{header|F Sharp|F#}}==
<
let steep = abs(y1 - y0) > abs(x1 - x0)
let x0, y0, x1, y1 =
Line 1,826:
else
loop (e-dy) (x+1) y
loop (dx/2) x0 y0</
The following program tests the above bresenham function by drawing 100 lines into an image and visualizing the result using
{{libheader|Windows Presentation Foundation}}:
<
open System.Windows.Media.Imaging
Line 1,845:
BitmapSource.Create(n, n, 1.0, 1.0, format, null, pixel, n)
Window(Content=image, Title="Bresenham's line algorithm")
|> (Application()).Run |> ignore</
=={{header|Factor}}==
A very ugly imperative implementation similar to the wikipedia pseudocode..
<
math.ranges math.vectors rosettacode.raster.display
rosettacode.raster.storage sequences ui.gadgets ;
Line 1,885:
: draw-line ( {R,G,B} pt1 pt2 image -- )
[ line-points ] dip
[ set-pixel ] curry with each ;</
=={{header|FBSL}}==
Line 1,893:
'''Using pure FBSL's built-in graphics functions:'''
<
#DEFINE WM_CLOSE 16
Line 1,927:
WEND
END SUB
END SUB</
'''Output:''' [[File:FBSLBresenham.PNG]]
=={{header|Forth}}==
<
defer ystep \ 1+ or 1-
Line 1,978:
** *
**
ok</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{trans|C}}
<
use RCImageBasic
Line 2,052:
end subroutine draw_line
end module RCImagePrimitive</
Usage example:
<
use RCImageBasic
use RCImageIO
Line 2,082:
call free_img(animage)
end program BasicImageTests</
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
' OR compile with: fbc -s gui
Line 2,123:
Loop Until InKey <> "" ' loop until a key is pressed
End</
=={{header|Go}}==
<
// Line draws line by Bresenham's algorithm.
Line 2,171:
func (b *Bitmap) LineRgb(x0, y0, x1, y1 int, c Rgb) {
b.Line(x0, y0, x1, y1, c.Pixel())
}</
A demonstration program:
<
// Files required to build supporting package raster are found in:
Line 2,195:
fmt.Println(err)
}
}</
=={{header|Haskell}}==
<
import Bitmap
Line 2,230:
deltax = x2 - x1
deltay = abs $ y2 - y1
ystep = if y1 < y2 then 1 else -1</
=={{header|J}}==
Line 2,236:
Using definitions from [[Basic bitmap storage#J|Basic bitmap storage]].
<
NB.*getBresenhamLine v Returns points for a line given start and end points
Line 2,251:
NB.*drawLines v Draws lines (x) on image (y)
NB. x is: 2-item list (start and end points) ; (color)
drawLines=: (1&{:: ;~ [: ; [: <@getBresenhamLine"2 (0&{::))@[ setPixels ]</
'''Example Usage:'''
<
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:
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 (Diamond;255 0 0) drawLines (Square;0 0 255) drawLines myimg</
=={{header|Java}}==
<
import java.awt.Dimension;
import java.awt.Graphics;
Line 2,379:
}
}
}</
=={{header|JavaScript}}==
Instead of swaps in the initialisation use error calculation for both directions x and y simultaneously:
<
var dx = Math.abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
Line 2,396:
if (e2 < dy) { err += dx; y0 += sy; }
}
}</
=={{header|Julia}}==
{{works with|Julia|0.6}}
<
δx = abs(x1 - x0)
δy = abs(y1 - y0)
Line 2,426:
drawline!(img, 1, 1, 5, 5, Gray(0.0));
println("\nModified image:")
display(img); println()</
{{out}}
Line 2,448:
=={{header|Korn Shell}}==
<syntaxhighlight lang=text>function line {
x0=$1; y0=$2 x1=$3; y1=$4
Line 2,482:
done
}</
Output from the statement:-
line 0 0 3 4
(which could be piped to another program)
<syntaxhighlight lang=text>0 0
1 1
1 2
2 3
3 4</
=={{header|Lua}}==
{{trans|C}}
{{works with|Lua 5.1 (or above, tested on: 5.1.5, 5.2.3, 5.3.5)}}
<
-----------------------------------------------
-- Bitmap replacement
Line 2,582:
bitmap:line(60,10,30,20)
bitmap:line(30,20,0,10)
bitmap:render({[0x000000]='.', [0xFFFFFFFF]='X'})</
{{out}}<pre>.............................XXX.............................
..........................XXX...XXX..........................
Line 2,608:
=={{header|Maple}}==
<
local deltax, deltay, x, y, ystep, steep, err, img2, x02, y02, x12, y12;
x02, x12, y02, y12 := y0, y1, x0, x1;
Line 2,643:
end do;
return img2;
end proc:</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
=={{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.]]
<
%screen = Bitmap object
%startPoint = [x0,y0]
Line 2,713:
assignin('caller',inputname(1),screen); %saves the changes to the object
end
</syntaxhighlight>
Sample Usage:
<
>> img = Bitmap(800,600);
>> img.bresenhamLine([400 550],[200 400],[255 255 255]);
Line 2,725:
>> img.bresenhamLine([400 550],[400 150],[255 255 255]);
>> disp(img)
</syntaxhighlight>
=={{header|MAXScript}}==
<
(
if steep then
Line 2,780:
myBitmap = bitmap 512 512 color:(color 0 0 0)
myBitmap = drawLine myBitmap [0, 511] [511, 0] #((color 255 255 255))
display myBitmap</
=={{header|Metal}}==
Line 2,786:
For drawing lines between points in an Apple Metal compute shader.
<
void drawLine(texture2d<float, access::write> targetTexture, uint2 start, uint2 end)
Line 2,824:
}
}
}</
=={{header|Nim}}==
<
proc drawLine*(img: Image; p, q: Point; color: Color) =
Line 2,861:
img.drawLine((15, 7), (7, 0), Black)
img.drawLine((7, 0), (0, 7), Black)
img.print()</
{{out}}
Line 2,883:
=={{header|OCaml}}==
<
let steep = abs(y1 - y0) > abs(x1 - x0) in
Line 2,922:
in
loop x0 y0 error
;;</
=={{header|Pascal}}==
Line 2,932:
{{libheader|Imlib2}}
<
use strict;
use Image::Imlib2;
Line 2,994:
$img->save("test1.png");
exit 0;</
Images <tt>test0.png</tt> and <tt>test1.png</tt> look different since Imlib2 draw lines with antialiasing.
Line 3,002:
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
<
global function bresLine(sequence image, integer x0, y0, x1, y1, colour)
Line 3,041:
screenData = bresLine(screenData,200,1,400,150,white)
screenData = bresLine(screenData,195,1,205,300,blue)
write_ppm("bresenham.ppm",screenData)</
=={{header|PicoLisp}}==
<
(let SX
(cond
Line 3,082:
(prinl "P1")
(prinl 120 " " 90)
(mapc prinl Img) ) )</
=={{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.}}
<
/* Draw a line from (x0, y0) to (x1, y1). 13 May 2010 */
/* Based on Rosetta code proforma. */
Line 3,124:
end draw_line;
</syntaxhighlight>
Output from the statement:-
call draw_line(-1, -3, 6, 10);
for a -10:10 x -10:10 grid:
<syntaxhighlight lang=text>
..........|..........
..........|..........
Line 3,151:
..........|..........
..........|..........
</syntaxhighlight>
===version 2===
<
brbn:Proc Options(main);
/*********************************************************************
Line 3,204:
image(x0,y0)='X';
end;
end;</
'''output'''
<pre>11 ..|.......
Line 3,227:
<
:- use_module(bitmap).
:- use_module(bitmapIO).
Line 3,264:
draw_line(NB,B,[0,0,0],2,2,10,90),
write_ppm_p6('line.ppm',NB).
</syntaxhighlight>
=={{header|PureBasic}}==
<
If Abs(y1 - y0) > Abs(x1 - x0);
steep =#True
Line 3,325:
Until Event = #PB_Event_CloseWindow
EndIf
EndIf</
=={{header|Python}}==
Line 3,332:
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]] and using the algorithm from the Ada solution:
<
"Bresenham's line algorithm"
dx = abs(x1 - x0)
Line 3,389:
| |
+-----------------+
'''</
===Not relying on floats===
Extending the example given [[Basic_bitmap_storage/Python#Alternative_version|here]].
<
from fractions import Fraction
Line 3,411:
# see test code above
</syntaxhighlight>
=={{header|Racket}}==
Port of the Python version.
<
#lang racket
(require racket/draw)
Line 3,449:
(apply draw-line (cons dc points)))
bm
</syntaxhighlight>
=={{header|Raku}}==
Line 3,455:
{{works with|Rakudo|2018.03}}
Bitmap class from [[Bitmap#Raku|Bitmap]] task.
<syntaxhighlight lang=raku
class Bitmap {
has UInt ($.width, $.height);
Line 3,506:
}
}
}</
=={{header|RapidQ}}==
Line 3,512:
Use this routine together with the code from [[Basic_bitmap_storage#RapidQ|Basic bitmap storage]] to create a full application.
<
x_dist = abs(x2-x1)
y_dist = abs(y2-y1)
Line 3,546:
END IF
END SUB</
Example usage:
<
draw_line 200, 10, 100, 200, &H00ff00
draw_line 100, 200, 200, 400, &H00ff00
draw_line 200, 400, 300, 200, &H00ff00
draw_line 300, 200, 200, 10, &H00ff00
END SUB</
=={{header|REXX}}==
Line 3,563:
<br>command line, displays a (background) plot field, uses an infinite field, and
it also handles multiple line segments.
<
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,602:
if err2 > -dy then do; err= err - dy; x= x + sx; end
if err2 < dx then do; err= err + dx; y= y + sy; end
end /*forever*/</
{{out|output|text= when using the default input:}}
<pre>
Line 3,628:
=== version 2 ===
<
* 21.05.2014 Walter Pachl
* Implementing the pseudo code of
Line 3,671:
end
end
Return</
'''output'''
<pre>11 ..|.......
Line 3,692:
=={{header|Ring}}==
<
load "guilib.ring"
load "stdlib.ring"
Line 3,749:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
Output :
[https://lh3.googleusercontent.com/-6uJvON0dAuo/V2LO2zz4zQI/AAAAAAAAALE/IjEGFuhta6oUSeG2QfuxcPBWMmCyNCjdwCLcB/s1600/CalmoSoftBresenham.jpg Bitmap/Bresenham's algorithm]
=={{header|Ruby}}==
<
class Pixmap
Line 3,801:
bitmap.draw_line(Pixel[10, 10], Pixel[a,490], RGBColour::YELLOW)
end
bitmap.draw_line(Pixel[10, 10], Pixel[490,490], RGBColour::YELLOW)</
=={{header|Rust}}==
<
struct Point {
x: i32,
Line 3,858:
}
}
</syntaxhighlight>
'''Output:'''
<pre>
Line 3,895:
=={{header|Scala}}==
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class.
<
def bresenham(bm:RgbBitmap, x0:Int, y0:Int, x1:Int, y1:Int, c:Color)={
val dx=math.abs(x1-x0)
Line 3,918:
bm.setPixel(x, y, c)
}
}</
=={{header|Sidef}}==
{{trans|Perl}}
<
var steep = (abs(y1 - y0) > abs(x1 - x0))
Line 3,974:
img.draw_line(80, 10, 10, 80)
img.save("test1.png")</
=={{header|Tcl}}==
{{libheader|Tk}}
ref [[Basic bitmap storage#Tcl]]
<
package require Tk
Line 4,017:
fill $img black
drawLine $img yellow {20 20} {180 80}
drawLine $img yellow {180 20} {20 80}</
=={{header|TI-89 BASIC}}==
Line 4,025:
{{trans|E}}
<
Prgm
Local steep, x, y, dx, dy, ystep, error, tmp
Line 4,058:
EndIf
EndFor
EndPrgm</
=={{header|VBScript}}==
{{trans|Rexx}}
<
Dim map(48,40), list(10), ox, oy
data=Array(1,8, 8,16, 16,8, 8,1, 1,8)
Line 4,114:
If err2< dx Then err=err+dx: y=y+sy
Loop
End Sub 'draw_line </
{{out}}
<pre>
Line 4,141:
=={{header|Vedit macro language}}==
<
// #1=x1, #2=y1; #3=x2, #4=y2
Line 4,179:
}
Num_Pop(31,35)
return</
=={{header|Wart}}==
<
def (line x0 y0 x1 y1)
let steep ((> abs) y1-y0 x1-x0)
Line 4,203:
when (error < 0)
y += ystep
error += deltax</
=={{header|Wren}}==
{{libheader|DOME}}
Requires version 1.3.0 of DOME or later.
<
import "dome" for Window
Line 4,264:
static draw(alpha) {}
}</
=={{header|XPL0}}==
Bresenham line draw is built-in.
<
[SetVid($112); \set 640x480 graphics in 24-bit color
Move(10, 20); \set start of line segment
Line 4,274:
if ChIn(1) then []; \wait for keystroke while viewing graphic screen
SetVid(3); \restore normal text mode
]</
=={{header|zkl}}==
[[File:Line.zkl.jpg|200px|thumb]]
Algorithm from Wikipedia plus other functions so I can reference this code in other examples.
<
ppm.line(50,100, 100,190, 0);
ppm.line(100,190, 150,100, 0);
Line 4,285:
ppm.line(100,10, 50,100, 0);
ppm.writeJPGFile("line.jpg");</
<
fcn init(width,height,rgb=0){
sz:=width*height;
Line 4,375:
}
}
}</
{{omit from|AWK}}
|