diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..2ec9d93 Binary files /dev/null and b/.DS_Store differ diff --git a/lec40_git/.DS_Store b/lec40_git/.DS_Store new file mode 100644 index 0000000..b4ff951 Binary files /dev/null and b/lec40_git/.DS_Store differ diff --git a/lec40_git/Purple_Turtle.java b/lec40_git/Purple_Turtle.java new file mode 100644 index 0000000..40ff1d2 --- /dev/null +++ b/lec40_git/Purple_Turtle.java @@ -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; + } +} \ No newline at end of file