1
25
26 Function drawLine(Integer32 x1,
28 Integer32 y1,
29 Integer32 x2,
30 Integer32 y2,
31 PointerToCharacter color,
32 Integer32 lineWidth) Which Returns Nothing Is External;
33
34 Function applyTurtleTransformation(PointerToCharacter svgDirective)
35 Which Returns Nothing Is External;
39 Integer32 directionX[4] : = {0, 1, 0, -1},
40 directionY[4] : = {-1, 0, 1, 0},
41 currentX : = 10,
42 currentY : = 250 + 490 - 410, currentDirection : = 0,
47 lineLength : = 5,
48 lineWidth : = 2,
49 currentStep : = 0;
50
51 Character path[16384], reversedPath[16384];
52
53 Function strlen(PointerToCharacter str) Which Returns Integer32 Is Declared;
57 Function strcpy(PointerToCharacter dest,
58 PointerToCharacter src) Which Returns Nothing Is Declared;
59 Function reverseString(PointerToCharacter string)
60 Which Returns Nothing Is Declared;
61 Function strcat(PointerToCharacter dest,
62 PointerToCharacter src) Which Returns Nothing Is Declared;
63 Function convertIntegerToString(PointerToCharacter string, Integer32 number)
64 Which Returns Integer32 Is Declared;
65
66 Function init() Which Returns Nothing Does
69 PointerToCharacter path : = AddressOf(path[0]);
70 PointerToCharacter reversedPath : = AddressOf(reversedPath[0]);
71 strcpy(path, "R");
72 Integer32 counter : = 0;
73 While strlen(path) < 8192 Loop
74 strcpy(reversedPath, path);
75 If mod(counter, 4) = 0 Then
76 reverseString(reversedPath);
77 EndIf
78 strcat(path, reversedPath);
79 strcat(path, not(mod(counter, 4)) ? "L" : "LLL");
80 counter : = counter + 1;
81 EndWhile
82 EndFunction
83
84 Function step() Which Returns Nothing Does
86 If not(path[currentStep] = 0)
87 and 0 < currentX < 500
88 and 0 < currentY < 500 Then
89 Integer32 nextX : = currentX + directionX[currentDirection] * lineLength,
90 nextY : = currentY + directionY[currentDirection] * lineLength;
91 drawLine(
92 currentX,
93 currentY,
94 nextX,
95 nextY,
96 currentStep = 0 ? "lightYellow"
97 : path[currentStep] = 'R' ? "red"
98 : path[currentStep] =
99 'L' ? "lightBlue" : "lightYellow",
100 lineWidth
101 );
102 currentX : = nextX;
103 currentY : = nextY;
104 If path[currentStep] = 'R' Then
105 currentDirection : = mod(currentDirection + 1, 4);
106 ElseIf not(currentDirection = 0) and path[currentStep] = 'L' Then
107 currentDirection : = currentDirection - 1;
108 ElseIf path[currentStep] = 'L' Then
109 currentDirection : = 3;
110 EndIf
111 Integer32 tmp; currentStep : = currentStep + 1;
117 Character turtleTranformation[64] : = {0};
118 PointerToCharacter turtleTranformation
119 : = AddressOf(turtleTranformation[0]);
120 strcat(turtleTranformation, "translate(");
121 tmp : = convertIntegerToString(
122 turtleTranformation + strlen(turtleTranformation),
123 currentX
124 );
125 strcat(turtleTranformation, " ");
126 tmp : = convertIntegerToString(
127 turtleTranformation + strlen(turtleTranformation),
128 currentY
129 );
130 strcat(turtleTranformation, ") rotate(");
131 tmp : = convertIntegerToString(
132 turtleTranformation + strlen(turtleTranformation),
133 currentDirection * 90
134 );
135 strcat(turtleTranformation, ")");
136 applyTurtleTransformation(turtleTranformation);
137 EndIf
138 EndFunction
139
140 Function strlen(PointerToCharacter str) Which Returns Integer32 Does
141 Integer32 length : = 0;
145 While ValueAt(str + length) Loop
146 length : = length + 1;
147 EndWhile
148 Return length;
149 EndFunction
150
151 Function strcpy(PointerToCharacter dest,
152 PointerToCharacter src) Which Returns Nothing Does
153 While ValueAt(src) Loop
154 ValueAt(dest) : = ValueAt(src);
155 dest : = dest + 1;
156 src : = src + 1;
157 EndWhile
158 ValueAt(dest) : = 0;
159 EndFunction
160
161 Function reverseString(PointerToCharacter string) Which Returns Nothing Does
162 PointerToCharacter pointerToLastCharacter : = string + strlen(string) - 1;
163 While pointerToLastCharacter - string > 0 Loop
164 Character tmp : = ValueAt(string);
165 ValueAt(string) : = ValueAt(pointerToLastCharacter);
166 ValueAt(pointerToLastCharacter) : = tmp;
167 string : = string + 1;
168 pointerToLastCharacter : = pointerToLastCharacter - 1;
169 EndWhile
170 EndFunction
171
172 Function strcat(PointerToCharacter dest,
173 PointerToCharacter src) Which Returns Nothing Does
174 strcpy(dest + strlen(dest), src);
175 EndFunction
176
177 Function convertIntegerToString(PointerToCharacter string, Integer32 number)
178 Which Returns Integer32 Does Integer32 isNumberNegative : = 0;
180 If number < 0 Then
181 number : = -number;
182 isNumberNegative : = 1;
183 EndIf
184 Integer32 i : = 0;
185 While number > 9 Loop
186 ValueAt(string + i) : = '0' + mod(number, 10);
187 number : = number / 10;
188 i : = i + 1;
189 EndWhile
190 ValueAt(string + i) : = '0' + number;
191 i : = i + 1;
192 If isNumberNegative Then
193 ValueAt(string + i) : = '-';
194 i : = i + 1;
195 EndIf
196 ValueAt(string + i) : = 0;
197 reverseString(string);
198 Return i;
199 EndFunction