Bitmap/Midpoint circle algorithm: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, simplified some code.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(40 intermediate revisions by 23 users not shown)
Line 1:
[[Category:Geometry]]
{{task|Raster graphics operations}}Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images,
{{task|Raster graphics operations}}
write an implementation of the '''midpoint circle algorithm'''
 
(also known as '''Bresenham's circle algorithm'''). <BR>
;Task:
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images,
write an implementation of the '''midpoint circle algorithm''' &nbsp; (also known as '''Bresenham's circle algorithm''').
 
([[wp:Midpoint_circle_algorithm|definition on Wikipedia]]).
<br><br>
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">T Colour
Byte r, g, b
 
F (r, g, b)
.r = r
.g = g
.b = b
 
F ==(other)
R .r == other.r & .g == other.g & .b == other.b
 
V black = Colour(0, 0, 0)
V white = Colour(255, 255, 255)
 
T Bitmap
Int width, height
Colour background
[[Colour]] map
 
F (width = 40, height = 40, background = white)
assert(width > 0 & height > 0)
.width = width
.height = height
.background = background
.map = (0 .< height).map(h -> (0 .< @width).map(w -> @@background))
 
F fillrect(x, y, width, height, colour = black)
assert(x >= 0 & y >= 0 & width > 0 & height > 0)
L(h) 0 .< height
L(w) 0 .< width
.map[y + h][x + w] = colour
 
F chardisplay()
V txt = .map.map(row -> row.map(bit -> (I bit == @@.background {‘ ’} E ‘@’)).join(‘’))
txt = txt.map(row -> ‘|’row‘|’)
txt.insert(0, ‘+’(‘-’ * .width)‘+’)
txt.append(‘+’(‘-’ * .width)‘+’)
print(reversed(txt).join("\n"))
 
F set(x, y, colour = black)
.map[y][x] = colour
 
F get(x, y)
R .map[y][x]
 
F circle(x0, y0, radius, colour = black)
V f = 1 - radius
V ddf_x = 1
V ddf_y = -2 * radius
V x = 0
V y = radius
.set(x0, y0 + radius, colour)
.set(x0, y0 - radius, colour)
.set(x0 + radius, y0, colour)
.set(x0 - radius, y0, colour)
 
L x < y
I f >= 0
y--
ddf_y += 2
f += ddf_y
x++
ddf_x += 2
f += ddf_x
.set(x0 + x, y0 + y, colour)
.set(x0 - x, y0 + y, colour)
.set(x0 + x, y0 - y, colour)
.set(x0 - x, y0 - y, colour)
.set(x0 + y, y0 + x, colour)
.set(x0 - y, y0 + x, colour)
.set(x0 + y, y0 - x, colour)
.set(x0 - y, y0 - x, colour)
 
V bitmap = Bitmap(25, 25)
bitmap.circle(x0' 12, y0' 12, radius' 12)
bitmap.chardisplay()</syntaxhighlight>
 
{{out}}
<pre>
+-------------------------+
| @@@@@@@ |
| @@ @@ |
| @@ @@ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
|@ @|
|@ @|
|@ @|
|@ @|
|@ @|
|@ @|
|@ @|
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @ @ |
| @@ @@ |
| @@ @@ |
| @@@@@@@ |
+-------------------------+
</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}}
<syntaxhighlight lang="action!">INCLUDE "H6:RGBCIRCL.ACT" ;from task Midpoint circle algorithm
 
RGB black,yellow,violet,blue
 
PROC DrawImage(RgbImage POINTER img BYTE x,y)
RGB POINTER ptr
BYTE i,j
 
ptr=img.data
FOR j=0 TO img.h-1
DO
FOR i=0 TO img.w-1
DO
IF RgbEqual(ptr,yellow) THEN
Color=1
ELSEIF RgbEqual(ptr,violet) THEN
Color=2
ELSEIF RgbEqual(ptr,blue) THEN
Color=3
ELSE
Color=0
FI
Plot(x+i,y+j)
ptr==+RGBSIZE
OD
OD
RETURN
 
PROC Main()
RgbImage img
BYTE CH=$02FC,width=[81],height=[51],st=[3]
BYTE ARRAY ptr(12393)
BYTE n
INT x,y
RGB POINTER col
 
Graphics(7+16)
SetColor(0,13,12) ;yellow
SetColor(1,4,8) ;violet
SetColor(2,8,6) ;blue
SetColor(4,0,0) ;black
 
RgbBlack(black)
RgbYellow(yellow)
RgbViolet(violet)
RgbBlue(blue)
 
InitRgbImage(img,width,height,ptr)
FillRgbImage(img,black)
 
FOR n=0 TO height/st
DO
IF n MOD 3=0 THEN col=yellow
ELSEIF n MOD 3=1 THEN col=violet
ELSE col=blue FI
RgbCircle(img,width/2,height/2,st*n,col)
OD
 
DrawImage(img,(160-width)/2,(96-height)/2)
 
DO UNTIL CH#$FF OD
CH=$FF
RETURN</syntaxhighlight>
{{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 39 ⟶ 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 70 ⟶ 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 106 ⟶ 288:
END # circle #;
 
SKIP</langsyntaxhighlight>'''File: test/Bitmap/Midpoint_circle_algorithm.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
Line 120 ⟶ 302:
(circle OF class image)(x, (8, 8), 5, (black OF class image));
(print OF class image)(x)
)</langsyntaxhighlight>
{{out}}
<pre>
Line 140 ⟶ 322:
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
</pre>
=={{header|BASIC256Applesoft BASIC}}==
<syntaxhighlight lang="gwbasic"> 10 GR : GOSUB 330: END
<lang basic256>fastgraphics
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</syntaxhighlight>
=={{header|ATS}}==
See [[Bresenham_tasks_in_ATS]].
 
=={{header|bash}}==
<syntaxhighlight lang="bash">#! /bin/bash
# Based on https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
 
function putpixel {
echo -en "\e[$2;$1H#"
}
 
function drawcircle {
x0=$1
y0=$2
radius=$3
 
for y in $( seq $((y0-radius)) $((y0+radius)) )
do
echo -en "\e[${y}H"
for x in $( seq $((x0+radius)) )
do
echo -n "-"
done
done
 
x=$((radius-1))
y=0
dx=1
dy=1
err=$((dx-(radius<<1)))
 
while [ $x -ge $y ]
do
putpixel $(( x0 + x )) $(( y0 + y ))
putpixel $(( x0 + y )) $(( y0 + x ))
putpixel $(( x0 - y )) $(( y0 + x ))
putpixel $(( x0 - x )) $(( y0 + y ))
putpixel $(( x0 - x )) $(( y0 - y ))
putpixel $(( x0 - y )) $(( y0 - x ))
putpixel $(( x0 + y )) $(( y0 - x ))
putpixel $(( x0 + x )) $(( y0 - y ))
 
if [ $err -le 0 ]
then
((++y))
((err+=dy))
((dy+=2))
fi
if [ $err -gt 0 ]
then
((--x))
((dx+=2))
((err+=dx-(radius<<1)))
fi
done
}
 
clear
drawcircle 13 13 11
echo -en "\e[H"
</syntaxhighlight>
{{Out}}
<pre>------#########------
----##---------##----
---#-------------#---
--#---------------#--
-#-----------------#-
-#-----------------#-
#-------------------#
#-------------------#
#-------------------#
#-------------------#
#-------------------#
#-------------------#
#-------------------#
#-------------------#
#-------------------#
-#-----------------#-
-#-----------------#-
--#---------------#--
---#-------------#---
----##---------##----
------#########------</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">fastgraphics
clg
color red
Line 179 ⟶ 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 255 ⟶ 529:
goto circle_loop
)
goto :EOF</langsyntaxhighlight>
{{Out}}
<pre> █ █
Line 288 ⟶ 562:
███████
</pre>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
[[Image:circle_bbc.gif|right]]
<langsyntaxhighlight lang="bbcbasic"> Width% = 200
Height% = 200
Line 334 ⟶ 607:
GCOL 1
LINE x%*2,y%*2,x%*2,y%*2
ENDPROC</langsyntaxhighlight>
=={{header|C}}==
 
== {{header|C}} ==
 
Interface:
 
<langsyntaxhighlight lang="c">void raster_circle(
image img,
unsigned int x0,
Line 347 ⟶ 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 394 ⟶ 666:
}
}
#undef plot</langsyntaxhighlight>
=={{header|C sharp|C#}}==
 
== {{header|C sharp}} ==
 
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 449 ⟶ 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 478 ⟶ 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 488 ⟶ 758:
@circle-points)]
(doseq [line grid]
(println (clojure.string/join line)))))</langsyntaxhighlight>
<pre>
Line 514 ⟶ 784:
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 542 ⟶ 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 575 ⟶ 845:
=={{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 615 ⟶ 885:
circle(img, 12, 12, 12, RGB.black);
img.textualShow;
}</langsyntaxhighlight>
{{out}}
<pre>.........#######.........
Line 642 ⟶ 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 681 ⟶ 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 725 ⟶ 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 757 ⟶ 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 842 ⟶ 1,223:
call draw_circle_toch(img%channel, c, radius, lum)
end subroutine draw_circle_sc</langsyntaxhighlight>
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">' version 0615-0710-20152016
' compile with: fbc -s consolegui
' OR compile with: fbc -s gui
 
' Variant with Integer-Based Arithmetic from Wikipedia page: Midpoint circle algorithm
' Midpoint circle algorithm
Sub circle_(x0 As Integer, y0 As Integer , radius As Integer, Col As Integer)
 
Dim As Integer x = radius
Dim As Integer y
Dim As Integer decisionOver2 = 1 - x ' Decision criterion divided by 2 evaluated at x=r, y=0
Dim As Integer decisionOver2 = 1 - x
 
While(x >= y)
PSet(x0 + x, y0 + y), col
PSet(x0 - x, y0 + y), col
PSet(x0 + x, y0 - y), col
PSet(x0 - x, y0 - y), col
PSet(x0 + y, y0 + x), col
PSet(x0 - y, y0 + x), col
PSet(x0 + y, y0 - x), col
PSet(x0 - y, y0 - x), col
y = y +1
If decisionOver2 <= 0 Then
decisionOver2 += y * 2 +1 ' Change in decision criterion for y -> y +1
Else
x = x -1
decisionOver2 += (y - x) * 2 +1 ' Change for y -> y +1, x -> x -1
End If
Wend
 
While(x >= y)
PSet(x0 + x, y0 + y), col
PSet(x0 - x, y0 + y), col
PSet(x0 + x, y0 - y), col
PSet(x0 - x, y0 - y), col
PSet(x0 + y, y0 + x), col
PSet(x0 - y, y0 + x), col
PSet(x0 + y, y0 - x), col
PSet(x0 - y, y0 - x), col
y = y + 1
If decisionOver2 <= 0 Then
decisionOver2 += y * 2 + 1 ' Change in decision criterion for y -> y+1
Else
x = x - 1
decisionOver2 += (y - x) * 2 + 1 ' Change for y -> y+1, x -> x-1
End If
Wend
End Sub
 
' ------=< MAIN >=------
 
ScreenRes 800, 800, 32
ScreenRes 600, 600, 32
Dim As Integer w, h, depth
Randomize Timer
Line 882 ⟶ 1,265:
ScreenInfo w, h
 
For i As Integer = 01 To 5010
circle_(Rnd * w, Rnd * h , Rnd * i * 4200 , Int(Rnd * &hFFFFFF))
Next
 
 
'save screen to BMP file
'save screen to BMP file
Bsave "Name.BMP", 0
BSave "Name.BMP", 0
 
 
' empty keyboard buffer
While InKeyInkey <> "" : Wend
Print : PrintWindowTitle "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.
<syntaxhighlight lang="futurebasic">
_wndW = 600
_wndH = 600
 
_window = 1
begin enum 1
_sliderBtn
_sliderValue
_cWell
_pixelView
_quitBtn
end enum
 
void local fn ViewDrawRect
CGRect viewRect, dotRect
CGContextRef ctx = fn GraphicsContextCurrentCGContext
viewRect = fn ViewBounds( _pixelView )
long sliderValue = fn ControlIntegerValue( _sliderBtn )
CGContextSaveGState( ctx )
CGContextSetLineWidth(ctx, 1.0)
CGContextSetRGBStrokeColor( ctx, 0.0, 0.0, 0.0, 1.0 )
CGContextStrokeRect( ctx, viewRect )
dotRect.origin.x = viewRect.size.width/2 - sliderValue
dotRect.origin.y = viewRect.size.height/2 - sliderValue
dotRect.size.width = 2 * sliderValue
dotRect.size.height = 2 * sliderValue
ColorRef col = fn ColorWellColor(_cWell)
CGColorRef myColor = fn ColorCGColor( col )
CGContextSetStrokeColorWithColor( ctx, myColor )
CGContextStrokeEllipseInRect( ctx, dotRect )
CGContextRestoreGState( ctx )
end fn
 
void local fn BuildWindow
window 1, @"DotView", ( 0, 0, _wndW, _wndH )
view subclass _pixelView, ( 0, 0, _wndH, _wndW)
ColorRef myColor = fn ColorWithRGB( 1.0, 0.0, 0.0, 1.0 )
colorwell _cWell,, myColor, ( 30, 30, 50, 30 )
ViewAddSubview( _pixelView, _cWell )
slider _sliderBtn,, 75, ( 170, 30, 250, 20 ), 1, 240
ControlSetContinuous( _sliderBtn, YES )
ViewAddSubview( _pixelView, _sliderBtn )
textlabel _sliderValue, @"75", ( 430, 35, 36, 17 )
ControlSetAlignment( _sliderValue, NSTextAlignmentCenter )
ViewAddSubview( _pixelView, _sliderValue )
button _quitBtn, , , @"Q", (_wndW - 50, 10, 40, 40),, NSBezelStyleCircular
ControlSetAction( _quitBtn, @"terminate:" )
end fn
 
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick
select (tag)
case _cWell : ViewSetNeedsDisplay( _pixelView )
case _sliderBtn
ControlTakeIntegerValueFrom( _sliderValue, _sliderBtn )
ViewSetNeedsDisplay( _pixelView )
end select
case _viewDrawRect
select ( tag )
case _pixelView : fn ViewDrawRect
end select
end select
end fn
 
on dialog fn DoDialog
fn BuildWindow
 
HandleEvents
</syntaxhighlight>
=={{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 932 ⟶ 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 954 ⟶ 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 986 ⟶ 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,019 ⟶ 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,032 ⟶ 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,060 ⟶ 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}}==
 
<syntaxhighlight lang="java">
== {{header|Java}} ==
 
<lang java>
import java.awt.Color;
 
Line 1,080 ⟶ 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,105 ⟶ 1,570:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<syntaxhighlight lang="julia">function drawcircle!(img::Matrix{T}, col::T, x0::Int, y0::Int, radius::Int) where T
x = radius - 1
y = 0
δx = δy = 1
er = δx - (radius << 1)
 
s = x + y
while x ≥ y
for opx in (+, -), opy in (+, -), el in (x, y)
@inbounds img[opx(x0, el) + 1, opy(y0, s - el) + 1] = col
end
if er ≤ 0
y += 1
er += δy
δy += 2
end
if er > 0
x -= 1
δx += 2
er += (-radius << 1) + δx
end
s = x + y
end
return img
end
 
# Test
using Images
 
img = fill(Gray(255.0), 25, 25);
drawcircle!(img, Gray(0.0), 12, 12, 12)</syntaxhighlight>
=={{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..
<syntaxhighlight lang="lua">function Bitmap:circle(x, y, r, c)
local dx, dy, err = r, 0, 1-r
while dx >= dy do
self:set(x+dx, y+dy, c)
self:set(x-dx, y+dy, c)
self:set(x+dx, y-dy, c)
self:set(x-dx, y-dy, c)
self:set(x+dy, y+dx, c)
self:set(x-dy, y+dx, c)
self:set(x+dy, y-dx, c)
self:set(x-dy, y-dx, c)
dy = dy + 1
if err < 0 then
err = err + 2 * dy + 1
else
dx, err = dx-1, err + 2 * (dy - dx) + 1
end
end
end</syntaxhighlight>
Demo:
<syntaxhighlight 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
for i = 0, self.height-1 do self:set(hw,i,"|") end
self:set(hw,hh,"+")
end
 
function Bitmap:render()
for y = 1, self.height do
print(table.concat(self.pixels[y]," "))
end
end
 
bitmap = Bitmap(25, 25)
bitmap:clear("·")
bitmap:axes()
bitmap:circle(12, 12, 11, "■")
bitmap:circle(12, 12, 8, "■")
bitmap:circle(12, 12, 5, "■")
bitmap:circle(12, 12, 2, "■")
bitmap:render()</syntaxhighlight>
{{out}}
<pre>· · · · · · · · · · · · | · · · · · · · · · · · ·
· · · · · · · · · ■ ■ ■ ■ ■ ■ ■ · · · · · · · · ·
· · · · · · · ■ ■ · · · | · · · ■ ■ · · · · · · ·
· · · · · ■ ■ · · · · · | · · · · · ■ ■ · · · · ·
· · · · ■ · · · · · ■ ■ ■ ■ ■ · · · · · ■ · · · ·
· · · ■ · · · · ■ ■ · · | · · ■ ■ · · · · ■ · · ·
· · · ■ · · ■ ■ · · · · | · · · · ■ ■ · · ■ · · ·
· · ■ · · · ■ · · · ■ ■ ■ ■ ■ · · · ■ · · · ■ · ·
· · ■ · · ■ · · · ■ · · | · · ■ · · · ■ · · ■ · ·
· ■ · · · ■ · · ■ · · · | · · · ■ · · ■ · · · ■ ·
· ■ · · ■ · · ■ · · · ■ ■ ■ · · · ■ · · ■ · · ■ ·
· ■ · · ■ · · ■ · · ■ · | · ■ · · ■ · · ■ · · ■ ·
- ■ - - ■ - - ■ - - ■ - + - ■ - - ■ - - ■ - - ■ -
· ■ · · ■ · · ■ · · ■ · | · ■ · · ■ · · ■ · · ■ ·
· ■ · · ■ · · ■ · · · ■ ■ ■ · · · ■ · · ■ · · ■ ·
· ■ · · · ■ · · ■ · · · | · · · ■ · · ■ · · · ■ ·
· · ■ · · ■ · · · ■ · · | · · ■ · · · ■ · · ■ · ·
· · ■ · · · ■ · · · ■ ■ ■ ■ ■ · · · ■ · · · ■ · ·
· · · ■ · · ■ ■ · · · · | · · · · ■ ■ · · ■ · · ·
· · · ■ · · · · ■ ■ · · | · · ■ ■ · · · · ■ · · ·
· · · · ■ · · · · · ■ ■ ■ ■ ■ · · · · · ■ · · · ·
· · · · · ■ ■ · · · · · | · · · · · ■ ■ · · · · ·
· · · · · · · ■ ■ · · · | · · · ■ ■ · · · · · · ·
· · · · · · · · · ■ ■ ■ ■ ■ ■ ■ · · · · · · · · ·
· · · · · · · · · · · · | · · · · · · · · · · · ·</pre>
=={{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,117 ⟶ 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,133 ⟶ 1,761:
color: Bitmap.Pixel);
 
END Circle.</langsyntaxhighlight>
<langsyntaxhighlight lang="modula3">MODULE Circle;
 
IMPORT Bitmap;
Line 1,174 ⟶ 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,188 ⟶ 1,816:
Circle.Draw(testpic, Bitmap.Point{16, 16}, 10, Bitmap.Black);
PPM.Create("testpic.ppm", testpic);
END Main.</langsyntaxhighlight>
=={{header|Nim}}==
{{trans|Ada}}
<syntaxhighlight lang="nim">import bitmap
 
proc setPixel(img: Image; x, y: int; color: Color) {.inline.} =
== {{Header|Perl 6}} ==
# Set a pixel at a given color.
{{trans|C}}
# Ignore if the point is outside of the image.
We'll augment the Pixel and Bitmap classes.
if x in 0..<img.w and y in 0..<img.h:
img[x, y] = color
 
<lang perl6>use MONKEY_TYPING;
augment class Pixel { method Str { "$.R $.G $.B" } }
augment class Bitmap {
method P3 {
join "\n", «P3 "$.width $.height" 255»,
do for ^$.height { join ' ', @.data[]»[$_] }
}
method raster-circle ( $x0, $y0, $r, Pixel $value ) {
my $f = 1 - $r;
my $ddF_x = 0;
my $ddF_y = -2 * $r;
my ($x, $y) = 0, $r;
self.set-pixel($x0, $y0 + $r, $value);
self.set-pixel($x0, $y0 - $r, $value);
self.set-pixel($x0 + $r, $y0, $value);
self.set-pixel($x0 - $r, $y0, $value);
while $x < $y {
if $f >= 0 {
$y--;
$ddF_y += 2;
$f += $ddF_y;
}
$x++;
$ddF_x += 2;
$f += $ddF_x + 1;
self.set-pixel($x0 + $x, $y0 + $y, $value);
self.set-pixel($x0 - $x, $y0 + $y, $value);
self.set-pixel($x0 + $x, $y0 - $y, $value);
self.set-pixel($x0 - $x, $y0 - $y, $value);
self.set-pixel($x0 + $y, $y0 + $x, $value);
self.set-pixel($x0 - $y, $y0 + $x, $value);
self.set-pixel($x0 + $y, $y0 - $x, $value);
self.set-pixel($x0 - $y, $y0 - $x, $value);
}
}
}</lang>
 
proc drawCircle(img: Image; center: Point; radius: Natural; color: Color) =
== {{Header|OCaml}} ==
## Draw a circle using midpoint circle algorithm.
 
var
f = 1 - radius
ddFX = 0
ddFY = -2 * radius
x = 0
y = radius
 
img.setPixel(center.x, center.y + radius, color)
img.setPixel(center.x, center.y - radius, color)
img.setPixel(center.x + radius, center.y, color)
img.setPixel(center.x - radius, center.y, color)
 
while x < y:
if f >= 0:
dec y
inc ddFY, 2
inc f, ddFY
inc x
inc ddFX, 2
inc f, ddFX + 1
 
img.setPixel(center.x + x, center.y + y, color)
img.setPixel(center.x - x, center.y + y, color)
img.setPixel(center.x + x, center.y - y, color)
img.setPixel(center.x - x, center.y - y, color)
img.setPixel(center.x + y, center.y + x, color)
img.setPixel(center.x - y, center.y + x, color)
img.setPixel(center.x + y, center.y - x, color)
img.setPixel(center.x - y, center.y - x, color)
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
when isMainModule:
var img = newImage(16, 16)
img.fill(White)
img.drawCircle((7, 7), 5, Black)
img.print()</syntaxhighlight>
 
{{out}}
<pre>................
................
.....HHHHH......
....H.....H.....
...H.......H....
..H.........H...
..H.........H...
..H.........H...
..H.........H...
..H.........H...
...H.......H....
....H.....H.....
.....HHHHH......
................
................
................</pre>
 
=={{Header|OCaml}}==
 
<langsyntaxhighlight lang="ocaml">let raster_circle ~img ~color ~c:(x0, y0) ~r =
let plot = put_pixel img color in
let x = 0
Line 1,259 ⟶ 1,915:
in
loop x y m
;;</langsyntaxhighlight>
=={{header|Perl}}==
<syntaxhighlight lang="perl"># 20220301 Perl programming solution
 
use strict;
use warnings;
 
use Algorithm::Line::Bresenham 'circle';
 
my @points;
my @circle = circle((10) x 3);
 
for (@circle) { $points[$_->[0]][$_->[1]] = '#' }
 
print join "\n", map { join '', map { $_ || ' ' } @$_ } @points</syntaxhighlight>
{{out}}
<pre>
#######
## ##
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
## ##
#######
</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>
Included as demo\rosetta\Bitmap_Circle.exw, resultsResults may be verified with demo\rosetta\viewppm.exw
<syntaxhighlight lang="phix">-- demo\rosetta\Bitmap_Circle.exw (runnable version)
<lang Phix>constant red = 0xff2020,
include ppm.e -- red, yellow, new_image(), write_ppm() -- (covers above requirements)
yellow = 0xffdf20
 
function SetPx(sequence img, atom x, atom y, integer colour)
if x>=1 and x<=length(img)
and y>=1 and y<=length(img[x]) then
img[x][y] = colour
Line 1,276 ⟶ 1,969:
end function
 
function Circle(sequence img, atom x, atom y, atom r, integer colour)
atom x1 = -r,
y1 = 0,
err = 2-2*r
if r>=0 then
-- Bresenham algorithm
Line 1,303 ⟶ 1,996:
 
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,333 ⟶ 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,389 ⟶ 2,080:
 
END CIRCLE;
</syntaxhighlight>
</lang>
{{out}} for three circles centered at the origin.
<pre>
Line 1,434 ⟶ 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 1,475 ⟶ 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 1,548 ⟶ 2,237:
+-------------------------+
'''
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Port of the Pyhton solution.
 
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/draw)
Line 1,597 ⟶ 2,285:
(draw-circle dc 12 12 12)
bm
</syntaxhighlight>
</lang>
=={{header|Raku}}==
(formerly Perl 6)
{{trans|C}}
We'll augment the Pixel and Bitmap classes from the [[Bitmap#Raku|Bitmap]] task.
 
<syntaxhighlight lang="raku" line>use MONKEY-TYPING;
 
class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
has Pixel @!data;
 
method fill(Pixel \p) {
@!data = p xx ($!width × $!height)
}
 
method pixel( \i where ^$!width, \j where ^$!height --> Pixel) is rw {
@!data[i × $!width + j]
}
 
method set-pixel (\i, \j, Pixel \p) {
self.pixel(i, j) = p;
}
 
method get-pixel (\i, \j) returns Pixel {
self.pixel(i, j);
}
}
 
augment class Pixel { method Str { "$.R $.G $.B" } }
augment class Bitmap {
method P3 {
join "\n", «P3 "$.width $.height" 255»,
do for ^($.width × $.height) { join ' ', @!data[$_] }
}
 
method raster-circle ( \x0, \y0, \r, Pixel \value ) {
my $ddF_x = 0;
my $ddF_y = -2 × r;
my $f = 1 - r;
my ($x, $y) = 0, r;
for flat (0 X 1,-1),(1,-1 X 0) -> \i, \j {
self.set-pixel(x0 + i×r, y0 + j×r, value)
}
 
while $x < $y {
($y--; $f += ($ddF_y += 2)) if $f ≥ 0;
$x++; $f += 1 + ($ddF_x += 2);
for flat (1,-1) X (1,-1) -> \i, \j {
self.set-pixel(x0 + i×$x, y0 + j×$y, value);
self.set-pixel(x0 + i×$y, y0 + j×$x, value);
}
}
}
}
 
my Bitmap $b = Bitmap.new( width => 32, height => 32);
$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;</syntaxhighlight>
=={{header|REXX}}==
Programming note: &nbsp; because of character output to a terminal screen, a circle appears to be elongated in the
Line 1,604 ⟶ 2,351:
<br>a good aspect ratio.
 
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.*/
call drawCircle 0, 0, 8, '#' /*plot 1st circle with pound character.*/
call drawCircle 0, 0, 11, '$' /* " 2nd " " dollar " */
call drawCircle 0, 0, 19, '@' /* " 3rd " " commercial at. */
border=2 2 /*BORDER: shows N extra grid points.*/
minX= minX - border*2; maxX= maxX + border*2 /*adjust min and max X to show border*/
minY= minY - border ; maxY= maxY +border border /* " " " " Y " " " */
if @.0.0==@. then @.0.0= '┼' /*maybe define the plot's axis origin. */
/*define the plot's horizontal grid──┐ */
do h=minX to maxX; if @.h.0==@. then @.h.0= '─'; end /* ◄───────────┘ */
do v=minY to maxY; if @.0.v==@. then @.0.v= '│'; end /* ◄──────────┐ */
/*define the plot's vertical grid───┘ */
do y=maxY by -1 to minY; _= /* [↓] draw grid from top ──► bottom.*/
do x=minX to maxX; _= _ || @.x.y /* ◄─── " " " left ──► right. */
end /*x*/ /* [↑] a grid row should be finished. */
say _ /*display a single row of the grid. */
Line 1,627 ⟶ 2,374:
/*──────────────────────────────────────────────────────────────────────────────────────*/
drawCircle: procedure expose @. minX maxX minY maxY
parse arg xx,yy,r 1 y,plotChar; fx= 1; fy= -2*r /*get X,Y coördinates*/
f= 1 - r
do x=0 while x<y /*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
if f>=0 then do; y= y - 1; fy= fy + 2; f= f + fy; end end /*▒*/
fx=fx+2; f=f+fx fx= fx + 2; f= f + fx /*▒*/
call plotPoint xx+x, yy+y /*▒*/
call plotPoint xx+y, yy+x /*▒*/
call plotPoint xx+y, yy-x /*▒*/
call plotPoint xx+x, yy-y /*▒*/
call plotPoint xx-y, yy+x /*▒*/
call plotPoint xx-x, yy+y /*▒*/
call plotPoint xx-x, yy-y /*▒*/
call plotPoint xx-y, yy-x /*▒*/
end /*x*/ /* [↑] place plot points ══► plot.▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
plotPoint: parse arg c,r; @.c.r=plotChar plotChar /*assign a character to be plotted. */
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:}}
'''output'''
<pre>
·······················│·······················
Line 1,693 ⟶ 2,440:
·······················│·······················
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">Pixel = Struct.new(:x, :y)
 
class Pixmap
Line 1,733 ⟶ 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 1,772 ⟶ 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 1,824 ⟶ 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 1,858 ⟶ 2,601:
 
return
</syntaxhighlight>
</lang>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color, ImageData
import "dome" for Window
 
class MidpointCircle {
construct new(width, height) {
Window.title = "Midpoint Circle"
Window.resize(width, height)
Canvas.resize(width, height)
_w = width
_h = height
_bmp = ImageData.create("midpoint circle", width, height)
}
 
init() {
fill(Color.pink)
drawCircle(200, 200, 100, Color.black)
drawCircle(200, 200, 50, Color.white)
_bmp.draw(0, 0)
}
 
fill(col) {
for (x in 0..._w) {
for (y in 0..._h) pset(x, y, col)
}
}
 
drawCircle(centerX, centerY, radius, circleColor) {
var d = ((5 - radius * 4)/4).truncate
var x = 0
var y = radius
while (true) {
pset(centerX + x, centerY + y, circleColor)
pset(centerX + x, centerY - y, circleColor)
pset(centerX - x, centerY + y, circleColor)
pset(centerX - x, centerY - y, circleColor)
pset(centerX + y, centerY + x, circleColor)
pset(centerX + y, centerY - x, circleColor)
pset(centerX - y, centerY + x, circleColor)
pset(centerX - y, centerY - x, circleColor)
if (d < 0) {
d = d + 2 * x + 1
} else {
d = d + 2 * (x - y) + 1
y = y - 1
}
x = x + 1
if (x > y) break
}
}
 
pset(x, y, col) { _bmp.pset(x, y, col) }
 
pget(x, y) { _bmp.pget(x, y) }
update() {}
 
draw(alpha) {}
}
 
var Game = MidpointCircle.new(400, 400)</syntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \include 'code' declarations
 
proc Circle(X0, Y0, Radius, Color); \Display a circle
Line 1,898 ⟶ 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 1,905 ⟶ 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 1,920 ⟶ 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