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

Requirements

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:

Acknowledgement

This laboratory assignment was developed by Dr. Chris Taylor

See your professor's instructions for details on submission guidelines and due dates.