| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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:dom'); | 7 #import('dart:dom'); |
| 8 #source("ColorPicker.dart"); | 8 #source("ColorPicker.dart"); |
| 9 #resource("spirodraw.css"); | 9 #resource("spirodraw.css"); |
| 10 | 10 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 penRadiusSlider.valueAsNumber = Math.random() * 9; | 211 penRadiusSlider.valueAsNumber = Math.random() * 9; |
| 212 penWidthSlider.valueAsNumber = 1 + Math.random() * 9; | 212 penWidthSlider.valueAsNumber = 1 + Math.random() * 9; |
| 213 colorPicker.selectedColor = Math.random() * 215; | 213 colorPicker.selectedColor = Math.random() * 215; |
| 214 start(); | 214 start(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 void drawFixed() { | 217 void drawFixed() { |
| 218 if (animationEnabled) { | 218 if (animationEnabled) { |
| 219 front.beginPath(); | 219 front.beginPath(); |
| 220 front.setLineWidth(2); | 220 front.setLineWidth(2); |
| 221 front.setStrokeStyle("gray"); | 221 front.strokeStyle = "gray"; |
| 222 front.arc(xc, yc, R, 0, PI2, true); | 222 front.arc(xc, yc, R, 0, PI2, true); |
| 223 front.closePath(); | 223 front.closePath(); |
| 224 front.stroke(); | 224 front.stroke(); |
| 225 } | 225 } |
| 226 } | 226 } |
| 227 | 227 |
| 228 /** | 228 /** |
| 229 * Draw the wheel with its center at angle theta | 229 * Draw the wheel with its center at angle theta |
| 230 * with respect to the fixed wheel | 230 * with respect to the fixed wheel |
| 231 * | 231 * |
| 232 * @param theta | 232 * @param theta |
| 233 */ | 233 */ |
| 234 void drawWheel(double theta) { | 234 void drawWheel(double theta) { |
| 235 double wx = xc + ((R + r) * Math.cos(theta)); | 235 double wx = xc + ((R + r) * Math.cos(theta)); |
| 236 double wy = yc - ((R + r) * Math.sin(theta)); | 236 double wy = yc - ((R + r) * Math.sin(theta)); |
| 237 if (animationEnabled) { | 237 if (animationEnabled) { |
| 238 if (rUnits>0) { | 238 if (rUnits>0) { |
| 239 // Draw ring | 239 // Draw ring |
| 240 front.beginPath(); | 240 front.beginPath(); |
| 241 front.arc(wx, wy, r.abs(), 0, PI2, true); | 241 front.arc(wx, wy, r.abs(), 0, PI2, true); |
| 242 front.closePath(); | 242 front.closePath(); |
| 243 front.stroke(); | 243 front.stroke(); |
| 244 // Draw center | 244 // Draw center |
| 245 front.setLineWidth(1); | 245 front.setLineWidth(1); |
| 246 front.beginPath(); | 246 front.beginPath(); |
| 247 front.arc(wx, wy, 3, 0, PI2, true); | 247 front.arc(wx, wy, 3, 0, PI2, true); |
| 248 front.setFillStyle("black"); | 248 front.fillStyle = "black"; |
| 249 front.fill(); | 249 front.fill(); |
| 250 front.closePath(); | 250 front.closePath(); |
| 251 front.stroke(); | 251 front.stroke(); |
| 252 } | 252 } |
| 253 } | 253 } |
| 254 drawTip(wx, wy, theta); | 254 drawTip(wx, wy, theta); |
| 255 } | 255 } |
| 256 | 256 |
| 257 /** | 257 /** |
| 258 * Draw a rotating line that shows the wheel rolling and leaves | 258 * Draw a rotating line that shows the wheel rolling and leaves |
| 259 * the pen trace | 259 * the pen trace |
| 260 * | 260 * |
| 261 * @param wx X coordinate of wheel center | 261 * @param wx X coordinate of wheel center |
| 262 * @param wy Y coordinate of wheel center | 262 * @param wy Y coordinate of wheel center |
| 263 * @param theta Angle of wheel center with respect to fixed circle | 263 * @param theta Angle of wheel center with respect to fixed circle |
| 264 */ | 264 */ |
| 265 void drawTip(double wx, double wy, double theta) { | 265 void drawTip(double wx, double wy, double theta) { |
| 266 // Calc wheel rotation angle | 266 // Calc wheel rotation angle |
| 267 double rot = (r==0) ? theta : theta * (R+r) / r; | 267 double rot = (r==0) ? theta : theta * (R+r) / r; |
| 268 // Find tip of line | 268 // Find tip of line |
| 269 double tx = wx + d * Math.cos(rot); | 269 double tx = wx + d * Math.cos(rot); |
| 270 double ty = wy - d * Math.sin(rot); | 270 double ty = wy - d * Math.sin(rot); |
| 271 if (animationEnabled) { | 271 if (animationEnabled) { |
| 272 front.beginPath(); | 272 front.beginPath(); |
| 273 front.setFillStyle(penColor); | 273 front.fillStyle = penColor; |
| 274 front.arc(tx, ty, penWidth/2+2, 0, PI2, true); | 274 front.arc(tx, ty, penWidth/2+2, 0, PI2, true); |
| 275 front.fill(); | 275 front.fill(); |
| 276 front.moveTo(wx, wy); | 276 front.moveTo(wx, wy); |
| 277 front.setStrokeStyle("black"); | 277 front.strokeStyle = "black"; |
| 278 front.lineTo(tx, ty); | 278 front.lineTo(tx, ty); |
| 279 front.closePath(); | 279 front.closePath(); |
| 280 front.stroke(); | 280 front.stroke(); |
| 281 } | 281 } |
| 282 drawSegmentTo(tx, ty); | 282 drawSegmentTo(tx, ty); |
| 283 } | 283 } |
| 284 | 284 |
| 285 void drawSegmentTo(double tx, double ty) { | 285 void drawSegmentTo(double tx, double ty) { |
| 286 if (lastX > 0) { | 286 if (lastX > 0) { |
| 287 back.beginPath(); | 287 back.beginPath(); |
| 288 back.setStrokeStyle(penColor); | 288 back.strokeStyle = penColor; |
| 289 back.setLineWidth(penWidth); | 289 back.setLineWidth(penWidth); |
| 290 back.moveTo(lastX, lastY); | 290 back.moveTo(lastX, lastY); |
| 291 back.lineTo(tx, ty); | 291 back.lineTo(tx, ty); |
| 292 back.closePath(); | 292 back.closePath(); |
| 293 back.stroke(); | 293 back.stroke(); |
| 294 } | 294 } |
| 295 lastX = tx; | 295 lastX = tx; |
| 296 lastY = ty; | 296 lastY = ty; |
| 297 } | 297 } |
| 298 | 298 |
| 299 } | 299 } |
| 300 | 300 |
| 301 int gcf(int n, int d) { | 301 int gcf(int n, int d) { |
| 302 if (n==d) | 302 if (n==d) |
| 303 return n; | 303 return n; |
| 304 int max = Math.max(n, d); | 304 int max = Math.max(n, d); |
| 305 for (int i = max ~/ 2; i > 1; i--) | 305 for (int i = max ~/ 2; i > 1; i--) |
| 306 if ((n % i == 0) && (d % i == 0)) | 306 if ((n % i == 0) && (d % i == 0)) |
| 307 return i; | 307 return i; |
| 308 return 1; | 308 return 1; |
| 309 } | 309 } |
| OLD | NEW |