-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTurtle.java
More file actions
273 lines (237 loc) · 9.61 KB
/
Turtle.java
File metadata and controls
273 lines (237 loc) · 9.61 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package lvp.views;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Locale;
import lvp.Clerk;
import lvp.skills.Interaction;
import lvp.skills.Text;
/**
* Turtle ermöglicht das Erstellen einfacher Turtle-Grafiken als SVG-Datei.
* Das Koordinatensystem ist kartesisch (0°=rechts, Winkel gegen den Uhrzeigersinn,
* Y-Achse positiv nach oben). SVG verwendet hingegen eine Y-Achse, die nach unten zeigt.
* Daher werden die Y-Koordinaten beim Export invertiert.
* Die einzelnen graphischen Elemente werden durchnummeriert in der Reihenfolge ihrer Erzeugung.
*/
public class Turtle implements Clerk{
public final String ID = Clerk.getHashID(this);
private final double xFrom, yFrom, viewWidth, viewHeight;
private final List<Element> elements = new ArrayList<>();
private int elementCounter = 0;
private State state;
private final Deque<State> stack = new ArrayDeque<>();
public Turtle() {
this(500, 500);
}
public Turtle(int width, int height) {
this(0, width, 0, height, width / 2.0, height / 2.0, 0);
}
/**
* @param xFrom linke Begrenzung des Sichtbereichs
* @param xTo rechte Begrenzung des Sichtbereichs
* @param yFrom untere Begrenzung des Sichtbereichs
* @param yTo obere Begrenzung des Sichtbereichs
* @param startX Start-X-Koordinate der Turtle
* @param startY Start-Y-Koordinate der Turtle
* @param startAngle Blickrichtung in Grad (0°=rechts, 90°=oben, gegen den Uhrzeigersinn)
*/
public Turtle(double xFrom, double xTo, double yFrom, double yTo,
double startX, double startY, double startAngle) {
this.xFrom = xFrom;
this.yFrom = yFrom;
this.viewWidth = xTo - xFrom;
this.viewHeight = yTo - yFrom;
this.state = new State(
startX, startY, startAngle,
new Color(0, 0, 0, 1.0), 1.0, true);
}
public Turtle penUp() {
state = state.withPenDown(false);
return this;
}
public Turtle penDown() {
state = state.withPenDown(true);
return this;
}
public Turtle forward(double distance) {
double rad = Math.toRadians(state.angle());
double dx = Math.cos(rad) * distance;
double dy = Math.sin(rad) * distance;
double newX = state.x() + dx;
double newY = state.y() + dy;
if (state.penDown()) {
elements.add(new Line(++elementCounter,
state.x(), state.y(), newX, newY,
state.color(), state.width()));
}
state = state.withPosition(newX, newY);
return this;
}
public Turtle backward(double distance) {
forward(-distance);
return this;
}
public Turtle right(double angle) {
// Normalize angle to be in [0, 360)
double newAngle = state.angle() - angle;
state = state.withAngle((newAngle % 360 + 360) % 360);
return this;
}
public Turtle left(double angle) {
// Normalize angle to be in [0, 360)
double newAngle = state.angle() + angle;
state = state.withAngle((newAngle % 360 + 360) % 360);
return this;
}
public Turtle color(int r, int g, int b, double a) {
if (!(0 <= r && r <= 255 && 0 <= g && g <= 255 && 0 <= b && b <= 255 && 0 <= a && a <= 1))
throw new IllegalArgumentException(
String.format(Locale.US, "Invalid color values: r=%d, g=%d, b=%d, a=%.2f. " +
"RGB must be [0,255], alpha must be [0.0,1.0].", r, g, b, a)
);
state = state.withColor(new Color(r, g, b, a));
return this;
}
public Turtle color(int r, int g, int b) {
return color(r, g, b, state.color().a());
}
public Turtle text(String text) {
return text(text, "16px sans-serif");
}
public Turtle text(String text, String font) {
double rad = Math.toRadians(state.angle());
double dx = Math.cos(rad);
double dy = Math.sin(rad);
elements.add(new SvgText(++elementCounter, text,
state.x(), state.y(), dx, dy, state.color(), font));
return this;
}
public Turtle lineWidth(double w) {
return width(w);
}
public Turtle width(double w) {
state = state.withWidth(w);
return this;
}
public Turtle push() {
stack.push(state);
return this;
}
public Turtle pop() {
if (stack.isEmpty()) {
throw new IllegalStateException("Cannot pop from an empty turtle state stack.");
}
state = stack.pop();
return this;
}
public Turtle write() {
Clerk.write(Text.fillOut("<div id='turtle${0}'>${1}</div>", ID, toString()));
return this;
}
public Turtle timelineSlider() {
Clerk.write(Text.fillOut("""
<div>
Linien sichtbar: <span id="currentLine${0}">${1}</span> / <span>${1}</span>
</div>
""", ID, elements.size()));
Clerk.write(
Interaction.slider(ID, 0, elements.size(), elements.size(), Text.fillOut("""
((e) => {
const n = Math.round(e.target.value);
const statusCurrent = document.getElementById("currentLine${0}");
statusCurrent.textContent = n;
const svgElement = document.getElementById("turtle${0}");
const lineIds = Array.from(svgElement.querySelectorAll("[svg-id]")).map(el => el.getAttribute("svg-id"));
lineIds.forEach((id,i) => {
const el = svgElement.querySelector(`[svg-id="` + id + `"]`);
if (el) el.style.display = i < n ? "" : "none";
});
})(event)
""", ID))
);
return this;
}
public void save(String filename) throws IOException {
Path path = Path.of(filename);
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
writer.write(
String.format(Locale.US,
"""
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="%.2f %.2f %.2f %.2f">
""", xFrom, yFrom, viewWidth, viewHeight)
);
for (Element e : elements) {
writer.write(elementString(e));
}
writer.write("</svg>\n");
}
}
public void save() throws IOException { save("output.svg"); }
@Override
public String toString() {
String out = String.format(Locale.US,
"""
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="%.2f %.2f %.2f %.2f">
""", xFrom, yFrom, viewWidth, viewHeight);
for (Element e : elements) {
out += elementString(e);
}
out += "</svg>\n";
return out;
}
private String elementString(Element e) {
return switch (e) {
case Line line -> {
double y1Svg = (viewHeight - (line.y1() - yFrom)) + yFrom;
double y2Svg = (viewHeight - (line.y2() - yFrom)) + yFrom;
yield String.format(Locale.US,
"""
<line svg-id="%d" x1="%.2f" y1="%.2f" x2="%.2f" y2="%.2f"
stroke="rgba(%d,%d,%d,%.2f)" stroke-width="%.2f" />
""",
line.id(), line.x1(), y1Svg, line.x2(), y2Svg,
line.color().r(), line.color().g(), line.color().b(), line.color().a(),
line.width());
}
case SvgText text -> {
double ySvg = (viewHeight - (text.y() - yFrom)) + yFrom;
yield String.format(Locale.US,
"""
<text svg-id="%d" x="%.2f" y="%.2f" dx="%.2f" dy="%.2f"
style="fill: rgba(%d,%d,%d,%.2f); font:%s;">%s</text>
""",
text.id(), text.x(), ySvg, text.dx(), text.dy(),
text.color().r(), text.color().g(), text.color().b(), text.color().a(),
text.font(), text.text());
}
};
}
private static record State(double x, double y, double angle, Color color, double width, boolean penDown) {
public State withPosition(double newX, double newY) {
return new State(newX, newY, angle, color, width, penDown);
}
public State withAngle(double newAngle) {
return new State(x, y, newAngle, color, width, penDown);
}
public State withColor(Color newColor) {
return new State(x, y, angle, newColor, width, penDown);
}
public State withWidth(double newWidth) {
return new State(x, y, angle, color, newWidth, penDown);
}
public State withPenDown(boolean isDown) {
return new State(x, y, angle, color, width, isDown);
}
}
private static record Line(int id, double x1, double y1, double x2, double y2, Color color, double width) implements Element {}
private static record SvgText(int id, String text, double x, double y, double dx, double dy, Color color, String font) implements Element {}
private static record Color(int r, int g, int b, double a) {}
public sealed interface Element permits Line, SvgText {}
}