Turtle Graphics

“Turtle Graphics“ is a conceptual model for teaching computational thinking, based on a “first-person cursor”. In this schema, developed in the 1960s by computer scientist and constructivist educator Seymour Papert, and inspired by Jean Piaget‘s studies of children’s learning and embodied cognition, vector graphics are produced using a relative cursor (the “turtle”) upon a Cartesian plane.

The Turtle object has the following methods or API (application programmer’s interface):

Turtle Graphics API for 15104

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

// makeTurtle(x, y) -- make a turtle at x, y, facing right, pen down

// left(d) -- turn left by d degrees

// right(d) -- turn right by d degrees

// forward(p) -- move forward by p pixels

// back(p) -- move back by p pixels

// penDown() -- pen down

// penUp() -- pen up

// goto(x, y) -- go straight to this location

// setColor(color) -- set the drawing color

// setWeight(w) -- set line width to w

// face(d) -- turn to this absolute direction in degrees

// angleTo(x, y) -- what is the angle from my heading to location x, y?

// turnToward(x, y, d) -- turn by d degrees toward location x, y

// distanceTo(x, y) -- how far is it to location x, y?

And here is the actual Turtle Graphics implementation for p5.js:

Turtle Implementation for 15104

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

functionturtleLeft(d){

this.angle-=d;

}

functionturtleRight(d){

this.angle+=d;

}

functionturtleForward(p){

varrad=radians(this.angle);

varnewx=this.x+cos(rad)*p;

varnewy=this.y+sin(rad)*p;

this.goto(newx,newy);

}

functionturtleBack(p){

this.forward(-p);

}

functionturtlePenDown(){

this.penIsDown=true;

}

functionturtlePenUp(){

this.penIsDown=false;

}

functionturtleGoTo(x,y){

if(this.penIsDown){

stroke(this.color);

strokeWeight(this.weight);

line(this.x,this.y,x,y);

}

this.x=x;

this.y=y;

}

functionturtleDistTo(x,y){

returnsqrt(sq(this.x-x)+sq(this.y-y));

}

functionturtleAngleTo(x,y){

varabsAngle=degrees(atan2(y-this.y,x-this.x));

varangle=((absAngle-this.angle)+360)%360.0;

returnangle;

}

functionturtleTurnToward(x,y,d){

varangle=this.angleTo(x,y);

if(angle<180){

this.angle+=d;

}else{

this.angle-=d;

}

}

functionturtleSetColor(c){

this.color=c;

}

functionturtleSetWeight(w){

this.weight=w;

}

functionturtleFace(angle){

this.angle=angle;

}

functionmakeTurtle(tx,ty){

varturtle={x:tx,y:ty,

angle:0.0,

penIsDown:true,

color:color(128),

weight:1,

left:turtleLeft,right:turtleRight,

forward:turtleForward,back:turtleBack,

penDown:turtlePenDown,penUp:turtlePenUp,

goto:turtleGoTo,angleto:turtleAngleTo,

turnToward:turtleTurnToward,

distanceTo:turtleDistTo,angleTo:turtleAngleTo,

setColor:turtleSetColor,setWeight:turtleSetWeight,

face:turtleFace};

returnturtle;

}

Turtle Example 1

Here is an example using Turtle Graphics. Note that in order to post such code on WordPress, the entire Turtle object code must be included into your p5.js sketch file. Note that the Turtle code has been “minified” to save space.

Turtle Example 2

If you want to see animated turtles, you can issue turtle commands in draw() without erasing (calling background()). In this case, draw becomes your loop. To get a turtle whose behavior changes over time, you would need to give it commands whose values were based on things like the millis(), random(), frameCount, or other progressively-modified global variables.

Turtle Example 3

Here’s an example of three Turtles (red, green and blue) chasing a gray “target”, whose location is based on Perlin noise. The turtles “overshoot” the target because they always move forward but can only turn 1 degree per frame (plus or minus a random 5 degrees to introduce some wandering into their paths).