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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 penColor = color; | 95 penColor = color; |
96 drawFrame(rad); | 96 drawFrame(rad); |
97 } | 97 } |
98 | 98 |
99 void onSpeedChange() { | 99 void onSpeedChange() { |
100 speed = speedSlider.valueAsNumber; | 100 speed = speedSlider.valueAsNumber; |
101 stepSize = calcStepSize(); | 101 stepSize = calcStepSize(); |
102 } | 102 } |
103 | 103 |
104 void onPenWidthChange() { | 104 void onPenWidthChange() { |
105 penWidth = penWidthSlider.valueAsNumber.toInt(); | 105 penWidth = penWidthSlider.valueAsNumber.truncate(); |
106 drawFrame(rad); | 106 drawFrame(rad); |
107 } | 107 } |
108 | 108 |
109 void refresh() { | 109 void refresh() { |
110 stop(); | 110 stop(); |
111 // Reset | 111 // Reset |
112 lastX = lastY = 0; | 112 lastX = lastY = 0; |
113 // Compute fixed radius | 113 // Compute fixed radius |
114 // based on starting diameter == min / 2, fixed radius == 10 units | 114 // based on starting diameter == min / 2, fixed radius == 10 units |
115 int min = Math.min(height, width); | 115 int min = Math.min(height, width); |
116 double pixelsPerUnit = min / 40; | 116 double pixelsPerUnit = min / 40; |
117 RUnits = fixedRadiusSlider.valueAsNumber.toInt(); | 117 RUnits = fixedRadiusSlider.valueAsNumber.truncate(); |
118 R = RUnits * pixelsPerUnit; | 118 R = RUnits * pixelsPerUnit; |
119 // Scale inner radius and pen distance in units of fixed radius | 119 // Scale inner radius and pen distance in units of fixed radius |
120 rUnits = wheelRadiusSlider.valueAsNumber.toInt(); | 120 rUnits = wheelRadiusSlider.valueAsNumber.truncate(); |
121 r = rUnits * R/RUnits * int.parse(inOrOut.value); | 121 r = rUnits * R/RUnits * int.parse(inOrOut.value); |
122 dUnits = penRadiusSlider.valueAsNumber.toInt(); | 122 dUnits = penRadiusSlider.valueAsNumber.truncate(); |
123 d = dUnits * R/RUnits; | 123 d = dUnits * R/RUnits; |
124 numPoints = calcNumPoints(); | 124 numPoints = calcNumPoints(); |
125 maxTurns = calcTurns(); | 125 maxTurns = calcTurns(); |
126 onSpeedChange(); | 126 onSpeedChange(); |
127 numTurns.text = "0 / ${maxTurns}"; | 127 numTurns.text = "0 / ${maxTurns}"; |
128 penWidth = penWidthSlider.valueAsNumber.toInt(); | 128 penWidth = penWidthSlider.valueAsNumber.truncate(); |
129 drawFrame(0.0); | 129 drawFrame(0.0); |
130 } | 130 } |
131 | 131 |
132 int calcNumPoints() { | 132 int calcNumPoints() { |
133 // Empirically, treat it like an oval. | 133 // Empirically, treat it like an oval. |
134 if (dUnits == 0 || rUnits == 0) return 2; | 134 if (dUnits == 0 || rUnits == 0) return 2; |
135 | 135 |
136 int gcf_ = gcf(RUnits, rUnits); | 136 int gcf_ = gcf(RUnits, rUnits); |
137 int n = RUnits ~/ gcf_; | 137 int n = RUnits ~/ gcf_; |
138 int d_ = rUnits ~/ gcf_; | 138 int d_ = rUnits ~/ gcf_; |
(...skipping 11 matching lines...) Expand all 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).truncate(); |
Lasse Reichstein Nielsen
2013/01/04 10:29:42
/ -> ~/ ?
floitsch
2013/03/11 13:39:15
done in different CL.
| |
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 |