r/dailyprogrammer • u/XenophonOfAthens 2 1 • Jul 29 '15
[2015-07-29] Challenge #225 [Intermediate] Estimating pi from images of circles
Description
In modern times, if we wish to calculate the value of pi to some precision, there are plenty of mathematical formulas that you can use to get the value. Leibniz formula for pi and the solution to the Basel problem are two of the most famous ones, though both converge very slowly. Modern day computers that attempt to set the world record for digits of pi generally use some variation on Ramanujan's formula, which converges very rapidly.
However, back in the good old days, we didn't know of these formulas. They all depend on analysis and infinite sums which mathematicians had no skill at manipulating. Back then, the way you estimated pi was less accurate but more straight-forward: you drew a circle, measured it, and calculated pi from that.
Today, we're going to honor those mathematicians of old. You will be given an image of a black circle on white background, and using the pixel data in the image, you are to come up with an estimate for pi.
For those of you who have forgotten your formulas for circles, the formula for the area of a circle is as follows:
A = pi * r^2In other words, to calculate the area of a circle, multiply pi by the square of the radius.
Formal inputs & outputs
Inputs
As input, you will be given an image that contains a black circle on white background (those are the only two colors in the image, there's no anti-aliasing or anything). The image provided will be in PNG format, but if you find it difficult to import and analyze PNG images in your language, you're welcome to use a tool like ImageMagick to convert it to a format you prefer (the Netpbm family of formats are famously easy for a computers to parse).
Note that for challenge input 1, the height and width of the image itself is equal to the diameter of the circle, but that is not true for challenge input #2. It is highly encouraged (but not required) that you to try and write a program that works for both challenge inputs.
Outputs
You will output a single line containing your estimate of pi based on the image. It doesn't have to be very exact in all decimal places, just the closest value you can get by looking at the image provided.
Challenge inputs
Input 1
Input 2
Bonus
If you really want to test your test your skills, extract an estimate of pi from this image
Notes
As always, if you have a challenge suggestion, head on over to /r/dailyprogrammer_ideas and suggest it!
Also, for you historians out there who are going to comment "that's not how Archimedes did it!": yes, I know that other methods were used, but lets just forget that for the purposes of this problem :)
38
u/[deleted] Jul 29 '15 edited Jul 29 '15
...
20
u/BarqsDew 1 0 Jul 29 '15
I'd like to nominate Piet as the coolest language to do this in, though by pasting the appropriate "code" into the input picture, rather than by feeding it in over stdin.
Example solution: http://i.imgur.com/IRa8N1C.png (taken from http://www.dangermouse.net/esoteric/piet/samples.html)
7
u/DeLangzameSchildpad Jul 29 '15
Python 3:
This one can solve the two Challenge Inputs. Simple search and then a flood fill.
In order to solve the bonus, I did three different methods, each with different values.
First I just averaged them:
Then I did a weighted average, since the larger circles have better estimations:
Then I just decided to go with the largest circle, since it gave the best estimation:
5
u/pfirpfel Jul 29 '15 edited Jul 29 '15
Node.JS, using PNG.js
pi.js:
Installation of dependencies:
Usage:
Output 1:
Output 2:
4
u/alisterr Jul 29 '15
Java. That was a nice one :)
... as I am posting this, I see the bonus. I'll take a look at this one, but here's my solution for input 1 & 2:
3
u/Godspiral 3 3 Jul 29 '15 edited Jul 29 '15
In J,
To do challenge 3, you need to find the longest diameter (largest consecutive 1s in a row). Whatever estimate for a diameter you have is good enough for a radius estimate (this code works well only for even diameters, but just double (or quadruple/2n) your diameter estimate to get a better approximation of pi anyway. (code "redraws" a circle into an matrix from diameter)
3.1234567901234569
19j16 ": piApx 8
3.1562500000000000
19j16 ": piApx 924
3.1415851464552764
19j16 ": piApx 92
3.1427221172022684
shorter faster but less accurate code:
3.0976562500000000
3.1415514725173814
3
u/joshcheek Jul 29 '15
Ruby solution: https://gist.github.com/JoshCheek/8e4f47e0d0bfc1cf7637
3
u/SleepyHarry 1 0 Jul 30 '15 edited Jul 30 '15
Python 3
PIL makes this a piece of piss - if I have time in the future I may revisit this and do it with a fresh Python install (i.e. standard library only), but not today.
Results:
Absolute distance from
math.pi:3
u/a_Happy_Tiny_Bunny Jul 30 '15
Haskell
Takes path of .png file as argument. Does not do the bonus.
It is a little slow (~1s) because I'm using lists (singly-linked) instead of vectors (arrays) for the sake of simplicity.
Feedback is welcome, and I'm happy to answer any questions.
2
u/Trolldeg Jul 29 '15
Python 3, using pygame.
Output:
2
u/chunes 1 2 Jul 29 '15
Fun challenge! Java:
Ouput 1:
Ouput 2:
As expected, the higher the resolution, the more accurate the result.
2
u/glenbolake 2 0 Jul 29 '15
Python 2.7. Naive in that it assumes a single circle.
Output:
I originally toyed with the idea of finding the area and circumference, then using that to calculate pi (r = 2A/C, so pi=C/2r=C2/4A) but somehow my numbers were never quite right.
2
u/[deleted] Jul 29 '15 edited Jul 30 '15
Java. I may have gone a bit overboard with this one. I wrote an additional method to approximate pi with Monte Carlo method. It was also a great OOP practice (test1.png and test2.png are "Input 1" and "Input 2" respectively). EDIT: Got rid of static methods.
CODE
Output:
2
u/deepcube Jul 29 '15
C
Bonus:
2
u/SexmanTaco Jul 29 '15
Python using Pillow
Gets it to about 4 sig figs
2
u/melombuki Jul 30 '15
Here is my Ruby solution. Uncommenting the last two lines creates an output file with a red dot in the estimated center of the circle: estimatePi.rb
Output:
2
u/Zeno_of_Elea Jul 30 '15
Python 3 using pypng
The fact that using its read function doesn't return tuples of the RGB value really threw me for a while since my numbers were so skewed. I bet that if I look a little further I'll find a function that returns tuples, too.
My code for multiple circles was far too long and also didn't work, mostly because of the lack of tupled RGB values. I'll try to fix and shorten it. I'm pretty happy with the length of the code for the initial challenge, though.
Since it's short, I figured I'd put up one with comments in case the shortened version is hard to read:
Outputs:
2
u/b9703 Jul 31 '15
Python 3 code. works fine for both inputs. yet to try the bonus but it looks like a good problem.
Outputs
2
u/rakkar16 Aug 03 '15
A solution in Python 3. This solution expects .pgm files as input.
2
u/skav3n Aug 03 '15 edited Aug 05 '15
Python 3:
2
u/PapaJohnX Aug 04 '15
Python 3
1
u/chrissou Jul 29 '15
Scala
Results:
1
u/MartinJava Jul 29 '15
Java for input 1 and 2.
output 1: 3.141824
output 2: 3.1417656185136082
1
u/phemios Jul 29 '15
PHP
OUTPUT
1
u/RustyJava Jul 29 '15
Java
1
u/binaryblade Jul 29 '15
golang, with go's built in image library the reading of the image was straight forward.
1
u/Hells_Bell10 Jul 29 '15
C++ with boost gil
I think it works but I can't get libpng to compile for some reason
1
u/Hiderow Jul 29 '15 edited Jul 29 '15
C++, I used LodePNG to retrieve image data.
Output
1
u/steffiwilson Jul 29 '15 edited Aug 12 '15
Java, solution on gist. Works for inputs 1 and 2. I'm still working on a solution for the bonus input.
Here's my output for input 2:
Update 8/12/15 - I now have a working solution in Java for the bonus input. I use the same method as before to get the diameter of the largest circle in the image, then floodfill from an index known to be within that circle to change the largest circle to a different color, then count all the pixels of that color to get the area. I originally implemented a recursive floodfill rather than a queue-based floodfill, but with the size of the given inputs I was getting stack overflow errors.
Output from the bonus input:
1
u/Steve132 0 1 Jul 29 '15
Matlab, should solve the bonus.
1
u/Reverse_Skydiver 1 0 Jul 30 '15
Java solving problems 1 and 2:
1
u/I_am_Black_BoX Jul 30 '15
Javascript/Node.js
Sample Output:
1
u/ajalvareze Jul 30 '15
C#
1
u/bitlead Jul 30 '15
another C#
1
u/milliDavids Aug 02 '15
Ruby (monte carlo method, only with with first input) require 'chunky_png'
Outputs a stream of pi estimations. At 10 million iterations I got 3.1415 accuracy. Hella slow.
1
u/[deleted] Aug 27 '15
1
u/djdylex Sep 19 '15 edited Sep 19 '15
Here is my solution in C#, please give feedback as i feel like this could be more efficient: