diff --git a/.idea/misc.xml b/.idea/misc.xml index d15472f..045943f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index 9513c6f..d34df80 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ Today we know that ratio to be PI, or **Math.PI** in JAVA, but HOW? ![Inscribed Polygons](images/geometry003.png) * With this observation, and a little more geometry, we can create an algorithm to calculate the perimeter of larger and larger **inscribed** polygons. * Remember, the more sides, (n represents the number of sides in the polygon), the more accurate our perimeter estimation is, so let's do the math... -![General Equation](./images/geometry001.jpg) + +![General Equation](images/geometry001.jpg) * From the image above, you can see the **octagon** is divided into **isosceles triangles**, the bottom of which (labeled **s**) is the length we need to know. * If we draw a line from the circle center perpendicular to the base of the triangle **s**, we divide **s** in half, so it becomes ${1 \over 2}s$ as depicted. * From the picture, we can simplify our lives by assuming $h = 1$, and that the circle is a **Unit circle**. @@ -21,7 +22,10 @@ Today we know that ratio to be PI, or **Math.PI** in JAVA, but HOW? * WELL, we can calculate B, so let's start there. Remember, a Circle is **360°** and we are equally dividing that by **n** sides ($n = 8$ in the example). * So... $B = {360° \over 8}$ or $45°$ and $A = {1 \over 2} * B$ so $A = 22.5°$ in our example. * Now we can calculate ${1 \over 2}s$ if we recall $sin(A) = {{1 \over 2}s \over h}$ as shown in the below diagram of the triangle. - ![Geometry Reminder](images/geometry002.jpg) + +![Geometry Reminder](images/geometry002.jpg) + +* ...continued... * Solving for *s* we see $s = 2 * h * sin(A)$ * And since we agreed $h = 1$, that simplifies to $s = 2 * sin(A)$ * Now we have *s*, so to get the **polygon perimeter**, we just multiply by the number of sides to get it. diff --git a/src/ArchimedesPiMethod.java b/src/ArchimedesPiMethod.java index 0b6e780..0b4eea6 100644 --- a/src/ArchimedesPiMethod.java +++ b/src/ArchimedesPiMethod.java @@ -1,7 +1,22 @@ import java.util.Scanner; - +/* Chris Shortt's homework Java Assignment-002 */ public class ArchimedesPiMethod { public static void main(String[] args) { + while (true) { + System.out.println("please type the number of sides"); + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + if (n < 1){ + break; + } + double b = 360.0 / n; + double a = b / 2; + double s = 2 * Math.sin(Math.toRadians(a)); + double p = n * s; + double pi = p / 2; + System.out.printf("Our PI estimate is: %.10f %n", pi); + System.out.println(pi); + } } -} +} \ No newline at end of file