Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics - Lecture 6

Similar presentations


Presentation on theme: "Computer Graphics - Lecture 6"— Presentation transcript:

1 Computer Graphics - Lecture 6
Turtle Graphics Objectives To design and construct the class Turtle. To give some details about recursive methods. To present the principles of Turtle Graphics. To construct some Turtle images: trees, Koch curve, Serpinski curve,... 5/6/2019 Computer Graphics - Lecture 6

2 Working with Java classes
Constructing a class /Constructing the object this. - The class variables: private, … - The class constructors: Same name as the class; do not have returning type. Initialise the class’ variables. - The class methods: do an action on the class data. Type of methods: get/set, draw/print, …. Action on this  non-static; Create an object (call a constructor) : NameClass obj = new NameClass(a1,a2,…); Call a method obj.nameMethod(a1,a2,…); NameClass.nameMethod(a1,a2,…); 5/6/2019 Computer Graphics - Lecture 6

3 Turtle-like Graphics Pens
What can turtles (as animals) do? Move forward or backward. Turn left or right. Retreat the head into the shell. Variables used for our Turtle-like Graphics Pens. x, y  give the turtle pen coordinates. Work with doubles dir  the direction to go in radians. pen  the pen’s status (pen=1 draws line) 5/6/2019 Computer Graphics - Lecture 6

4 Design of the Class Turtle
public void moveTo(int x0,int y0) {x=x0;y=y0;} public void up(){pen=0;} public void down(){pen=1;} public void left(double a){dir+=a;} public void right(double a){dir-=a;} public void forward(int d,Graphics g){} public void backward(int d,Graphics g) {forward(-d,g);} } A Turtle object is a pen such that: - Has a direction to go - Can draw lines. public class Turtle{ public final static double PI=Math.PI; private double dir; private int pen; private double x,y; public Turtle(int x0,int y0,int dir0); public Turtle(); public double getDir(){return dir;} public int getX(){return x}; public int getY(){return y}; 5/6/2019 Computer Graphics - Lecture 6

5 Implement and Test the Class Turtle
import java.awt.*; import java.applet.*; public class L6Appl1 extends Applet{ Turtle t = new Turtle(); final double PI=Math.PI; public void paint(Graphics g) { t.moveTo(20,20); t.forward(100,g);t.right(PI/2); } See the execution public Turtle(int x0,int y0,double dir0){ x=x0;y=y0;dir=dir0; pen=1; } public Turtle(){ x=250;y=250;dir=0;pen=1; public void forward(int d,Graphics g){ double x1,y1; x1=(int)(x+d*Math.cos(dir)); y1=(int)(y+d*Math.sin(dir)); if (pen==1) g.drawLine((int)x,(int)y,(int)x1,(int)y1); x=x1;y=y1; 5/6/2019 Computer Graphics - Lecture 6

6 Computer Graphics - Lecture 6
Recursive Methods A method can call any (known) methods from the class or libraries. A method is recursive when invokes itself. Important Rule: A recursive method must have a termination step that solves directly the problem. type myRecMethod(type1 arg1, type2 arg2, …. ){ if (termination) {calculate directly the result res; return res; } myRecMethod(a1,a2,…) } int sum (int n, int [] a){ if(n==0) return 0; // termination step int s1 = sum(n-1,a); // recursive call return s1+a[n-1]; 5/6/2019 Computer Graphics - Lecture 6

7 Principles of Turtle Geometry
1. Define Recursively the figure Fn. - termination step: give the form of F0 - define Fn based on Fn-1. 2. Use a Turtle object to draw the figure. - the direction of the Turtle object must be the same. Turtle Geometry represents the simplest way to construct fractals. 5/6/2019 Computer Graphics - Lecture 6

8 Computer Graphics - Lecture 6
Binary Tree (1) Consider T(n,l) the binary tree. n - the order n; l - the length. T(0,l)= a line with the length l. T(n,l) is defined as follows: - construct the trunk - left 45 (PI/4) - construct T(n-1,l/2) - right 90 (PI/2) - go back at the root public void tree(int n, int l, Graphics g){ if(l==1 || n==0) return; t.forward(l,g); t.left(PI/4);tree(n-1,l/2,g); t.right(PI/2);tree(n-1,l/2,g); t.left(PI/4); t.backward(l,g); } 5/6/2019 Computer Graphics - Lecture 6

9 Computer Graphics - Lecture 6
Binary Tree (2) import java.awt.*; import java.applet.*; import Turtle; public class L6Appl2 extends Applet{ TextField textField1, textField2; Turtle t = new Turtle(); public void init(){ textField1 = new TextField(10); textField2 = new TextField(10); add(textField1); add(textField2); textField1.setText("2"); textField2.setText("200"); } public void paint(Graphics g){ String s = textField1.getText(); int order = Integer.parseInt(s); s = textField2.getText(); int length = Integer.parseInt(s); t.moveTo(10,300); tree(order,length,g); } public boolean action(Event event, Object arg){ repaint(); return true; The declaration of the method Tree Execution 5/6/2019 Computer Graphics - Lecture 6

10 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

11 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

12 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

13 Computer Graphics - Lecture 6
Koch’s Fractal public void koch(int n, int l, Graphics g){ if(l<2 || n==0){t.forward(l,g);return;} koch(n-1,l/3,g); t.left(PI/3);koch(n-1,l/3,g); t.right(2*PI/3);koch(n-1,l/3,g); } public void flake(int n, int l, Graphics g){ for(int i=0;i<3;i++){ koch(n,l,g); t.left(2*PI/3); Consider K(n,l) the Koch’s curve. K(0,l)= a line. K(n,l) is defined as follows: - construct K(n-1,l/3); - left 60 (PI/3); construct K(n-1,l/3) - right 120 (2PI/3); construct K(n-1,l/3) The snow flake F(n,k) - construct K(n,l); left 120 (2PI/3); F(n,k) is a fractal representing an infinite curve bounding a finite area. 5/6/2019 Computer Graphics - Lecture 6

14 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

15 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

16 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

17 Sierpinski’s Fractal (curve)
Consider S(n,l) the Sierpinski’s curve. Find d=sqrt(l); S(0,l) = nothing. S(n,l) is defined as follows: - construct S(n-1,l); right 45; forward d - right 45; construct S(n-1,l); - left 90; forward l; left 90; - construct S(n-1,l); - right 45;forward d; right 45 S(n,k) is a fractal representing an infinite curve bounded by finite area. public void s(int n, int l, Graphics g){ int d=(int)l/Math.sqrt(2); if(n==0)return; s(n-1,l,g);t.right(PI/4);t.forward((int)d,g); t.right(PI/4);s(n-1,l,g); t.left(PI/2);t.forward(l,g);t.left(PI/2); s(n-1,l,g); t.right(PI/4);t.forward((int)d,g);t.right(PI/4); } 5/6/2019 Computer Graphics - Lecture 6

18 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

19 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

20 Computer Graphics - Lecture 6
5/6/2019 Computer Graphics - Lecture 6

21 Computer Graphics - Lecture 6
Problems to Solve Write an Applet to draw the Curve C(n,l). C(0,l) is a line with the length l. C(n,l) is defined construct C(n-1,l); right 90; construct C(n-1,l);left 90; Define yourself and construct a tree with 4 branches. Problems to read: Recursive Methods: Java Black Book, Using recursion, pp 192. An advanced topic about Turtle fractals: 5/6/2019 Computer Graphics - Lecture 6


Download ppt "Computer Graphics - Lecture 6"

Similar presentations


Ads by Google