/** * Simple turtle with position and direction. * * Inspired by turtle graphics. */ class Turtle { // position float x; float y; // direction in degree float angle = 0; // default constructor with centered position Turtle() { x = width / 2; y = height / 2; } // constructor with given position Turtle(float _x, float _y) { x = _x; y = _y; } // moves this Turtle forward void forward(float distance) { x += distance * cos(radians(angle)); y += distance * sin(radians(angle)); } // moves this Turtle backwards void back(float distance) { x -= distance * cos(radians(angle)); y -= distance * sin(radians(angle)); } // changes direction clockwise void right(float angle) { this.angle += angle; if (angle >= 360) { angle -= 360; } } // changes direction counter-clockwise void left(float degree) { this.angle -= degree; if (angle <= 0) { angle += 360; } } // information on current state of this Turtle String toString() { return "Turtle " + angle + "° (" + (int)x + "," + (int)y + ")"; } }