Total circles area: Difference between revisions

m
→‎optimized: added/changed comments and whitespace, used a template for the output section.
m (→‎using all circles: added/changed comments and whitespace, used a template for the output section.)
m (→‎optimized: added/changed comments and whitespace, used a template for the output section.)
Line 2,451:
===optimized===
This REXX version elides any circle that is completely contained in another circle.
 
<br>Also, another optimization is the sorting of the circles by (descending) radii,
<br>this reduces the computation time (for overlapping circles) by around 25%.
 
<br>This, with other optimizations, makes this 2<sup>nd</sup> version about twice as fast as the 1<sup>st</sup>.
 
This version also has additional information displayed.
Line 2,460 ⟶ 2,462:
if box=='' | box==',' then box= -500 /*Not specified? Then use the default.*/
if dig=='' | dig==',' then dig= 12 /* " " " " " " */
verbose= box<0; box= abs(box); boxen= box+1 /*set a flag if we're in verbose mode. */
numeric digits dig /*have enough decimal digits for points*/
 
/* ══════x══════ ══════y══════ ═══radius═══ ══════x══════ ══════y══════ ═══radius═══*/
$=' 1.6417233788 1.6121789534 0.0848270516 -1.4944608174 1.2077959613 1.1039549836',
Line 2,476 ⟶ 2,479:
' 1.4685857879 -0.8347049536 1.3670667538 -0.6855727502 1.6465021616 1.0593087096',
' 0.0152957411 0.0638919221 0.9771215985 ' /*define circles with X, Y, and R.*/
 
circles= words($) % 3 /*figure out how many circles. */
if verbose then say 'There are' circles "circles." /*display the number of circles. */
parse var $ minX minY . 1 maxX maxY . /*assign minimum & maximum values.*/
 
do j=1 for circles; _= j * 3-2 - 2 /*assign some circles with datum. */
@x.j= word($, _); @y.j=word($, _ + 1)
@r.j=word($, _ + 2) / 1; @rr.j= @r.j **2
minX= min(minX, @x.j - @r.j); maxX= max(maxX, @x.j + @r.j)
minY= min(minY, @y.j - @r.j); maxY= max(maxY, @y.j + @r.j)
end /*j*/
 
do m=1 for circles /*sort the circles by their radii.*/
do n=m+1 to circles /* [↓] sort by descending radii.*/
if @r.n>@r.m then parse value @x.n @y.n @r.n @x.m @y.m @r.m with,
@x.m @y.m @r.m @x.n @y.n @r.n
Line 2,494 ⟶ 2,498:
end /*m*/
 
dx= (maxX-minX) / box; dy= (maxY-minY) / box /*compute the DX and DY values*/
w= length(circles) /*# in ►─ fully contained circles.*/
#in= 0
do j=1 for circles /*traipse through the J circles.*/
do k=1 for circles; if k==j | @r.j==0 then iterate /*ignore self and/or 0*/
Line 2,503 ⟶ 2,507:
@y.j-@r.j < @y.k-@r.k | @x.j+@r.j > @x.k+@r.k then iterate
if verbose then say 'Circle ' right(j,w) ' is contained in circle ' right(k,w)
@r.j= 0; #in= #in +1 1 /*elide this circle; and bump # in*/
end /*k*/
end /*j*/ /* [↑] elided overlapping circle.*/
Line 2,517 ⟶ 2,521:
do col=0 for boxen; x=minX + col*dx /* " " " " " column.*/
do k=1 for nC /*now process each new circle. */
if (x - @x.k)**2 + (y - @y.k)**2 <= @rr.k then do; #= #+1; leave; end
end /*k*/
end /*col*/
Line 2,524 ⟶ 2,528:
say 'Using ' box " boxes (which have " box**2 ' points) and ' dig " decimal digits,"
say 'the approximate area is: ' #*dx*dy</lang>
'''{{out|output''' |text=&nbsp; when using various number of boxes:}}
 
<br>[Output shown is a combination of several runs.]
<pre>
There are 25 circles.