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