Jump to content

Total circles area: Difference between revisions

Updated to work with Nim 1.4: added "import sequtils"; corrected ".. <". Also replaced "map" with the simpler "mapIt". Added a template "sqr".
(Added 11l)
(Updated to work with Nim 1.4: added "import sequtils"; corrected ".. <". Also replaced "map" with the simpler "mapIt". Added a template "sqr".)
Line 1,773:
===Grid Sampling Version===
{{trans|Python}}
<lang nim>import futuresequtils
 
type Circle = tuple[x, y, r: float]
Line 1,804:
( 0.0152957411, 0.0638919221, 0.9771215985)]
 
template sqr(x: SomeNumber): SomeNumber = x * x
let xMin = min circles.map((c: Circle) => c.x - c.r)
 
let xMax = max circles.map((c: Circle) => c.x + c.r)
let yMinxMin = min circles.mapmapIt((c: Circle) => cit.yx - cit.r)
let yMaxxMax = max circles.mapmapIt((c: Circle) => cit.yx + cit.r)
let xMinyMin = min circles.mapmapIt((c: Circle) => cit.xy - cit.r)
let xMaxyMax = max circles.mapmapIt((c: Circle) => cit.xy + cit.r)
 
const boxSide = 500
Line 1,816 ⟶ 1,818:
var count = 0
 
for r in 0 .. < boxSide:
let y = yMin + float(r) * dy
for c in 0 .. < boxSide:
let x = xMin + float(c) * dx
for circle in circles:
if sqr(x -circle.x)*(x- circle.x) + sqr(y -circle.y)*(y- circle.y) <= sqr(circle.r*circle.r):
inc count
break
 
echo "Approximated area: ", float(count) * dx * dy</lang>
 
Output:
{{out}}
<pre>Approximated area: 221.1561559772003317e+0156155977200332</pre>
 
=={{header|Perl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.