Bitmap/Midpoint circle algorithm: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(11 intermediate revisions by 7 users not shown)
Line 1:
[[Category:Geometry]]
{{task|Raster graphics operations}}
 
Line 7 ⟶ 8:
([[wp:Midpoint_circle_algorithm|definition on Wikipedia]]).
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">T Colour
Byte r, g, b
 
Line 86:
V bitmap = Bitmap(25, 25)
bitmap.circle(x0' 12, y0' 12, radius' 12)
bitmap.chardisplay()</langsyntaxhighlight>
 
{{out}}
Line 118:
+-------------------------+
</pre>
 
=={{header|Action!}}==
Part of the task is available in [http://www.rosettacode.org/wiki/Category:Action!_Bitmap_tools#RGBCIRCL.ACT RGBCIRCL.ACT].
{{libheader|Action! Bitmap tools}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:RGBCIRCL.ACT" ;from task Midpoint circle algorithm
 
RGB black,yellow,violet,blue
Line 184 ⟶ 183:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Midpoint_circle_algorithm.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">procedure Circle
( Picture : in out Image;
Center : Point;
Line 223 ⟶ 221:
Picture (Center.X - Y, Center.Y - X) := Color;
end loop;
end Circle;</langsyntaxhighlight>
The following illustrates use:
<langsyntaxhighlight lang="ada"> X : Image (1..16, 1..16);
begin
Fill (X, White);
Circle (X, (8, 8), 5, Black);
Print (X);</langsyntaxhighlight>
{{out}}
<pre>
Line 249 ⟶ 247:
 
</pre>
 
=={{header|ALGOL 68}}==
{{trans|Ada}}
Line 255 ⟶ 252:
{{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/Midpoint_circle_algorithm.a68'''<langsyntaxhighlight lang="algol68"># -*- coding: utf-8 -*- #
 
circle OF class image :=
Line 291 ⟶ 288:
END # circle #;
 
SKIP</langsyntaxhighlight>'''File: test/Bitmap/Midpoint_circle_algorithm.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
Line 305 ⟶ 302:
(circle OF class image)(x, (8, 8), 5, (black OF class image));
(print OF class image)(x)
)</langsyntaxhighlight>
{{out}}
<pre>
Line 326 ⟶ 323:
</pre>
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight lang="gwbasic"> 10 GR : GOSUB 330: END
330 COLOR= 15:CX = 20:CY = 20:R = 18: GOSUB 350"CIRCLE"
340 COLOR= 0:CX = 15:CY = 15:R = 6
350 F = 1 - R:X = 0:Y = R:DX = 0:DY = - 2 * R: PLOT CX,CY + R: PLOT CX,CY - R: HLIN CX - R,CX + R AT CY: IF X > = Y THEN RETURN
360 FOR I = 0 TO 1: IF F > = 0 THEN Y = Y - 1:DY = DY + 2:F = F + DY
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</langsyntaxhighlight>
=={{header|ATS}}==
See [[Bresenham_tasks_in_ATS]].
 
=={{header|bash}}==
<langsyntaxhighlight lang="bash">#! /bin/bash
# Based on https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
 
Line 389:
drawcircle 13 13 11
echo -en "\e[H"
</syntaxhighlight>
</lang>
{{Out}}
<pre>------#########------
Line 413:
------#########------</pre>
 
=={{header|BASIC256BASIC}}==
==={{header|BASIC256}}===
<lang basic256>fastgraphics
<syntaxhighlight lang="basic256">fastgraphics
clg
color red
Line 452 ⟶ 453:
end while
return 0
End Function</langsyntaxhighlight>
{{Out}}
[http://s16.postimg.org/ge0ndfs9h/Output.jpg http://s16.postimg.org/ge0ndfs9h/Output.jpg]
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
setlocal enabledelayedexpansion
 
Line 528 ⟶ 529:
goto circle_loop
)
goto :EOF</langsyntaxhighlight>
{{Out}}
<pre> █ █
Line 561 ⟶ 562:
███████
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
[[Image:circle_bbc.gif|right]]
<langsyntaxhighlight lang="bbcbasic"> Width% = 200
Height% = 200
Line 607:
GCOL 1
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
 
Interface:
 
<langsyntaxhighlight lang="c">void raster_circle(
image img,
unsigned int x0,
Line 620 ⟶ 619:
color_component r,
color_component g,
color_component b );</langsyntaxhighlight>
 
Implementation:
 
<langsyntaxhighlight lang="c">#define plot(x, y) put_pixel_clip(img, x, y, r, g, b)
 
void raster_circle(
Line 667 ⟶ 666:
}
}
#undef plot</langsyntaxhighlight>
 
=={{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.
 
<langsyntaxhighlight lang="csharp">
/// <summary>
/// Draws a circle.
Line 722 ⟶ 720:
} while (x <= y);
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
Based upon the Common Lisp version.
<langsyntaxhighlight lang="clojure">(defn draw-circle [draw-function x0 y0 radius]
(letfn [(put [x y m]
(let [x+ (+ x0 x)
Line 751 ⟶ 748:
y
(+ m 4 (* 8 x)))))))]
(put 0 radius (- 5 (* 4 radius)))))</langsyntaxhighlight>
<langsyntaxhighlight lang="clojure">(let [circle-points (atom [])]
(letfn [(draw-fn [x y]
(swap! circle-points #(conj % [x y])))]
Line 761 ⟶ 758:
@circle-points)]
(doseq [line grid]
(println (clojure.string/join line)))))</langsyntaxhighlight>
<pre>
Line 784 ⟶ 781:
nil
</pre>
 
=={{header|Common Lisp}}==
Based upon the OCaml version.
 
<langsyntaxhighlight lang="lisp">(defun draw-circle (draw-function x0 y0 radius)
(labels ((foo (x y)
(funcall draw-function x y))
Line 816 ⟶ 812:
(+ m 4 (* 8 x))))))))
(put 0 radius (- 5 (* 4 radius)))
(values)))</langsyntaxhighlight>
<langsyntaxhighlight lang="lisp">CL-USER> (let ((buffer (make-array '(30 30)
:element-type 'bit)))
(draw-circle (lambda (x y)
(setf (bit buffer x y) 1)) 15 15 10)
buffer)</langsyntaxhighlight>
<pre>;; edited for your convenience
(( )
Line 847 ⟶ 843:
( ))
</pre>
 
=={{header|D}}==
Uses the bitmap module from the Bitmap Task.
<langsyntaxhighlight lang="d">import bitmap: Image, RGB;
 
void circle(Color)(Image!Color img, in int x0, in int y0,
Line 890 ⟶ 885:
circle(img, 12, 12, 12, RGB.black);
img.textualShow;
}</langsyntaxhighlight>
{{out}}
<pre>.........#######.........
Line 917 ⟶ 912:
.......##.......##.......
.........#######.........</pre>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiBrezCircle.png|frame|none]]
 
 
<syntaxhighlight lang="Delphi">
 
 
procedure DrawCircle(Image: TImage; Radius: integer; Center: TPoint);
var T1,T2: integer;
var X,Y: integer;
var Cnt: integer;
 
procedure DrawPixels(X,Y: integer);
{Draw pixel into all 8 octents}
begin
Image.Canvas.Pixels[Center.X + x, Center.Y + y]:=clRed;
Image.Canvas.Pixels[Center.X - X, Center.Y + Y]:=clRed;
Image.Canvas.Pixels[Center.X + X, Center.Y - Y]:=clRed;
Image.Canvas.Pixels[Center.X - X, Center.Y - Y]:=clRed;
Image.Canvas.Pixels[Center.X + Y, Center.Y + X]:=clRed;
Image.Canvas.Pixels[Center.X - Y, Center.Y + X]:=clRed;
Image.Canvas.Pixels[Center.X + y, Center.Y - X]:=clRed;
Image.Canvas.Pixels[Center.X - Y, Center.Y - X]:=clRed;
end;
 
begin
Cnt:=0;
T1:= Radius div 32;
{Start on X-axis}
X:= Radius; Y:= 0;
repeat
begin
DrawPixels(X, Y);
Y:=Y + 1;
T1:=T1 + Y;
T2:=T1 - X;
if T2 >= 0 then
begin
T1:=T2;
X:=X - 1;
end;
Inc(Cnt);
end
until x < y;
Form1.Caption:=IntToStr(Cnt);
end;
 
 
 
procedure ShowBrezCircle(Image: TImage);
begin
{Draw three times to make line thicker}
DrawCircle(Image,100,Point(200,200));
DrawCircle(Image,99,Point(200,200));
DrawCircle(Image,98,Point(200,200));
Image.Invalidate;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Elapsed Time: 7.341 ms.
</pre>
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM BCircle
 
!$INCLUDE="PC.LIB"
Line 956 ⟶ 1,019:
BCircle(100,100,80)
END PROGRAM
</syntaxhighlight>
</lang>
 
=={{header|Evaldraw}}==
Gives slightly faster (in some cases), but more importantly prettier circles than the builtin drawsph(x,y,-rad) function.
<syntaxhighlight lang="C">()
{
cls(0);
setcol(0xffffff);
srand(1234);
for(i=0; i<1000; i++) {
rad = int( abs(nrnd*10) );
x=rnd*xres;
y=rnd*yres;
drawcircle(x,y,rad);
//drawsph(x,y,-rad);
}
}
 
drawcircle(cx,cy,r) {
if (cx+r < 0 || cy+r < 0) return;
if (cx-r > xres || cy-r > yres) return;
r = int(r);
if (r<=0) return;
r2 = r+r;
x = r; y = 0;
dy = -2; dx = r2+r2 - 4;
d = r2-1;
while(y<=x) {
setpix(cx-x, cy-y);
setpix(cx+x, cy-y);
setpix(cx-x, cy+y);
setpix(cx+x, cy+y);
setpix(cx-y, cy-x);
setpix(cx+y, cy-x);
setpix(cx-y, cy+x);
setpix(cx+y, cy+x);
d += dy;
dy -= 4;
++y;
if (d<0) {
d += dx;
dx -= 4;
--x;
}
}
}</syntaxhighlight>
 
=={{header|FBSL}}==
'''Using pure FBSL's built-in graphics functions:'''
<langsyntaxhighlight lang="qbasic">#DEFINE WM_LBUTTONDOWN 513
#DEFINE WM_CLOSE 16
 
Line 1,000 ⟶ 1,108:
WEND
END SUB
END SUB</langsyntaxhighlight>
'''Ouptut:''' [[File:FBSLMidpoint.PNG]]
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: circle { x y r color bmp -- }
1 r - 0 r 2* negate 0 r { f ddx ddy dx dy }
color x y r + bmp b!
Line 1,032 ⟶ 1,139:
0 test bfill
6 6 5 blue test circle
test bshow cr</langsyntaxhighlight>
 
=={{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]].
 
<langsyntaxhighlight lang="fortran">interface draw_circle
module procedure draw_circle_sc, draw_circle_rgb
end interface
 
private :: plot, draw_circle_toch</langsyntaxhighlight>
 
<langsyntaxhighlight lang="fortran">subroutine plot(ch, p, v)
integer, dimension(:,:), intent(out) :: ch
type(point), intent(in) :: p
Line 1,117 ⟶ 1,223:
call draw_circle_toch(img%channel, c, radius, lum)
end subroutine draw_circle_sc</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 15-10-2016
' compile with: fbc -s gui
 
Line 1,173 ⟶ 1,278:
WindowTitle "hit any key to end program"
Sleep
End</langsyntaxhighlight>
 
 
=={{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.
<langsyntaxhighlight lang="futurebasic">
_wndW = 600
_wndH = 600
Line 1,259 ⟶ 1,362:
 
HandleEvents
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
This produces identical results to the C code in the WP article, but with more compact code.
<langsyntaxhighlight lang="go">package raster
 
// Circle plots a circle with center x, y and radius r.
Line 1,298 ⟶ 1,400:
func (b *Bitmap) CircleRgb(x, y, r int, c Rgb) {
b.Circle(x, y, r, c.Pixel())
}</langsyntaxhighlight>
Demonstration program:
<syntaxhighlight lang="text">package main
 
// Files required to build supporting package raster are found in:
Line 1,320 ⟶ 1,422:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
The basic algorithm can be implemented generically.
<langsyntaxhighlight lang="haskell">module Circle where
 
import Data.List
Line 1,352 ⟶ 1,453:
ddf_x' = ddf_x + 2
x' = x + 1
</syntaxhighlight>
</lang>
An example using regular 2d arrays of characters to represent a bitmap:
<langsyntaxhighlight lang="haskell">module CircleArrayExample where
 
import Circle
Line 1,385 ⟶ 1,486:
-- Converts a surface to a string and prints it
printSurface = putStrLn . showSurface
</syntaxhighlight>
</lang>
Using the Image type from the Bitmap module defined [[Basic_bitmap_storage|here]]:
<langsyntaxhighlight lang="haskell">module CircleBitmapExample where
 
import Circle
Line 1,398 ⟶ 1,499:
forM_ pixels $ \pixel -> setPix image pixel colour
return image
</syntaxhighlight>
</lang>
 
=={{header|J}}==
'''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.)
<langsyntaxhighlight lang="j">NB.*getBresenhamCircle v Returns points for a circle given center and radius
NB. y is: y0 x0 radius
getBresenhamCircle=: monad define
Line 1,426 ⟶ 1,526:
NB.*drawCircles v Draws circle(s) (x) on image (y)
NB. x is: 2-item list of boxed (y0 x0 radius) ; (color)
drawCircles=: (1&{:: ;~ [: ; [: <@getBresenhamCircle"1 (0&{::))@[ setPixels ]</langsyntaxhighlight>
 
'''Example usage:'''
<langsyntaxhighlight 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
viewRGB ((12 12 9 ,: 12 12 6) ; 0 0 255) drawCircles myimg NB. draw two more concentric circles</langsyntaxhighlight>
 
=={{header|Java}}==
 
<langsyntaxhighlight lang="java">
import java.awt.Color;
 
Line 1,446 ⟶ 1,545:
 
private void drawCircle(final int centerX, final int centerY, final int radius) {
int d = (5 - rradius * 4)/4;
int x = 0;
int y = radius;
Line 1,471 ⟶ 1,570:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<langsyntaxhighlight lang="julia">function drawcircle!(img::Matrix{T}, col::T, x0::Int, y0::Int, radius::Int) where T
x = radius - 1
y = 0
Line 1,505 ⟶ 1,604:
 
img = fill(Gray(255.0), 25, 25);
drawcircle!(img, Gray(0.0), 12, 12, 12)</langsyntaxhighlight>
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">// version 1.1.4-3
 
import java.awt.Color
import java.awt.Graphics
import java.awt.image.BufferedImage
import javax.swing.JOptionPane
import javax.swing.JLabel
import javax.swing.ImageIcon
 
class BasicBitmapStorage(width: Int, height: Int) {
val image = BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR)
 
fun fill(c: Color) {
val g = image.graphics
g.color = c
g.fillRect(0, 0, image.width, image.height)
}
 
fun setPixel(x: Int, y: Int, c: Color) = image.setRGB(x, y, c.getRGB())
 
fun getPixel(x: Int, y: Int) = Color(image.getRGB(x, y))
}
 
fun drawCircle(bbs: BasicBitmapStorage, centerX: Int, centerY: Int, radius: Int, circleColor: Color) {
var d = (5 - radius * 4) / 4
var x = 0
var y = radius
do {
with(bbs) {
setPixel(centerX + x, centerY + y, circleColor)
setPixel(centerX + x, centerY - y, circleColor)
setPixel(centerX - x, centerY + y, circleColor)
setPixel(centerX - x, centerY - y, circleColor)
setPixel(centerX + y, centerY + x, circleColor)
setPixel(centerX + y, centerY - x, circleColor)
setPixel(centerX - y, centerY + x, circleColor)
setPixel(centerX - y, centerY - x, circleColor)
}
if (d < 0) {
d += 2 * x + 1
}
else {
d += 2 * (x - y) + 1
y--
}
x++
}
while (x <= y)
}
 
fun main(args: Array<String>) {
val bbs = BasicBitmapStorage(400, 400)
bbs.fill(Color.pink)
drawCircle(bbs, 200, 200, 100, Color.black)
drawCircle(bbs, 200, 200, 50, Color.white)
val label = JLabel(ImageIcon(bbs.image))
val title = "Bresenham's circle algorithm"
JOptionPane.showMessageDialog(null, label, title, JOptionPane.PLAIN_MESSAGE)
}</syntaxhighlight>
=={{header|Lua}}==
Uses Bitmap class [[Bitmap#Lua|here]], with an ASCII pixel representation, then extending..
<langsyntaxhighlight lang="lua">function Bitmap:circle(x, y, r, c)
local dx, dy, err = r, 0, 1-r
while dx >= dy do
Line 1,527 ⟶ 1,687:
end
end
end</langsyntaxhighlight>
Demo:
<langsyntaxhighlight lang="lua">function Bitmap:axes()
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
Line 1,549 ⟶ 1,709:
bitmap:circle(12, 12, 5, "■")
bitmap:circle(12, 12, 2, "■")
bitmap:render()</langsyntaxhighlight>
{{out}}
<pre>· · · · · · · · · · · · | · · · · · · · · · · · ·
Line 1,576 ⟶ 1,736:
· · · · · · · · · ■ ■ ■ ■ ■ ■ ■ · · · · · · · · ·
· · · · · · · · · · · · | · · · · · · · · · · · ·</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<lang scala>// version 1.1.4-3
 
import java.awt.Color
import java.awt.Graphics
import java.awt.image.BufferedImage
import javax.swing.JOptionPane
import javax.swing.JLabel
import javax.swing.ImageIcon
 
class BasicBitmapStorage(width: Int, height: Int) {
val image = BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR)
 
fun fill(c: Color) {
val g = image.graphics
g.color = c
g.fillRect(0, 0, image.width, image.height)
}
 
fun setPixel(x: Int, y: Int, c: Color) = image.setRGB(x, y, c.getRGB())
 
fun getPixel(x: Int, y: Int) = Color(image.getRGB(x, y))
}
 
fun drawCircle(bbs: BasicBitmapStorage, centerX: Int, centerY: Int, radius: Int, circleColor: Color) {
var d = (5 - radius * 4) / 4
var x = 0
var y = radius
do {
with(bbs) {
setPixel(centerX + x, centerY + y, circleColor)
setPixel(centerX + x, centerY - y, circleColor)
setPixel(centerX - x, centerY + y, circleColor)
setPixel(centerX - x, centerY - y, circleColor)
setPixel(centerX + y, centerY + x, circleColor)
setPixel(centerX + y, centerY - x, circleColor)
setPixel(centerX - y, centerY + x, circleColor)
setPixel(centerX - y, centerY - x, circleColor)
}
if (d < 0) {
d += 2 * x + 1
}
else {
d += 2 * (x - y) + 1
y--
}
x++
}
while (x <= y)
}
 
fun main(args: Array<String>) {
val bbs = BasicBitmapStorage(400, 400)
bbs.fill(Color.pink)
drawCircle(bbs, 200, 200, 100, Color.black)
drawCircle(bbs, 200, 200, 50, Color.white)
val label = JLabel(ImageIcon(bbs.image))
val title = "Bresenham's circle algorithm"
JOptionPane.showMessageDialog(null, label, title, JOptionPane.PLAIN_MESSAGE)
}</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">SetAttributes[drawcircle, HoldFirst];
drawcircle[img_, {x0_, y0_}, r_, color_: White] :=
Module[{f = 1 - r, ddfx = 1, ddfy = -2 r, x = 0, y = r,
Line 1,650 ⟶ 1,746:
pixels = Join[pixels, {{x, y}, {x, -y}, {-x, y}, {-x, -y},
{y, x}, {y, -x}, {-y, x}, {-y, -x}}]];
img = ReplacePixelValue[img, {x0, y0} + # -> color & /@ pixels]]</langsyntaxhighlight>
Example usage(it will draw a circle on Lena's face.):
<langsyntaxhighlight lang="mathematica">img = ExampleData[{"TestImage", "Lena"}];
drawcircle[img, {250, 250}, 100]</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">INTERFACE Circle;
 
IMPORT Bitmap;
Line 1,666 ⟶ 1,761:
color: Bitmap.Pixel);
 
END Circle.</langsyntaxhighlight>
<langsyntaxhighlight lang="modula3">MODULE Circle;
 
IMPORT Bitmap;
Line 1,707 ⟶ 1,802:
 
BEGIN
END Circle.</langsyntaxhighlight>
 
Example (outputs a [[Write_ppm_file | PPM]] image):
<langsyntaxhighlight lang="modula3">MODULE Main;
 
IMPORT Circle, Bitmap, PPM;
Line 1,721 ⟶ 1,816:
Circle.Draw(testpic, Bitmap.Point{16, 16}, 10, Bitmap.Black);
PPM.Create("testpic.ppm", testpic);
END Main.</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Ada}}
<langsyntaxhighlight Nimlang="nim">import bitmap
 
proc setPixel(img: Image; x, y: int; color: Color) {.inline.} =
Line 1,773 ⟶ 1,867:
img.fill(White)
img.drawCircle((7, 7), 5, Black)
img.print()</langsyntaxhighlight>
 
{{out}}
Line 1,793 ⟶ 1,887:
................</pre>
 
=={{Header|OCaml}}==
 
<syntaxhighlight lang="ocaml">let raster_circle ~img ~color ~c:(x0, y0) ~r =
let plot = put_pixel img color in
let x = 0
and y = r
and m = 5 - 4 * r
in
let rec loop x y m =
plot (x0 + x) (y0 + y);
plot (x0 + y) (y0 + x);
plot (x0 - x) (y0 + y);
plot (x0 - y) (y0 + x);
plot (x0 + x) (y0 - y);
plot (x0 + y) (y0 - x);
plot (x0 - x) (y0 - y);
plot (x0 - y) (y0 - x);
let y, m =
if m > 0
then (y - 1), (m - 8 * y)
else y, m
in
if x <= y then
let x = x + 1 in
let m = m + 8 * x + 4 in
loop x y m
in
loop x y m
;;</syntaxhighlight>
=={{header|Perl}}==
<langsyntaxhighlight lang="perl"># 20220301 Perl programming solution
 
use strict;
Line 1,806 ⟶ 1,929:
for (@circle) { $points[$_->[0]][$_->[1]] = '#' }
 
print join "\n", map { join '', map { $_ || ' ' } @$_ } @points</langsyntaxhighlight>
{{out}}
<pre>
Line 1,831 ⟶ 1,954:
#######
</pre>
 
=={{header|Phix}}==
{{Trans|Go}}
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
<langsyntaxhighlight Phixlang="phix">-- demo\rosetta\Bitmap_Circle.exw (runnable version)
include ppm.e -- red, yellow, new_image(), write_ppm() -- (covers above requirements)
 
Line 1,875 ⟶ 1,997:
sequence img = new_image(400,300,yellow)
img = Circle(img, 200, 150, 100, red)
write_ppm("Circle.ppm",img)</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de midPtCircle (Img CX CY Rad)
(let (F (- 1 Rad) DdFx 0 DdFy (* -2 Rad) X 0 Y Rad)
(set (nth Img (+ CY Rad) CX) 1)
Line 1,904 ⟶ 2,025:
(prinl "P1")
(prinl 120 " " 120)
(mapc prinl Img) ) )</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* Plot three circles. */
 
Line 1,960 ⟶ 2,080:
 
END CIRCLE;
</syntaxhighlight>
</lang>
{{out}} for three circles centered at the origin.
<pre>
Line 2,005 ⟶ 2,125:
....................|....................
</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure rasterCircle(cx, cy, r, Color)
;circle must lie completely within the image boundaries
Protected f= 1 - r
Line 2,046 ⟶ 2,165:
ImageGadget(0, 0, 0, 0, 0, ImageID(0))
 
Repeat: Until WaitWindowEvent() = #PB_Event_CloseWindow</langsyntaxhighlight>
 
=={{header|Python}}==
{{works with|Python|3.1}}
 
Extending the example given [[Basic_bitmap_storage#Alternative_version|here]]
<langsyntaxhighlight lang="python">def circle(self, x0, y0, radius, colour=black):
f = 1 - radius
ddf_x = 1
Line 2,119 ⟶ 2,237:
+-------------------------+
'''
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Port of the Pyhton solution.
 
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/draw)
Line 2,168 ⟶ 2,285:
(draw-circle dc 12 12 12)
bm
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
Line 2,175 ⟶ 2,291:
We'll augment the Pixel and Bitmap classes from the [[Bitmap#Raku|Bitmap]] task.
 
<syntaxhighlight lang="raku" perl6line>use MONKEY-TYPING;
 
class Pixel { has UInt ($.R, $.G, $.B) }
Line 2,229 ⟶ 2,345:
$b.fill( Pixel.new( R => 0, G => 0, B => 0) );
$b.raster-circle(16, 16, 15, Pixel.new(R=>0, G=>0, B=>100));
say $b.P3;</langsyntaxhighlight>
 
=={{Header|OCaml}}==
 
<lang ocaml>let raster_circle ~img ~color ~c:(x0, y0) ~r =
let plot = put_pixel img color in
let x = 0
and y = r
and m = 5 - 4 * r
in
let rec loop x y m =
plot (x0 + x) (y0 + y);
plot (x0 + y) (y0 + x);
plot (x0 - x) (y0 + y);
plot (x0 - y) (y0 + x);
plot (x0 + x) (y0 - y);
plot (x0 + y) (y0 - x);
plot (x0 - x) (y0 - y);
plot (x0 - y) (y0 - x);
let y, m =
if m > 0
then (y - 1), (m - 8 * y)
else y, m
in
if x <= y then
let x = x + 1 in
let m = m + 8 * x + 4 in
loop x y m
in
loop x y m
;;</lang>
 
=={{header|REXX}}==
Programming note: &nbsp; because of character output to a terminal screen, a circle appears to be elongated in the
Line 2,267 ⟶ 2,352:
 
The program automatically shows all of the plot's points by finding the minimum and maximum &nbsp; '''X''','''Y''' &nbsp; coördinates.
<langsyntaxhighlight lang="rexx">/*REXX program plots three circles using midpoint/Bresenham's circle algorithm. */
@.= '·' /*fill the array with middle─dots char.*/
minX= 0; maxX= 0; minY= 0; maxY= 0 /*initialize the minimums and maximums.*/
Line 2,308 ⟶ 2,393:
minX= min(minX,c); maxX= max(maxX,c) /*determine the minimum and maximum X.*/
minY= min(minY,r); maxY= max(maxY,r) /* " " " " " Y.*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,355 ⟶ 2,440:
·······················│·······················
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">Pixel = Struct.new(:x, :y)
 
class Pixmap
Line 2,395 ⟶ 2,479:
 
bitmap = Pixmap.new(30, 30)
bitmap.draw_circle(Pixel[14,14], 12, RGBColour::BLACK)</langsyntaxhighlight>
 
=={{header|Scala}}==
Uses the [[Basic_bitmap_storage#Scala|Scala Basic Bitmap Storage]] class.
<langsyntaxhighlight lang="scala">object BitmapOps {
def midpoint(bm:RgbBitmap, x0:Int, y0:Int, radius:Int, c:Color)={
var f=1-radius
Line 2,434 ⟶ 2,517:
}
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
ref [[Basic bitmap storage#Tcl]] and [[Assertions#Tcl]]
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 2,486 ⟶ 2,568:
fill $img black
drawCircle $img blue {100 50} 49</langsyntaxhighlight>
 
=={{header|Vedit macro language}}==
<langsyntaxhighlight lang="vedit">
// Draw a circle using Bresenham's circle algorithm.
// #21 = center x, #22 = center y; #23 = radius
Line 2,520 ⟶ 2,601:
 
return
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color, ImageData
import "dome" for Window
 
Line 2,584 ⟶ 2,664:
}
 
var Game = MidpointCircle.new(400, 400)</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \include 'code' declarations
 
proc Circle(X0, Y0, Radius, Color); \Display a circle
Line 2,624 ⟶ 2,704:
if ChIn(1) then []; \wait for keystroke
SetVid(3); \restore normal text mode
]</langsyntaxhighlight>
 
=={{header|zkl}}==
Image cribbed from the BBC BASIC entry. Algorithm from Wikipedia article.<br/>
Line 2,631 ⟶ 2,710:
[[Image:circle_bbc.gif|right]]
This is the code from the PPM class:
<langsyntaxhighlight lang="zkl"> fcn circle(x0,y0,r,rgb){
x:=r; y:=0; radiusError:=1-x;
while(x >= y){
Line 2,646 ⟶ 2,725:
else{ x-=1; radiusError+=2*(y - x + 1); }
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ppm:=PPM(200,200,0xFF|FF|FF);
ppm.circle(100,100,40,00); // black circle
ppm.circle(100,100,80,0xFF|00|00); // red circle
 
ppm.write(File("foo.ppm","wb"));</langsyntaxhighlight>
 
{{omit from|AWK}}
{{omit from|PARI/GP}}
 
[[Category:Geometry]]
9,476

edits