Bitmap/Midpoint circle algorithm: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 1: | Line 1: | ||
⚫ | |||
{{task|Raster graphics operations}} |
{{task|Raster graphics operations}} |
||
Line 7: | Line 8: | ||
([[wp:Midpoint_circle_algorithm|definition on Wikipedia]]). |
([[wp:Midpoint_circle_algorithm|definition on Wikipedia]]). |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=11l>T Colour |
<syntaxhighlight lang="11l">T Colour |
||
Byte r, g, b |
Byte r, g, b |
||
Line 118: | Line 118: | ||
+-------------------------+ |
+-------------------------+ |
||
</pre> |
</pre> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
Part of the task is available in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBCIRCL.ACT RGBCIRCL.ACT]. |
Part of the task is available in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBCIRCL.ACT RGBCIRCL.ACT]. |
||
{{libheader|Action! Bitmap tools}} |
{{libheader|Action! Bitmap tools}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="action!">INCLUDE "H6:RGBCIRCL.ACT" ;from task Midpoint circle algorithm |
||
RGB black,yellow,violet,blue |
RGB black,yellow,violet,blue |
||
Line 187: | Line 186: | ||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Midpoint_circle_algorithm.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Midpoint_circle_algorithm.png Screenshot from Atari 8-bit computer] |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang=ada>procedure Circle |
<syntaxhighlight lang="ada">procedure Circle |
||
( Picture : in out Image; |
( Picture : in out Image; |
||
Center : Point; |
Center : Point; |
||
Line 225: | Line 223: | ||
end Circle;</syntaxhighlight> |
end Circle;</syntaxhighlight> |
||
The following illustrates use: |
The following illustrates use: |
||
<syntaxhighlight 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 249: | Line 247: | ||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
Line 255: | Line 252: | ||
{{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/Midpoint_circle_algorithm.a68'''<syntaxhighlight lang=algol68># -*- coding: utf-8 -*- # |
'''File: prelude/Bitmap/Midpoint_circle_algorithm.a68'''<syntaxhighlight lang="algol68"># -*- coding: utf-8 -*- # |
||
circle OF class image := |
circle OF class image := |
||
Line 291: | Line 288: | ||
END # circle #; |
END # circle #; |
||
SKIP</syntaxhighlight>'''File: test/Bitmap/Midpoint_circle_algorithm.a68'''<syntaxhighlight lang=algol68>#!/usr/bin/a68g --script # |
SKIP</syntaxhighlight>'''File: test/Bitmap/Midpoint_circle_algorithm.a68'''<syntaxhighlight lang="algol68">#!/usr/bin/a68g --script # |
||
# -*- coding: utf-8 -*- # |
# -*- coding: utf-8 -*- # |
||
Line 326: | Line 323: | ||
</pre> |
</pre> |
||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
<syntaxhighlight lang=gwbasic> 10 GR : GOSUB 330: END |
<syntaxhighlight lang="gwbasic"> 10 GR : GOSUB 330: END |
||
330 COLOR= 15:CX = 20:CY = 20:R = 18: GOSUB 350"CIRCLE" |
330 COLOR= 15:CX = 20:CY = 20:R = 18: GOSUB 350"CIRCLE" |
||
340 COLOR= 0:CX = 15:CY = 15:R = 6 |
340 COLOR= 0:CX = 15:CY = 15:R = 6 |
||
Line 333: | Line 330: | ||
370 X = X + 1:DX = DX + 2:F = F + DX + 1: HLIN CX - X,CX + X AT CY + Y: HLIN CX - X,CX + X AT CY - Y: HLIN CX - Y,CX + Y AT CY + X: HLIN CX - Y,CX + Y AT CY - X:I = X > = Y: NEXT I: RETURN</syntaxhighlight> |
370 X = X + 1:DX = DX + 2:F = F + DX + 1: HLIN CX - X,CX + X AT CY + Y: HLIN CX - X,CX + X AT CY - Y: HLIN CX - Y,CX + Y AT CY + X: HLIN CX - Y,CX + Y AT CY - X:I = X > = Y: NEXT I: RETURN</syntaxhighlight> |
||
=={{header|bash}}== |
=={{header|bash}}== |
||
<syntaxhighlight lang=bash>#! /bin/bash |
<syntaxhighlight lang="bash">#! /bin/bash |
||
# Based on https://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
# Based on https://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
||
Line 412: | Line 409: | ||
----##---------##---- |
----##---------##---- |
||
------#########------</pre> |
------#########------</pre> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
<syntaxhighlight lang=basic256>fastgraphics |
<syntaxhighlight lang="basic256">fastgraphics |
||
clg |
clg |
||
color red |
color red |
||
Line 455: | Line 451: | ||
{{Out}} |
{{Out}} |
||
[http://s16.postimg.org/ge0ndfs9h/Output.jpg http://s16.postimg.org/ge0ndfs9h/Output.jpg] |
[http://s16.postimg.org/ge0ndfs9h/Output.jpg http://s16.postimg.org/ge0ndfs9h/Output.jpg] |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
<syntaxhighlight lang=dos>@echo off |
<syntaxhighlight lang="dos">@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
Line 561: | Line 556: | ||
███████ |
███████ |
||
</pre> |
</pre> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
[[Image:circle_bbc.gif|right]] |
[[Image:circle_bbc.gif|right]] |
||
<syntaxhighlight lang=bbcbasic> Width% = 200 |
<syntaxhighlight lang="bbcbasic"> Width% = 200 |
||
Height% = 200 |
Height% = 200 |
||
Line 608: | Line 602: | ||
LINE x%*2,y%*2,x%*2,y%*2 |
LINE x%*2,y%*2,x%*2,y%*2 |
||
ENDPROC</syntaxhighlight> |
ENDPROC</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Interface: |
Interface: |
||
<syntaxhighlight lang=c>void raster_circle( |
<syntaxhighlight lang="c">void raster_circle( |
||
image img, |
image img, |
||
unsigned int x0, |
unsigned int x0, |
||
Line 624: | Line 617: | ||
Implementation: |
Implementation: |
||
<syntaxhighlight lang=c>#define plot(x, y) put_pixel_clip(img, x, y, r, g, b) |
<syntaxhighlight lang="c">#define plot(x, y) put_pixel_clip(img, x, y, r, g, b) |
||
void raster_circle( |
void raster_circle( |
||
Line 668: | Line 661: | ||
} |
} |
||
#undef plot</syntaxhighlight> |
#undef plot</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
This extension method extends GenericImage which is very similar to [http://rosettacode.org/wiki/Bitmap#C.23 Bitmap] but instead of using a SetPixel method it uses a "Color this[int x, int y] { get; set; }" property to get and set pixels. |
This extension method extends GenericImage which is very similar to [http://rosettacode.org/wiki/Bitmap#C.23 Bitmap] but instead of using a SetPixel method it uses a "Color this[int x, int y] { get; set; }" property to get and set pixels. |
||
<syntaxhighlight lang=csharp> |
<syntaxhighlight lang="csharp"> |
||
/// <summary> |
/// <summary> |
||
/// Draws a circle. |
/// Draws a circle. |
||
Line 723: | Line 715: | ||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Based upon the Common Lisp version. |
Based upon the Common Lisp version. |
||
<syntaxhighlight lang=clojure>(defn draw-circle [draw-function x0 y0 radius] |
<syntaxhighlight lang="clojure">(defn draw-circle [draw-function x0 y0 radius] |
||
(letfn [(put [x y m] |
(letfn [(put [x y m] |
||
(let [x+ (+ x0 x) |
(let [x+ (+ x0 x) |
||
Line 752: | Line 743: | ||
(+ m 4 (* 8 x)))))))] |
(+ m 4 (* 8 x)))))))] |
||
(put 0 radius (- 5 (* 4 radius)))))</syntaxhighlight> |
(put 0 radius (- 5 (* 4 radius)))))</syntaxhighlight> |
||
<syntaxhighlight lang=clojure>(let [circle-points (atom [])] |
<syntaxhighlight lang="clojure">(let [circle-points (atom [])] |
||
(letfn [(draw-fn [x y] |
(letfn [(draw-fn [x y] |
||
(swap! circle-points #(conj % [x y])))] |
(swap! circle-points #(conj % [x y])))] |
||
Line 784: | Line 775: | ||
nil |
nil |
||
</pre> |
</pre> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Based upon the OCaml version. |
Based upon the OCaml version. |
||
<syntaxhighlight lang=lisp>(defun draw-circle (draw-function x0 y0 radius) |
<syntaxhighlight lang="lisp">(defun draw-circle (draw-function x0 y0 radius) |
||
(labels ((foo (x y) |
(labels ((foo (x y) |
||
(funcall draw-function x y)) |
(funcall draw-function x y)) |
||
Line 817: | Line 807: | ||
(put 0 radius (- 5 (* 4 radius))) |
(put 0 radius (- 5 (* 4 radius))) |
||
(values)))</syntaxhighlight> |
(values)))</syntaxhighlight> |
||
<syntaxhighlight lang=lisp>CL-USER> (let ((buffer (make-array '(30 30) |
<syntaxhighlight lang="lisp">CL-USER> (let ((buffer (make-array '(30 30) |
||
:element-type 'bit))) |
:element-type 'bit))) |
||
(draw-circle (lambda (x y) |
(draw-circle (lambda (x y) |
||
Line 847: | Line 837: | ||
( )) |
( )) |
||
</pre> |
</pre> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Uses the bitmap module from the Bitmap Task. |
Uses the bitmap module from the Bitmap Task. |
||
<syntaxhighlight lang=d>import bitmap: Image, RGB; |
<syntaxhighlight lang="d">import bitmap: Image, RGB; |
||
void circle(Color)(Image!Color img, in int x0, in int y0, |
void circle(Color)(Image!Color img, in int x0, in int y0, |
||
Line 917: | Line 906: | ||
.......##.......##....... |
.......##.......##....... |
||
.........#######.........</pre> |
.........#######.........</pre> |
||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="erre">PROGRAM BCircle |
||
!$INCLUDE="PC.LIB" |
!$INCLUDE="PC.LIB" |
||
Line 957: | Line 945: | ||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
'''Using pure FBSL's built-in graphics functions:''' |
'''Using pure FBSL's built-in graphics functions:''' |
||
<syntaxhighlight lang=qbasic>#DEFINE WM_LBUTTONDOWN 513 |
<syntaxhighlight lang="qbasic">#DEFINE WM_LBUTTONDOWN 513 |
||
#DEFINE WM_CLOSE 16 |
#DEFINE WM_CLOSE 16 |
||
Line 1,002: | Line 989: | ||
END SUB</syntaxhighlight> |
END SUB</syntaxhighlight> |
||
'''Ouptut:''' [[File:FBSLMidpoint.PNG]] |
'''Ouptut:''' [[File:FBSLMidpoint.PNG]] |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<syntaxhighlight lang=forth>: circle { x y r color bmp -- } |
<syntaxhighlight lang="forth">: circle { x y r color bmp -- } |
||
1 r - 0 r 2* negate 0 r { f ddx ddy dx dy } |
1 r - 0 r 2* negate 0 r { f ddx ddy dx dy } |
||
color x y r + bmp b! |
color x y r + bmp b! |
||
Line 1,033: | Line 1,019: | ||
6 6 5 blue test circle |
6 6 5 blue test circle |
||
test bshow cr</syntaxhighlight> |
test bshow cr</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
This code should be inside <tt>RCImagePrimitive</tt> (see [[Bresenham's line algorithm#Fortran|here]]). The private subroutine <code>draw_circle_toch</code>, which writes to a ''channel'', is used by both <code>draw_circle_rgb</code> and <code>draw_circle_sc</code> and the interface allows to use <code>draw_circle</code> with ''[[Basic bitmap storage#Fortran|rgb]]'' images and [[Grayscale image#Fortran|grayscale images]]. |
This code should be inside <tt>RCImagePrimitive</tt> (see [[Bresenham's line algorithm#Fortran|here]]). The private subroutine <code>draw_circle_toch</code>, which writes to a ''channel'', is used by both <code>draw_circle_rgb</code> and <code>draw_circle_sc</code> and the interface allows to use <code>draw_circle</code> with ''[[Basic bitmap storage#Fortran|rgb]]'' images and [[Grayscale image#Fortran|grayscale images]]. |
||
<syntaxhighlight lang=fortran>interface draw_circle |
<syntaxhighlight lang="fortran">interface draw_circle |
||
module procedure draw_circle_sc, draw_circle_rgb |
module procedure draw_circle_sc, draw_circle_rgb |
||
end interface |
end interface |
||
Line 1,044: | Line 1,029: | ||
private :: plot, draw_circle_toch</syntaxhighlight> |
private :: plot, draw_circle_toch</syntaxhighlight> |
||
<syntaxhighlight lang=fortran>subroutine plot(ch, p, v) |
<syntaxhighlight lang="fortran">subroutine plot(ch, p, v) |
||
integer, dimension(:,:), intent(out) :: ch |
integer, dimension(:,:), intent(out) :: ch |
||
type(point), intent(in) :: p |
type(point), intent(in) :: p |
||
Line 1,118: | Line 1,103: | ||
call draw_circle_toch(img%channel, c, radius, lum) |
call draw_circle_toch(img%channel, c, radius, lum) |
||
end subroutine draw_circle_sc</syntaxhighlight> |
end subroutine draw_circle_sc</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="freebasic">' version 15-10-2016 |
||
' compile with: fbc -s gui |
' compile with: fbc -s gui |
||
Line 1,174: | Line 1,158: | ||
Sleep |
Sleep |
||
End</syntaxhighlight> |
End</syntaxhighlight> |
||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
FB has native functions that handle bitmap calculations. This compiles as a stand-alone Macintosh app that allows the user to adjust the screen-centered bitmap width from a single pixel to a large circle. |
FB has native functions that handle bitmap calculations. This compiles as a stand-alone Macintosh app that allows the user to adjust the screen-centered bitmap width from a single pixel to a large circle. |
||
<syntaxhighlight lang=futurebasic> |
<syntaxhighlight lang="futurebasic"> |
||
_wndW = 600 |
_wndW = 600 |
||
_wndH = 600 |
_wndH = 600 |
||
Line 1,260: | Line 1,242: | ||
HandleEvents |
HandleEvents |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
This produces identical results to the C code in the WP article, but with more compact code. |
This produces identical results to the C code in the WP article, but with more compact code. |
||
<syntaxhighlight lang=go>package raster |
<syntaxhighlight lang="go">package raster |
||
// Circle plots a circle with center x, y and radius r. |
// Circle plots a circle with center x, y and radius r. |
||
Line 1,300: | Line 1,281: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Demonstration program: |
Demonstration program: |
||
<syntaxhighlight lang=text>package main |
<syntaxhighlight lang="text">package main |
||
// Files required to build supporting package raster are found in: |
// Files required to build supporting package raster are found in: |
||
Line 1,321: | Line 1,302: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
The basic algorithm can be implemented generically. |
The basic algorithm can be implemented generically. |
||
<syntaxhighlight lang=haskell>module Circle where |
<syntaxhighlight lang="haskell">module Circle where |
||
import Data.List |
import Data.List |
||
Line 1,354: | Line 1,334: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
An example using regular 2d arrays of characters to represent a bitmap: |
An example using regular 2d arrays of characters to represent a bitmap: |
||
<syntaxhighlight lang=haskell>module CircleArrayExample where |
<syntaxhighlight lang="haskell">module CircleArrayExample where |
||
import Circle |
import Circle |
||
Line 1,387: | Line 1,367: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
Using the Image type from the Bitmap module defined [[Basic_bitmap_storage|here]]: |
Using the Image type from the Bitmap module defined [[Basic_bitmap_storage|here]]: |
||
<syntaxhighlight lang=haskell>module CircleBitmapExample where |
<syntaxhighlight lang="haskell">module CircleBitmapExample where |
||
import Circle |
import Circle |
||
Line 1,399: | Line 1,379: | ||
return image |
return image |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:'''<br> |
'''Solution:'''<br> |
||
Using definitions from [[Basic bitmap storage#J|Basic bitmap storage]]. (Note that viewRGB is at the bottom of the entry - separate from the rest of the definitions.) |
Using definitions from [[Basic bitmap storage#J|Basic bitmap storage]]. (Note that viewRGB is at the bottom of the entry - separate from the rest of the definitions.) |
||
<syntaxhighlight lang=j>NB.*getBresenhamCircle v Returns points for a circle given center and radius |
<syntaxhighlight lang="j">NB.*getBresenhamCircle v Returns points for a circle given center and radius |
||
NB. y is: y0 x0 radius |
NB. y is: y0 x0 radius |
||
getBresenhamCircle=: monad define |
getBresenhamCircle=: monad define |
||
Line 1,429: | Line 1,408: | ||
'''Example usage:''' |
'''Example usage:''' |
||
<syntaxhighlight lang=j>myimg=: 0 255 0 makeRGB 25 25 NB. 25 by 25 green image |
<syntaxhighlight lang="j">myimg=: 0 255 0 makeRGB 25 25 NB. 25 by 25 green image |
||
myimg=: (12 12 12 ; 255 0 0) drawCircles myimg NB. draw red circle with radius 12 |
myimg=: (12 12 12 ; 255 0 0) drawCircles myimg NB. draw red circle with radius 12 |
||
viewRGB ((12 12 9 ,: 12 12 6) ; 0 0 255) drawCircles myimg NB. draw two more concentric circles</syntaxhighlight> |
viewRGB ((12 12 9 ,: 12 12 6) ; 0 0 255) drawCircles myimg NB. draw two more concentric circles</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang=java> |
<syntaxhighlight lang="java"> |
||
import java.awt.Color; |
import java.awt.Color; |
||
Line 1,472: | Line 1,450: | ||
} |
} |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
<syntaxhighlight lang=julia>function drawcircle!(img::Matrix{T}, col::T, x0::Int, y0::Int, radius::Int) where T |
<syntaxhighlight lang="julia">function drawcircle!(img::Matrix{T}, col::T, x0::Int, y0::Int, radius::Int) where T |
||
x = radius - 1 |
x = radius - 1 |
||
y = 0 |
y = 0 |
||
Line 1,506: | Line 1,483: | ||
img = fill(Gray(255.0), 25, 25); |
img = fill(Gray(255.0), 25, 25); |
||
drawcircle!(img, Gray(0.0), 12, 12, 12)</syntaxhighlight> |
drawcircle!(img, Gray(0.0), 12, 12, 12)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=scala>// version 1.1.4-3 |
<syntaxhighlight lang="scala">// version 1.1.4-3 |
||
import java.awt.Color |
import java.awt.Color |
||
Line 1,569: | Line 1,545: | ||
JOptionPane.showMessageDialog(null, label, title, JOptionPane.PLAIN_MESSAGE) |
JOptionPane.showMessageDialog(null, label, title, JOptionPane.PLAIN_MESSAGE) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Uses Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending.. |
Uses Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending.. |
||
<syntaxhighlight lang=lua>function Bitmap:circle(x, y, r, c) |
<syntaxhighlight lang="lua">function Bitmap:circle(x, y, r, c) |
||
local dx, dy, err = r, 0, 1-r |
local dx, dy, err = r, 0, 1-r |
||
while dx >= dy do |
while dx >= dy do |
||
Line 1,592: | Line 1,567: | ||
end</syntaxhighlight> |
end</syntaxhighlight> |
||
Demo: |
Demo: |
||
<syntaxhighlight lang=lua>function Bitmap:axes() |
<syntaxhighlight lang="lua">function Bitmap:axes() |
||
local hw, hh = math.floor(self.width/2), math.floor(self.height/2) |
local hw, hh = math.floor(self.width/2), math.floor(self.height/2) |
||
for i = 0, self.width-1 do self:set(i,hh,"-") end |
for i = 0, self.width-1 do self:set(i,hh,"-") end |
||
Line 1,639: | Line 1,614: | ||
· · · · · · · · · ■ ■ ■ ■ ■ ■ ■ · · · · · · · · · |
· · · · · · · · · ■ ■ ■ ■ ■ ■ ■ · · · · · · · · · |
||
· · · · · · · · · · · · | · · · · · · · · · · · ·</pre> |
· · · · · · · · · · · · | · · · · · · · · · · · ·</pre> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang=mathematica>SetAttributes[drawcircle, HoldFirst]; |
<syntaxhighlight lang="mathematica">SetAttributes[drawcircle, HoldFirst]; |
||
drawcircle[img_, {x0_, y0_}, r_, color_: White] := |
drawcircle[img_, {x0_, y0_}, r_, color_: White] := |
||
Module[{f = 1 - r, ddfx = 1, ddfy = -2 r, x = 0, y = r, |
Module[{f = 1 - r, ddfx = 1, ddfy = -2 r, x = 0, y = r, |
||
Line 1,652: | Line 1,626: | ||
img = ReplacePixelValue[img, {x0, y0} + # -> color & /@ pixels]]</syntaxhighlight> |
img = ReplacePixelValue[img, {x0, y0} + # -> color & /@ pixels]]</syntaxhighlight> |
||
Example usage(it will draw a circle on Lena's face.): |
Example usage(it will draw a circle on Lena's face.): |
||
<syntaxhighlight lang=mathematica>img = ExampleData[{"TestImage", "Lena"}]; |
<syntaxhighlight lang="mathematica">img = ExampleData[{"TestImage", "Lena"}]; |
||
drawcircle[img, {250, 250}, 100]</syntaxhighlight> |
drawcircle[img, {250, 250}, 100]</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
<syntaxhighlight lang=modula3>INTERFACE Circle; |
<syntaxhighlight lang="modula3">INTERFACE Circle; |
||
IMPORT Bitmap; |
IMPORT Bitmap; |
||
Line 1,667: | Line 1,640: | ||
END Circle.</syntaxhighlight> |
END Circle.</syntaxhighlight> |
||
<syntaxhighlight lang=modula3>MODULE Circle; |
<syntaxhighlight lang="modula3">MODULE Circle; |
||
IMPORT Bitmap; |
IMPORT Bitmap; |
||
Line 1,710: | Line 1,683: | ||
Example (outputs a [[Write_ppm_file | PPM]] image): |
Example (outputs a [[Write_ppm_file | PPM]] image): |
||
<syntaxhighlight lang=modula3>MODULE Main; |
<syntaxhighlight lang="modula3">MODULE Main; |
||
IMPORT Circle, Bitmap, PPM; |
IMPORT Circle, Bitmap, PPM; |
||
Line 1,722: | Line 1,695: | ||
PPM.Create("testpic.ppm", testpic); |
PPM.Create("testpic.ppm", testpic); |
||
END Main.</syntaxhighlight> |
END Main.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Ada}} |
{{trans|Ada}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import bitmap |
||
proc setPixel(img: Image; x, y: int; color: Color) {.inline.} = |
proc setPixel(img: Image; x, y: int; color: Color) {.inline.} = |
||
Line 1,795: | Line 1,767: | ||
=={{Header|OCaml}}== |
=={{Header|OCaml}}== |
||
<syntaxhighlight lang=ocaml>let raster_circle ~img ~color ~c:(x0, y0) ~r = |
<syntaxhighlight lang="ocaml">let raster_circle ~img ~color ~c:(x0, y0) ~r = |
||
let plot = put_pixel img color in |
let plot = put_pixel img color in |
||
let x = 0 |
let x = 0 |
||
Line 1,822: | Line 1,794: | ||
loop x y m |
loop x y m |
||
;;</syntaxhighlight> |
;;</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<syntaxhighlight lang=perl># 20220301 Perl programming solution |
<syntaxhighlight lang="perl"># 20220301 Perl programming solution |
||
use strict; |
use strict; |
||
Line 1,861: | Line 1,832: | ||
####### |
####### |
||
</pre> |
</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{Trans|Go}} |
{{Trans|Go}} |
||
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> |
||
Results may be verified with demo\rosetta\viewppm.exw |
Results may be verified with demo\rosetta\viewppm.exw |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="phix">-- demo\rosetta\Bitmap_Circle.exw (runnable version) |
||
include ppm.e -- red, yellow, new_image(), write_ppm() -- (covers above requirements) |
include ppm.e -- red, yellow, new_image(), write_ppm() -- (covers above requirements) |
||
Line 1,906: | Line 1,876: | ||
img = Circle(img, 200, 150, 100, red) |
img = Circle(img, 200, 150, 100, red) |
||
write_ppm("Circle.ppm",img)</syntaxhighlight> |
write_ppm("Circle.ppm",img)</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="picolisp">(de midPtCircle (Img CX CY Rad) |
||
(let (F (- 1 Rad) DdFx 0 DdFy (* -2 Rad) X 0 Y Rad) |
(let (F (- 1 Rad) DdFx 0 DdFy (* -2 Rad) X 0 Y Rad) |
||
(set (nth Img (+ CY Rad) CX) 1) |
(set (nth Img (+ CY Rad) CX) 1) |
||
Line 1,935: | Line 1,904: | ||
(prinl 120 " " 120) |
(prinl 120 " " 120) |
||
(mapc prinl Img) ) )</syntaxhighlight> |
(mapc prinl Img) ) )</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="pl/i"> |
||
/* Plot three circles. */ |
/* Plot three circles. */ |
||
Line 2,035: | Line 2,003: | ||
....................|.................... |
....................|.................... |
||
</pre> |
</pre> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="purebasic">Procedure rasterCircle(cx, cy, r, Color) |
||
;circle must lie completely within the image boundaries |
;circle must lie completely within the image boundaries |
||
Protected f= 1 - r |
Protected f= 1 - r |
||
Line 2,077: | Line 2,044: | ||
Repeat: Until WaitWindowEvent() = #PB_Event_CloseWindow</syntaxhighlight> |
Repeat: Until WaitWindowEvent() = #PB_Event_CloseWindow</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|3.1}} |
{{works with|Python|3.1}} |
||
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]] |
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]] |
||
<syntaxhighlight lang=python>def circle(self, x0, y0, radius, colour=black): |
<syntaxhighlight lang="python">def circle(self, x0, y0, radius, colour=black): |
||
f = 1 - radius |
f = 1 - radius |
||
ddf_x = 1 |
ddf_x = 1 |
||
Line 2,150: | Line 2,116: | ||
''' |
''' |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
Port of the Pyhton solution. |
Port of the Pyhton solution. |
||
<syntaxhighlight lang=racket> |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require racket/draw) |
(require racket/draw) |
||
Line 2,199: | Line 2,164: | ||
bm |
bm |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
Line 2,205: | Line 2,169: | ||
We'll augment the Pixel and Bitmap classes from the [[Bitmap#Raku|Bitmap]] task. |
We'll augment the Pixel and Bitmap classes from the [[Bitmap#Raku|Bitmap]] task. |
||
<syntaxhighlight lang=raku line>use MONKEY-TYPING; |
<syntaxhighlight lang="raku" line>use MONKEY-TYPING; |
||
class Pixel { has UInt ($.R, $.G, $.B) } |
class Pixel { has UInt ($.R, $.G, $.B) } |
||
Line 2,260: | Line 2,224: | ||
$b.raster-circle(16, 16, 15, Pixel.new(R=>0, G=>0, B=>100)); |
$b.raster-circle(16, 16, 15, Pixel.new(R=>0, G=>0, B=>100)); |
||
say $b.P3;</syntaxhighlight> |
say $b.P3;</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Programming note: because of character output to a terminal screen, a circle appears to be elongated in the |
Programming note: because of character output to a terminal screen, a circle appears to be elongated in the |
||
Line 2,267: | Line 2,230: | ||
The program automatically shows all of the plot's points by finding the minimum and maximum '''X''','''Y''' coördinates. |
The program automatically shows all of the plot's points by finding the minimum and maximum '''X''','''Y''' coördinates. |
||
<syntaxhighlight lang=rexx>/*REXX program plots three circles using midpoint/Bresenham's circle algorithm. */ |
<syntaxhighlight lang="rexx">/*REXX program plots three circles using midpoint/Bresenham's circle algorithm. */ |
||
@.= '·' /*fill the array with middle─dots char.*/ |
@.= '·' /*fill the array with middle─dots char.*/ |
||
minX= 0; maxX= 0; minY= 0; maxY= 0 /*initialize the minimums and maximums.*/ |
minX= 0; maxX= 0; minY= 0; maxY= 0 /*initialize the minimums and maximums.*/ |
||
Line 2,355: | Line 2,318: | ||
·······················│······················· |
·······················│······················· |
||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=ruby>Pixel = Struct.new(:x, :y) |
<syntaxhighlight lang="ruby">Pixel = Struct.new(:x, :y) |
||
class Pixmap |
class Pixmap |
||
Line 2,396: | Line 2,358: | ||
bitmap = Pixmap.new(30, 30) |
bitmap = Pixmap.new(30, 30) |
||
bitmap.draw_circle(Pixel[14,14], 12, RGBColour::BLACK)</syntaxhighlight> |
bitmap.draw_circle(Pixel[14,14], 12, RGBColour::BLACK)</syntaxhighlight> |
||
=={{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. |
||
<syntaxhighlight lang=scala>object BitmapOps { |
<syntaxhighlight lang="scala">object BitmapOps { |
||
def midpoint(bm:RgbBitmap, x0:Int, y0:Int, radius:Int, c:Color)={ |
def midpoint(bm:RgbBitmap, x0:Int, y0:Int, radius:Int, c:Color)={ |
||
var f=1-radius |
var f=1-radius |
||
Line 2,435: | Line 2,396: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
ref [[Basic bitmap storage#Tcl]] and [[Assertions#Tcl]] |
ref [[Basic bitmap storage#Tcl]] and [[Assertions#Tcl]] |
||
<syntaxhighlight lang=tcl>package require Tcl 8.5 |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
package require Tk |
package require Tk |
||
Line 2,487: | Line 2,447: | ||
fill $img black |
fill $img black |
||
drawCircle $img blue {100 50} 49</syntaxhighlight> |
drawCircle $img blue {100 50} 49</syntaxhighlight> |
||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
<syntaxhighlight lang=vedit> |
<syntaxhighlight lang="vedit"> |
||
// Draw a circle using Bresenham's circle algorithm. |
// Draw a circle using Bresenham's circle algorithm. |
||
// #21 = center x, #22 = center y; #23 = radius |
// #21 = center x, #22 = center y; #23 = radius |
||
Line 2,521: | Line 2,480: | ||
return |
return |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|DOME}} |
{{libheader|DOME}} |
||
<syntaxhighlight lang=ecmascript>import "graphics" for Canvas, Color, ImageData |
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, ImageData |
||
import "dome" for Window |
import "dome" for Window |
||
Line 2,585: | Line 2,543: | ||
var Game = MidpointCircle.new(400, 400)</syntaxhighlight> |
var Game = MidpointCircle.new(400, 400)</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \include 'code' declarations |
||
proc Circle(X0, Y0, Radius, Color); \Display a circle |
proc Circle(X0, Y0, Radius, Color); \Display a circle |
||
Line 2,625: | Line 2,582: | ||
SetVid(3); \restore normal text mode |
SetVid(3); \restore normal text mode |
||
]</syntaxhighlight> |
]</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Image cribbed from the BBC BASIC entry. Algorithm from Wikipedia article.<br/> |
Image cribbed from the BBC BASIC entry. Algorithm from Wikipedia article.<br/> |
||
Line 2,631: | Line 2,587: | ||
[[Image:circle_bbc.gif|right]] |
[[Image:circle_bbc.gif|right]] |
||
This is the code from the PPM class: |
This is the code from the PPM class: |
||
<syntaxhighlight lang=zkl> fcn circle(x0,y0,r,rgb){ |
<syntaxhighlight lang="zkl"> fcn circle(x0,y0,r,rgb){ |
||
x:=r; y:=0; radiusError:=1-x; |
x:=r; y:=0; radiusError:=1-x; |
||
while(x >= y){ |
while(x >= y){ |
||
Line 2,647: | Line 2,603: | ||
} |
} |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
<syntaxhighlight lang=zkl>ppm:=PPM(200,200,0xFF|FF|FF); |
<syntaxhighlight lang="zkl">ppm:=PPM(200,200,0xFF|FF|FF); |
||
ppm.circle(100,100,40,00); // black circle |
ppm.circle(100,100,40,00); // black circle |
||
ppm.circle(100,100,80,0xFF|00|00); // red circle |
ppm.circle(100,100,80,0xFF|00|00); // red circle |
||
ppm.write(File("foo.ppm","wb"));</syntaxhighlight> |
ppm.write(File("foo.ppm","wb"));</syntaxhighlight> |
||
{{omit from|AWK}} |
{{omit from|AWK}} |
||
{{omit from|PARI/GP}} |
{{omit from|PARI/GP}} |
||
⚫ |