CS 1110/1111: Introduction to Programming

Lecture 6

Announcements

None yet. ☺

Talking Points

Continue with Lecture 5 material.

public static void main(String[] args) {
    World w = new World();
    Turtle t = new Turtle(w);
    for (int i = 0; i < 4; ++i) {
        t.forward(100);
        t.left(90);
    } 
}
public static void Triangle(Turtle dana) {
    int sides = 3; 
    for(int i = 0; i < sides; ++i) {
        dana.forward(100);
        dana.right(360/sides);
    }
}

public static void main(String[] args) {
    World w = new World();
    Turtle t = new Turtle(w);
    triangle(t);
    t.forward(50);
    triangle(t);
}
public static void polygon(Turtle red, int sides, int size) {
    for(int i = 0; i < sides; ++i) {
        red.forward(size);
        red.right(360/sides);
    }
}
public static void tree(Turtle t, int size) {
    if (size > 1) {
        // I'll do this if size > 1
        // draw a trunk with two trees on it
        t.forward(20); 
        t.left(30);
        tree(t, size - 1);
        t.right(60);
        tree(t, size - 1);
        t.left(30);
        t.backward(20);
        
    } else {
        // I'll do this otherwise (i.e., size ≤ 1)
        // draw a leaf
        t.dropPicture("http://www.tutorialsforblender3d.com/Textures/Leaf/textures/Leaf_8_Color.png", 15);
    }
    
}
public static void main(String[] args) {
    World w = new World(1000, 1000, Color.YELLOW);
    Turtle hans = new Turtle(w);
    hans.left(90);
    hans.setDelay(0);
    tree(hans, 7);
}
public static void tree(Turtle t, int bigness) {
    if (bigness == 1) {
        // we'll do this if bigness is 1
        // draw a leaf
        t.setColor(Color.GREEN);
        t.setPenWidth(27);
        t.forward(3);
        t.backward(3);
        t.setPenWidth(1);
        t.setColor(Color.BLACK);
    } else { // means otherwise
        // we'll do this if bigness is not 1
        // draw a trunk with two trees on it
        t.forward(27);
        t.left(27);
        tree(t, bigness - 1);
        t.right(27 * 2);
        tree(t, bigness - 1);
        t.left(27);
        t.backward(27);
    }
}
Copyright © 2014 by Luther Tychonievich. All rights reserved.