Total circles area: Difference between revisions

Updated tags to circumvent a links problem
(Simplified the Task a little)
(Updated tags to circumvent a links problem)
Line 266:
Approximate area: 21.56503660593628004</pre>
Runtime is about 7.6 seconds. DMD 2.061, -O -release -inline -noboundscheck.
 
=={{header|Haskell}}==
===Haskell: Grid Sampling Version===
{{trans|Python}}
<lang haskell>data Circle = Circle { cx :: Double, cy :: Double, cr :: Double }
Line 326 ⟶ 327:
<pre>Approximated area: 21.564955642878786</pre>
 
===Haskell: Analytical Solution===
Breaking down circles to non-intersecting arcs and assemble zero winding paths, then calculate their areas. Pro: precision doesn't depend on a step size, so no need to wait longer for a more precise result; Con: probably not numerically stable in marginal situations, which can be catastrophic.
<lang haskell>{-# LANGUAGE GeneralizedNewtypeDeriving #-}
Line 692 ⟶ 693:
div: 16384 unk: 75301 est: 21.565017 wet: 21.560920 dry: 21.569115 diff: 0.008195 error: -7.683898e-011</pre>
Here the "diff" is calculated by subtracting the known wet and dry areas from the total area, and the "error" is the difference between that and the sum of the areas of the unknown blocks, to give a rough idea of how much floating point roundoff error we've accumulated.
 
=={{header|Python}}==
===Python: Grid Sampling Version===
This implements a regular grid sampling. For this problems this is more efficient than a Montecarlo sampling.
<lang python>from collections import namedtuple
Line 755 ⟶ 757:
<pre>Approximated area: 21.561559772</pre>
 
===Python: Scanline Conversion===
<lang python>from math import floor, ceil, sqrt
 
Line 824 ⟶ 826:
* And the square of the radius computed outside the main loops.
 
<lang python>from __future__ import division
from __future__ import division
from math import sqrt
from itertools import count
Line 833 ⟶ 834:
except:
pass
 
 
# Remove duplicates and sort, largest first.
Line 938:
if __name__ == '__main__':
main(circles)</lang>
The above is tested to work with python2Python v.2.7, python3Python3 and pypyPyPy.
 
{{out}}
<pre>python3 total_circle_area.py