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