Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added lec40_git/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions lec40_git/Purple_Turtle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.awt.*;
import java.awt.geom.Line2D;

/**
* A simple "Turtle Graphics" style tool for students.
*/
public class Turtle {
private Graphics2D g2d;
private double x = 0;
private double y = 0;
private double heading = 0; // Degrees, 0 is East

public Turtle(Graphics2D g2d) {
this.g2d = g2d;
// Set default stroke
g2d.setStroke(new BasicStroke(3));
}

public void setColor(Color c) {
g2d.setColor(Color.MAGENTA);
}

public void forward(double pixels) {
double rad = Math.toRadians(heading);
double newX = x + Math.cos(rad) * pixels;
double newY = y + Math.sin(rad) * pixels;

g2d.draw(new Line2D.Double(x, y, newX, newY));

x = newX;
y = newY;
}

public void turnRight(double degrees) {
heading += degrees;
}

public void turnLeft(double degrees) {
heading -= degrees;
}
}