Bitmap/Midpoint circle algorithm: Difference between revisions

Content added Content deleted
(→‎{{Header|OCaml}}: Add Python)
(Add J)
Line 377: Line 377:
call draw_circle_toch(img%channel, c, radius, lum)
call draw_circle_toch(img%channel, c, radius, lum)
end subroutine draw_circle_sc</lang>
end subroutine draw_circle_sc</lang>

=={{header|J}}==
'''Solution:'''<br>
Using definitions from [[Basic bitmap storage#J|Basic bitmap storage]].
<lang j>NB.*getBresenhamCircle v Returns points for a circle given center and radius
NB. y is: y0 x0 radius
getBresenhamCircle=: monad define
'y0 x0 radius'=. y
x=. 0
y=. radius
f=. -. radius
pts=. 0 2$0
while. x <: y do.
pts=. pts , x , y
if. f >: 0 do.
y=. <:y
f=. f + _2&* y NB. _2 * y
end.
x=. >:x
f =. f + 2&(>:@*) x NB. 1 + 2 * x
end.
offsets=. (,|."1) (1 _1 {~ #: i.4) *"1"1 _ pts
~.,/ (x0,y0) +"1 offsets
)

NB.*drawCircle v Draws circle (x) on image (y)
NB. x is: 2-item list (y0 x0 radius) ; (color)
drawCircle=: (1&{:: ;~ [: ; [: <@getBresenhamCircle"1 (0&{::))@[ setPixels ]
</lang>

'''Example usage:'''
<lang j> myimg=: 0 255 0 makeRGB 25 25 NB. 25 by 25 green image
myimg=: (12 12 12 ; 255 0 0) drawCircle myimg NB. draw red circle with radius 12
viewRGB ((12 12 9 ,: 12 12 6) ; 0 0 255) drawCircle myimg NB. draw two more concentric circles

'+' (<"1 getBresenhamCircle 7 7 7)} 15 15$ ' '
+++++
++ ++
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
++ ++
+++++ </lang>


=={{header|Modula-3}}==
=={{header|Modula-3}}==