-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtle.pde
140 lines (121 loc) · 3.48 KB
/
Turtle.pde
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2010 Shrutarshi Basu
// This file is part of Lisip.
// Lisip is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Lisip is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Lisip. If not, see <http://www.gnu.org/licenses/>.
class Turtle
{
float X, Y, angle;
ArrayList posX, posY, posAngle;
int positions;
Turtle()
{
X = width/2;
Y = height/2;
angle = 0;
posX = new ArrayList();
posY = new ArrayList();
posAngle = new ArrayList();
positions = -1;
}
Turtle(float startX, float startY)
{
X = startX;
Y = startY;
angle = 0;
posX = new ArrayList();
posY = new ArrayList();
posAngle = new ArrayList();
positions = -1;
}
void moveTo(float newX, float newY, float newAngle)
{
X = newX;
Y = newY;
angle = newAngle;
}
void forward(float distance)
{
float newX = X + distance*cos(radians(angle));
float newY = Y + distance*sin(radians(angle));
line(X,Y, newX,newY);
X = newX;
Y = newY;
}
void backward(float distance)
{
float newX = X + distance*cos(radians(angle));
float newY = Y + distance*sin(radians(angle));
line(X,Y, newX,newY);
X = newX;
Y = newY;
}
void jumpf(float distance)
{
float newX = X + distance*cos(radians(angle));
float newY = Y + distance*sin(radians(angle));
X = newX;
Y = newY;
}
void jumpb(float distance)
{
float newX = X - distance*cos(radians(angle));
float newY = Y - distance*sin(radians(angle));
X = newX;
Y = newY;
}
void left(float difference)
{
angle += difference;
}
void right(float difference)
{
angle -= difference;
}
void push()
{
posX.add(X);
posY.add(Y);
posAngle.add(angle);
positions++;
}
void pop()
{
X = (Float)posX.remove(positions);
Y = (Float)posY.remove(positions);
angle = (Float)posAngle.remove(positions);
positions--;
}
void execute(String steps)
{
String step;
Scanner sc = new Scanner(steps);
while(sc.hasNext()) {
step = sc.next();
if (step.equals("forward")) {
forward(sc.nextFloat());
} else if (step.equals("backward")) {
backward(sc.nextFloat());
} else if (step.equals("left")) {
left(sc.nextFloat());
} else if (step.equals("right")) {
right(sc.nextFloat());
} else if (step.equals("jumpf")) {
jumpf(sc.nextFloat());
} else if (step.equals("jumpb")) {
jumpb(sc.nextFloat());
} else if (step.equals("push")) {
push();
} else if (step.equals("pop")) {
pop();
}
}
}
}