| 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 sunflower; | 5 library sunflower; |
| 6 | 6 |
| 7 import 'dart:html'; | 7 import 'dart:html'; |
| 8 import 'dart:math'; | 8 import 'dart:math'; |
| 9 | 9 |
| 10 const String ORANGE = "orange"; | 10 const String ORANGE = "orange"; |
| 11 const int SEED_RADIUS = 2; | 11 const int SEED_RADIUS = 2; |
| 12 const int SCALE_FACTOR = 4; | 12 const int SCALE_FACTOR = 4; |
| 13 const num TAU = PI * 2; | 13 const num TAU = PI * 2; |
| 14 const int MAX_D = 300; | 14 const int MAX_D = 300; |
| 15 const num centerX = MAX_D / 2; | 15 const num centerX = MAX_D / 2; |
| 16 const num centerY = centerX; | 16 const num centerY = centerX; |
| 17 | 17 |
| 18 final InputElement slider = query("#slider"); | 18 final InputElement slider = query("#slider"); |
| 19 final Element notes = query("#notes"); | 19 final Element notes = query("#notes"); |
| 20 final num PHI = (sqrt(5) + 1) / 2; | 20 final num PHI = (sqrt(5) + 1) / 2; |
| 21 int seeds = 0; | 21 int seeds = 0; |
| 22 final CanvasRenderingContext2D context = | 22 final CanvasRenderingContext2D context = |
| 23 (query("#canvas") as CanvasElement).context2d; | 23 (query("#canvas") as CanvasElement).context2D; |
| 24 | 24 |
| 25 void main() { | 25 void main() { |
| 26 slider.onChange.listen((e) => draw()); | 26 slider.onChange.listen((e) => draw()); |
| 27 draw(); | 27 draw(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 /// Draw the complete figure for the current number of seeds. | 30 /// Draw the complete figure for the current number of seeds. |
| 31 void draw() { | 31 void draw() { |
| 32 seeds = int.parse(slider.value); | 32 seeds = int.parse(slider.value); |
| 33 context.clearRect(0, 0, MAX_D, MAX_D); | 33 context.clearRect(0, 0, MAX_D, MAX_D); |
| 34 for (var i = 0; i < seeds; i++) { | 34 for (var i = 0; i < seeds; i++) { |
| 35 final num theta = i * TAU / PHI; | 35 final num theta = i * TAU / PHI; |
| 36 final num r = sqrt(i) * SCALE_FACTOR; | 36 final num r = sqrt(i) * SCALE_FACTOR; |
| 37 drawSeed(centerX + r * cos(theta), centerY - r * sin(theta)); | 37 drawSeed(centerX + r * cos(theta), centerY - r * sin(theta)); |
| 38 } | 38 } |
| 39 notes.text = "${seeds} seeds"; | 39 notes.text = "${seeds} seeds"; |
| 40 } | 40 } |
| 41 | 41 |
| 42 /// Draw a small circle representing a seed centered at (x,y). | 42 /// Draw a small circle representing a seed centered at (x,y). |
| 43 void drawSeed(num x, num y) { | 43 void drawSeed(num x, num y) { |
| 44 context..beginPath() | 44 context..beginPath() |
| 45 ..lineWidth = 2 | 45 ..lineWidth = 2 |
| 46 ..fillStyle = ORANGE | 46 ..fillStyle = ORANGE |
| 47 ..strokeStyle = ORANGE | 47 ..strokeStyle = ORANGE |
| 48 ..arc(x, y, SEED_RADIUS, 0, TAU, false) | 48 ..arc(x, y, SEED_RADIUS, 0, TAU, false) |
| 49 ..fill() | 49 ..fill() |
| 50 ..closePath() | 50 ..closePath() |
| 51 ..stroke(); | 51 ..stroke(); |
| 52 } | 52 } |
| OLD | NEW |