Total circles area: Difference between revisions

Content added Content deleted
(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: Line 1,773:
===Grid Sampling Version===
===Grid Sampling Version===
{{trans|Python}}
{{trans|Python}}
<lang nim>import future
<lang nim>import sequtils


type Circle = tuple[x, y, r: float]
type Circle = tuple[x, y, r: float]
Line 1,804: Line 1,804:
( 0.0152957411, 0.0638919221, 0.9771215985)]
( 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 yMin = min circles.map((c: Circle) => c.y - c.r)
let xMin = min circles.mapIt(it.x - it.r)
let yMax = max circles.map((c: Circle) => c.y + c.r)
let xMax = max circles.mapIt(it.x + it.r)
let yMin = min circles.mapIt(it.y - it.r)
let yMax = max circles.mapIt(it.y + it.r)


const boxSide = 500
const boxSide = 500
Line 1,816: Line 1,818:
var count = 0
var count = 0


for r in 0 .. <boxSide:
for r in 0 ..< boxSide:
let y = yMin + float(r) * dy
let y = yMin + float(r) * dy
for c in 0 .. <boxSide:
for c in 0 ..< boxSide:
let x = xMin + float(c) * dx
let x = xMin + float(c) * dx
for circle in circles:
for circle in circles:
if (x-circle.x)*(x-circle.x) + (y-circle.y)*(y-circle.y) <= circle.r*circle.r:
if sqr(x - circle.x) + sqr(y - circle.y) <= sqr(circle.r):
inc count
inc count
break
break


echo "Approximated area: ", float(count) * dx * dy</lang>
echo "Approximated area: ", float(count) * dx * dy</lang>

Output:
{{out}}
<pre>Approximated area: 2.1561559772003317e+01</pre>
<pre>Approximated area: 21.56155977200332</pre>


=={{header|Perl}}==
=={{header|Perl}}==