Total circles area: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (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 |
<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 |
let xMin = min circles.mapIt(it.x - it.r) |
||
let |
let xMax = max circles.mapIt(it.x + it.r) |
||
⚫ | |||
⚫ | |||
const boxSide = 500 |
const boxSide = 500 |
||
Line 1,816: | Line 1,818: | ||
var count = 0 |
var count = 0 |
||
for r in 0 .. |
for r in 0 ..< boxSide: |
||
let y = yMin + float(r) * dy |
let y = yMin + float(r) * dy |
||
for c in 0 .. |
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- |
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: |
<pre>Approximated area: 21.56155977200332</pre> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |