turtle graphics
Download
Skip this Video
Download Presentation
Turtle Graphics

Loading in 2 Seconds...

play fullscreen
1 / 21

Turtle Graphics - PowerPoint PPT Presentation


  • 1137 Views
  • Uploaded on

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,... . Working with Java classes.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Turtle Graphics' - Audrey


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
turtle graphics
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,...

Computer Graphics - Lecture 6

working with java classes
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,…);

Computer Graphics - Lecture 6

turtle like graphics pens
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)

Computer Graphics - Lecture 6

design of the class turtle
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};

Computer Graphics - Lecture 6

implement and test the class turtle
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);

t.forward(100,g);t.right(PI/2);

t.forward(100,g);t.right(PI/2);

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;

}

Computer Graphics - Lecture 6

recursive methods
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];

}

Computer Graphics - Lecture 6

principles of turtle geometry
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.

Computer Graphics - Lecture 6

binary tree 1
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)

- construct T(n-1,l/2)

- left 45 (PI/4)

- 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);

}

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");

}

Binary Tree (2)

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

Computer Graphics - Lecture 6

koch s fractal
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)

- left 60 (PI/3); construct K(n-1,l/3)

The snow flake F(n,k)

- construct K(n,l); left 120 (2PI/3);

- construct K(n,l); left 120 (2PI/3);

- construct K(n,l); left 120 (2PI/3);

F(n,k) is a fractal representing an infinite curve bounding a finite area.

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);

t.left(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);

}

Computer Graphics - Lecture 6

sierpinski s fractal curve
Sierpinski’s Fractal (curve)

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);

s(n-1,l,g);

}

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

- construct S(n-1,l);

S(n,k) is a fractal representing an infinite curve bounded by finite area.

Computer Graphics - Lecture 6

problems to solve
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:

http://www.math.sunysb.edu/~scott/Book331/turtle_in_fractal.html

Computer Graphics - Lecture 6

ad