OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library spirodraw; | 5 library spirodraw; |
6 | 6 |
7 import 'dart:html'; | 7 import 'dart:html'; |
8 import 'dart:math' as Math; | 8 import 'dart:math' as Math; |
9 | 9 |
10 part "ColorPicker.dart"; | 10 part "ColorPicker.dart"; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 ..drawImage(backCanvas, 0, 0); | 150 ..drawImage(backCanvas, 0, 0); |
151 drawFixed(); | 151 drawFixed(); |
152 } | 152 } |
153 drawWheel(theta); | 153 drawWheel(theta); |
154 } | 154 } |
155 | 155 |
156 void animate(double time) { | 156 void animate(double time) { |
157 if (run && rad <= maxTurns * PI2) { | 157 if (run && rad <= maxTurns * PI2) { |
158 rad+=stepSize; | 158 rad+=stepSize; |
159 drawFrame(rad); | 159 drawFrame(rad); |
160 int nTurns = (rad / PI2).toInt(); | 160 int nTurns = rad ~/ PI2; |
161 numTurns.text = '${nTurns}/$maxTurns'; | 161 numTurns.text = '${nTurns}/$maxTurns'; |
162 window.requestAnimationFrame(animate); | 162 window.requestAnimationFrame(animate); |
163 } else { | 163 } else { |
164 stop(); | 164 stop(); |
165 } | 165 } |
166 } | 166 } |
167 | 167 |
168 void start() { | 168 void start() { |
169 refresh(); | 169 refresh(); |
170 rad = 0.0; | 170 rad = 0.0; |
171 run = true; | 171 run = true; |
172 window.requestAnimationFrame(animate); | 172 window.requestAnimationFrame(animate); |
173 } | 173 } |
174 | 174 |
175 int calcTurns() { | 175 int calcTurns() { |
176 // compute ratio of wheel radius to big R then find LCM | 176 // compute ratio of wheel radius to big R then find LCM |
177 if ((dUnits==0) || (rUnits==0)) return 1; | 177 if ((dUnits==0) || (rUnits==0)) return 1; |
178 int ru = rUnits.abs(); | 178 int ru = rUnits.abs(); |
179 int wrUnits = RUnits + rUnits; | 179 int wrUnits = RUnits + rUnits; |
180 int g = gcf (wrUnits, ru); | 180 int g = gcf (wrUnits, ru); |
181 return (ru ~/ g).toInt(); | 181 return ru ~/ g; |
182 } | 182 } |
183 | 183 |
184 void stop() { | 184 void stop() { |
185 run = false; | 185 run = false; |
186 // Show drawing only | 186 // Show drawing only |
187 front..clearRect(0, 0, width, height) | 187 front..clearRect(0, 0, width, height) |
188 ..drawImage(backCanvas, 0, 0); | 188 ..drawImage(backCanvas, 0, 0); |
189 // Reset angle | 189 // Reset angle |
190 rad = 0.0; | 190 rad = 0.0; |
191 } | 191 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 295 } |
296 | 296 |
297 int gcf(int n, int d) { | 297 int gcf(int n, int d) { |
298 if (n == d) return n; | 298 if (n == d) return n; |
299 int max = Math.max(n, d); | 299 int max = Math.max(n, d); |
300 for (int i = max ~/ 2; i > 1; i--) { | 300 for (int i = max ~/ 2; i > 1; i--) { |
301 if ((n % i == 0) && (d % i == 0)) return i; | 301 if ((n % i == 0) && (d % i == 0)) return i; |
302 } | 302 } |
303 return 1; | 303 return 1; |
304 } | 304 } |
OLD | NEW |