Overview
In this assignment, you will create a program that will estimate the value of π.
Assignment
The figure above represents one quarter of a unit circle (a circle with a radius of 1). The area under the curve is equal to π/4 since the area of a unit circle is π x r2 = π x 12 = π. In this assignment, you will create your own quarter of a unit circle using a character to represent each pixel. A space indicates a pixel outside of the circle and a * indicates a pixel inside the circle. For example:
+-----------+
| *** |
| ***** |
| ******* |
| ******* |
| ******** |
| ******** |
| ********* |
| ********* |
| ********* |
+-----------+
The area in the quarter circle is equal to the ratio of the area in the quarter circle to the area of the square. The area of the square is 1 x 1 = 1, and the area under the curve is π/4 since it is a quarter of the unit circle.
The area can be approximated by counting the number of pixels. The above figure represents a 9 x 9 image. There are a total of 81 pixels in the image and 65 of them are in the quarter circle. Therefore, we can estimate π as: 4 x 65 / 81 = 3.201
By taking the ratio of all of the pixels that are in the quarter circle to the total number of pixels plotted, we can estimate the value of π. There are 81 pixels in the above figure. 65 of them are in the quarter circle, so we estimate π to be 3.201.
We can determine if a pixel is in the unit circle (and therefore should be displayed as an asterisk instead of a space) by calculating its distance from the center of the circle (the bottom left corner of the image). For example, the pixel shown in the following "image" as O is seven pixels above and four pixels to the right of the center of the circle.
+-----------+
| *** |
| ****O |
| ******* |
| ******* |
| ******** |
| ******** |
| ********* |
| ********* |
| ********* |
+-----------+
To determine if that pixel falls inside the circle, we can use the Pythagorean theorem:
distance = squareRoot(x2 + y2)
We use a radius of 8.5 pixels since we want the distance to the center of the pixel, so if the distance to O is 8.5 or less, the pixel is inside the unit circle. Here we have:
distance = squareRoot(42 + 72) = squareRoot(16 + 49) = squareRoot(65) = 8.06.
Write a program that
- Asks the user to enter the desired width (in pixels) of the simulated image to estimate π.
- Prints out an ASCII-art version of the quarter of a unit circle.
- Calculates an estimate for π based on the generated image.
- Asks the user to enter the minimum desired error for an estimate of π.
- Generates (but does not display) progressively larger images to estimate π with no more than the minimum desired error entered by the user.
- Displays the estimate of π and the total number of pixels for the image producing that estimate.
Requirements
- The program must be created in a class named
PiEstimate
that is in a package named the same as your MSOE username. - As long as the user enters an integer for the desired width (in pixels) used to generate the estimate, the program must not crash. If the user enters an invalid integer value, the program should report the error professionally and reprint the prompt.
- As long as the user enters a double for the desired minimum error, the program must not crash. If the user enters an invalid value, the program should report the error professionally and reprint the prompt.
Your program output should look like this:
Enter the desired width:
9
+-----------+
| *** |
| ***** |
| ******* |
| ******* |
| ******** |
| ******** |
| ********* |
| ********* |
| ********* |
+-----------+
4 * 65/81 = 3.2098765432098766
Enter the minimum desired error for the estimate of PI: 0.05
An estimate of 3.171875 was achieved with a width of 16.
Just for Fun
Once you have met the requirements for the assignment, you may wish to add the following enhancements:
- Create menu allowing the user to:
- Generate the ASCII-art image and individual estimate
- Find the image size needed to estimate π within a specified error threshold
- Have the program prompt the user to repeat without having to run the program again
- Plot the error as a function of image width
- Generate a comma-separated list of values corresponding to the width and estimate error
- Each line should contain
width, error
- Copy and paste the lines into a file with the extention
.csv
- Load into Excel and plot
Acknowledgement
This laboratory assignment was developed by Dr. Chris Taylor