r/dailyprogrammer • u/nint22 1 2 • Dec 05 '13
[12/05/13] Challenge #138 [Intermediate] Overlapping Circles
(Intermediate): Overlapping Circles
Computing the volume of a circle is pretty straight-forward: Pi x Radius x Radius, or simply Pi x r 2.
What if we wanted to computer the volume of two circles? Easy, just sum it! Yet, what about two intersecting circles, much like the classic Venn diagram?
Your goal is to write a program that takes two unit-circles (radius of one) at given locations, and compute that shape's volume. You must make sure to not double-count the intersecting volume! (i.e. you must not sum this red area twice).
As a starting point, check out how to compute circle segments.
Formal Inputs & Outputs
Input Description
On standard input you will be given four floating-point space-delimited values: x y u w. x and y are the first circle's position in Cartesian coordinates. The second pair u and w are the second circle's position.
Note that the given circles may not actually intersect. If this is the case, return the sum of both circles (which will always be Pi x 2 since our circles are unit-circles).
Output Description
Print the summed volume of the two circles, up to an accuracy of 4 digits after the decimal place.
Sample Inputs & Outputs
Sample Input
-0.5 0 0.5 0Sample Output
5.0548
8
u/rectal_smasher_2000 1 1 Dec 05 '13
volume or area?
edit:
also, don't we need the radius of the circles?edit2: radius is 1.
4
u/[deleted] Dec 05 '13
Is the sample output correct?
3
u/demon_ix 1 0 Dec 05 '13 edited Dec 05 '13
Are you sure about that test case?
The total area of un-overlapped circles would be 2 pi, which is about 6.28. Your circles touch each others' center, which is quite a bit more overlap. It seems unlikely the overlap area would be 6.28 - 6.1020 ~ 0.18.
My solution gives 5.0548 for that input.
Python.
Edit - Damnit, typed in fabs instead of sqrt...
28
u/pandubear 0 1 Dec 06 '13 edited Dec 06 '13
I had fun with this! My first attempt at obfuscated* C.
I used a "test each pixel" sort of approach to approximate the answer instead of just using a formula. You can change the resolution/precision by changing what
dis.* No special tricks in terms of obfuscation... (other than lots of
#definesand short variable names) Also very liberal use of whitespace that I suspect is frowned upon by obfuscators...18
u/prondose 0 0 Dec 06 '13
Perl:
6
u/[deleted] Dec 05 '13
Python
5
u/Edward_H Dec 05 '13
A solution in Managed COBOL:
5
u/lordtnt Dec 06 '13
why use the mathematic formula when you have computing power?
just pick 1 mil points in the square around circle A and count the number of points lie in both circle A and circle B. Dividing it by the number points in circle A and multiply by circle A's area gives the approx. overlapping area.
(more like a double check function. A triple check one would be integration but it's way more complicated than this)
suck runtime: 4.4 sec http://ideone.com/X1c2Lj
4
u/[deleted] Dec 05 '13
Just started learning Haskell... Usage: c (-0.5) 0 0.5 0
4
u/thinksInCode Dec 05 '13 edited Dec 10 '13
Man, I'm surprised I remembered anything from geometry/trigonometry.
EDIT: Derp, forgot to simplify the formula. EDIT2: Simplified further.
Java solution:
3
u/skeeto -9 8 Dec 05 '13
Elisp, using this equation,
Usage:
3
u/rectal_smasher_2000 1 1 Dec 05 '13
C++
i think this challenge should have been marked as easy, as it only requires the application of a relatively straightforward equation. also, when my c++ code is similar in length to solutions provided in scripting/functional languages - i begin to doubt my perceptions of reality.
3
u/IceDane 0 0 Dec 06 '13
Volume of a circle? Surely you are referring to the area of a circle?
3
u/tokou88 Dec 09 '13
Here's a very straightforward Go version
2
u/s7a1k3r Dec 05 '13 edited Dec 05 '13
2
u/eslag90 Dec 06 '13
Python 2.7, nothing special :)
2
u/mujjingun Dec 06 '13
A simple C solution:
2
u/dreugeworst Dec 06 '13
Damnit, I figured I would have a different solution from everyone else, and use arctan to compute the fraction of the circle that's shared, then find 2 ((1-frac) * pi + triangle_area), but after shuffling the terms of the resulting equation around, I ended up with virtually the same as everyone else. Damn you mathematics!
2
u/TheSpongeGod Dec 08 '13
Haskell:
2
u/TheGag96 Dec 08 '13
Java. I feel that this was just as easy in the long run as the braille one.
2
u/benzrf 0 0 Dec 08 '13
Haskell:
2
u/dobbybabee Dec 09 '13
This was done in c++.
2
u/Torcherist Dec 11 '13
C++
2
u/jnazario 2 0 Dec 13 '13
scala, largely a direct port of the py code from /u/kamelasher so it's not very functional/scala
example usage:
i recall this being an ACM programming contest question many moons ago.
2
u/vaibhavsagar Dec 14 '13
Straightforward and hopefully readable Python 3.
2
u/Taunk Dec 14 '13 edited Dec 14 '13
Cython: (this will give domain errors if they don't intersect)
Setup.py:
Area.pyx:
Usage:
2
u/brenny87 Dec 14 '13 edited Dec 15 '13
html/javascript
2
u/slackertwo Dec 17 '13
Ruby:
Output:
2
u/[deleted] Dec 20 '13
Toyed around with ipython, it's pretty sweet!
http://nbviewer.ipython.org/url/rc-racing.se/e/dp-i138.ipynb?create=1
2
u/ReginaldIII Dec 22 '13 edited Dec 22 '13
C++: Even though the direct equation is trivial to compute I decided to go for a more fun method solving by monte carlo simulation!
Edit: Removed sqrt from distance calculation and test against dist2 for performance
2
u/VerifiedMyEmail Dec 23 '13 edited Dec 23 '13
Javascript for the console
feedback is welcomed
2
u/agambrahma Jan 08 '14
C++ solution ... boring and straightforward
3
u/thinksInCode Dec 09 '13
Two dimensional shapes have no volume. Thus, the answer is always zero.
1
u/[deleted] Mar 04 '14
Python 2.7:
1
u/jeaton Jan 09 '14
Python:
0
u/[deleted] Dec 05 '13
[deleted] — view deleted comment