Draw a circle triangle square Increase the number
Draw a circle triangle square Increase the number of lines before coming back to the origin …
We need to calculate 1) How many lines? (= how many angles? ) N 2) The value of each angle? A Any relationship between them? ? ? N= 3 4 5 6 7 8 A= 60 90 108 120 ? ?
How to calculate an angle…? Use the fact that “The sum of all angles in a triangle is 180 degree” How many triangles in square, pentagon, … etc? T N= 3 4 5 6 7 8 T= 1 2 3 4 5 6
The number of lines (= number of angles) N= 3 4 5 6 7 8 5 6 The number of triangles in each shape T= 1 2 3 4 The sum of all angles in each shape S= 180 360 540 720 900 1080 128 135 An angle in each shape A= 60 90 108
The number of lines (= number of angles) N= 3 4 5 6 7 The number of triangles in each shape T= 1 2 3 4 180 360 540 720 An angle in each shape A= 60 90 108 N– 2 5 The sum of all angles in each shape S= 8 6 (N – 2) * 180 900 1080 (N – 2) * 180 / N 120 128 135
Given N, the number of lines of a shape Each angle in the shape, A is A = (N – 2) * 180 / N One more thing we have to be careful … A A A So, a turtle should turn its face 180 -A dgree
// this method will draw a n-polygon // n will be specified by a user public void draw. Polygon( int n, int len ) { int angle = (n - 2) * 180 / n; for(int k=0; k<n; k++) { forward( len ); turn( 180 - angle ); } }
Paint a shape There is no method to paint in Turtle class One way to paint is to increase pen width by using a method set. Pen. Width( int size );
- Slides: 8