19
3
The size of the moon mystery
I am sure you have heard that the moon changes its size. When you're in love and you're lucky, the moon is almost twice in size compared to normal situations. Some people say the reason is the atmosphere which acts as a lens. Others think that it is only a matter of comparison to other objects such as trees nearby. Whatever explanation you read, it's quite subjective.
The size of the moon science
Ok, we're programmers, aren't we? We rely on facts, right? So here's the experiment:
- Take a nice camera which supports setting time and aperture manually.
- Set your camera to maximum zoom level.
- Go out, take some photographs of the moon in order to detect the best settings so that the moon is sharp and lighting is just fine.
- Remember the settings
- Take photos of the moon with those settings every time you think the moon is large or small.
- Calculate the size of the moon in pixels
The camera won't lie, would it? By counting the bright pixels we can effectively measure the size of the moon - at least in pixels.
If the size is the same across all photos, then it's a bug in our brain. If the size differs, then there's room for speculation
- the moon really grows (but what does it eat?)
- there's an atmospherical lens effect
- the moon has an elliptical curve and is sometimes nearer, sometimes further away from earth
- ...
But I'll leave that open until your task is completed. Of course you want to know in advance if your software can calculate the moon size accurately.
The task
Given a few optimized pictures of the moon, please calculate the size of the moon. The optimization is: the pixels are either black or white. Nothing in between. No antialiasing. That makes it easy, doesn't it?
The caveat: the moon is not always full, you know ... it can be a sickle! But even in shape of a sickle, the size of the moon is larger. So you'll calculate the full size, please.
- Your program takes a PNG as input, e.g. as file name command line argument, piped into
stdin
or as a Bitmap object (of a standard framework library) if you write a function instead of a program. - Your program works with any reasonable input bitmap size, not necessarily square. Minimum width and height of 150 pixels are guaranteed.
- The full moon covers at least 25% of the picture.
- Your program outputs the calculated size of the moon in pixels as if it were a full moon.
- We assume that the moon is a perfect sphere.
- The exact size is always an integer number, but you can output a decimal number if your calculation returns that.
- The accuracy should be between 98% and 102%. (That's rather a guess than something I could guarantee to be achievable. If you think it's too hard to reach, please leave a comment.)
Update:
- The center of the moon isn't necessarily in the middle of the picture.
- The minimum visible area is 5% of the moon or 1.25% of the total number of pixels.
- The picture is taken in a way that the whole moon would fit the image, i.e. the total number of pixels is an upper boundary for the moon size.
- The moon will not be cropped / clipped.
The samples
You can generate your own samples using the blend file if you like. I have created the following pictures for you. You can count pixels in a PNG file using WhitePixelCounter.exe (needs .NET) to check whether the image contains black and white pixels only and how many of them.
The following 256x256 pixel images differ in the amount of white pixels, but should all result in a calculated moon size of 16416 pixels.
And these 177x177 pixel images should return 10241 pixels. The images are basically the same, but this time a camera with a different focal length was used.
Non-square and non-centered samples with a result of 9988:
Oh, I don't have a reference implementation for now and I even don't know whether I am able to implement something. But in my brain there is a strong belief that tells me it must be mathematically solvable.
The rules
This is Code Golf. The shortest code on 2015-03-30 gets accepted.
9In all the examples, the center of the moon appears to be centered within the picture. Can we assume the moon will always be centered? – Digital Trauma – 2015-03-12T23:24:57.043
1your accuracy of +/-2% on area corresponds to +/-1% on diameter: example r=100 pixels, area = 10000pi; r=101 pixels, area = 10201pi. Your smaller image has r=72 therefore d=144 so it should just be possible. For images below d=100 I think the accuracy could not be met, however. – Level River St – 2015-03-12T23:49:32.197
@DigitalTrauma: the center needn't be in the middle. – Thomas Weller – 2015-03-13T16:58:14.960
@MartinBüttner: the minimum visible percentage is 5% of the moon or 1,25% of the picture. – Thomas Weller – 2015-03-13T17:17:49.910
@MartinBüttner: ok, I've updated the question, updated the blend file to produce non-square, non-centered images by default. You can download all images here (*.png.zip). Updated pixel counter as well: outputs some more information and checks the 1.25% rule.
– Thomas Weller – 2015-03-13T20:27:10.287@steveverrill: I have written a kind of "reference implementation". It fails if the sickle is pointing straight upwards, but for all other pictures, the area is from -1.66% to +1.88%. – Thomas Weller – 2015-03-13T22:17:07.440