The following rudimentary demo illustrates using TurtleGraphics in Processing and is a modification of work initially done by Natalie Freed: https://gist.githubusercontent.com/nataliefreed/8483050 . This technique of drawing is described in the header and is apparently popular when teaching younger students to write code. As simple as it may seem, Logo’s TurtleGraphics is capable of creating elaborate fractals that are difficult to reproduce with other programming languages.
The initial post ran all of the drawing code from the draw() loop, as is customary. However, the drawings ‘morphed’ over a period of time due to repeatedly drawing over the original, i.e. a multi-sided polygon approximated a circle over time. The source code was modified to use noLoop() in setup() and recursion (function calling itself) so that the object is only drawn once using repetitive lines. The number of recursions are set as a parameter in the initial function call inside of draw(), which no longer loops. This demo has room for enhancement to include additional TurtleGraphic functions.
//https://gist.githubusercontent.com/nataliefreed/8483050
// Turtle Graphics in Processing
// Natalie Freed, February 2013
// This program shows another way to think about moving
// through Processing's coordinate system. Instead of placing
// points on a grid, you can imagine yourself as being somewhere
// on the grid, facing a direction. You can move forward or turn.
// The drawn line follows behind you.
// Modified to use noLoop() with recursion September 2024
PVector loc; //current location
float orientation; //current orientation
boolean pd = true; //pen down
void polygon() {
// Use 4 repeats
fd(100);
lt(radians(120));
fd(100);
lt(radians(120));
fd(100);
lt(radians(120));
fd(100);
rt(radians(90));
}
void myTriangle() {
// Use 3 repeats
fd(100);
rt(radians(120));
}
void mySquare() {
// Use 4 repeats
fd(100);
rt(radians(90));
}
void star() {
// Use 5 repeats
fd(100);
rt(radians(144));
}
void myFlower() {
// Use 12 repeats
fd(100);
rt(radians(90));
rt(radians(120));
}
// Set pattern to be created here
void turtleGraphics(int repeats) {
if (repeats > 0) {
repeats -= 1;
//polygon();
//star();
//myTriangle();
//mySquare();
myFlower();
turtleGraphics(repeats); // recursion - draw lines again until repeats == 0
}
}
void setup() {
size(400, 400);
surface.setTitle("TurtleGraphics Demo");
strokeWeight(2);
loc = new PVector(width/2-50, height/2-50); //starting position is near center
noLoop(); // Drawing is done once using recursion
}
// Set number of repeats here.
void draw() {
turtleGraphics(12);
}
// Below are utility functions to calculate new positions and orientations
// when moving forward or turning. You don't need to change these.
//calculate positions when moving forward
void fd(float pixels) {
PVector start = loc;
PVector end = PVector.add(loc, polar(pixels, orientation));
if (pd == true) {
line(start, end);
}
loc = end;
}
//calculate new orientation
void lt(float theta) {
orientation += theta;
}
//calculate new orientation
void rt(float theta) {
orientation -= theta;
}
//jump directly to a specific position
void jumpTo(int x, int y) {
loc = new PVector(x, y);
}
//new line function with PVectors. used by forward function
void line(PVector a, PVector b) {
line(a.x, a.y, b.x, b.y);
}
//converts an angle and radius into a vector
PVector polar(float r, float theta) {
return new PVector(r*cos(theta), r*sin(-theta)); // negate y for left handed coordinate system
}
Output: