Turtle graphics
1 / 21

Turtle Graphics - PowerPoint PPT Presentation

  • Updated 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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
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 l.jpg
Turtle Graphics


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 l.jpg
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



Computer Graphics - Lecture 6

Turtle like graphics pens l.jpg
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 l.jpg
Design of the Class Turtle

public void moveTo(int x0,int 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)



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 l.jpg
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){








See the execution

public Turtle(int x0,int y0,double dir0){




public Turtle(){



public void forward(int d,Graphics g){

double x1,y1;



if (pen==1) g.drawLine((int)x,(int)y,(int)x1,(int)y1);



Computer Graphics - Lecture 6

Recursive methods l.jpg
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; }



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 l.jpg
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 l.jpg
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;







Computer Graphics - Lecture 6

Binary tree 2 l.jpg

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




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




public boolean action(Event event, Object arg){ repaint(); return true;


The declaration of the method Tree



Computer Graphics - Lecture 6

Koch s fractal l.jpg

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






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 l.jpg
Sierpinski’s Fractal (curve)

public void s(int n, int l, Graphics g){

int d=(int)l/Math.sqrt(2);









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 l.jpg
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:


Computer Graphics - Lecture 6