/
sunflower.dart
74 lines (57 loc) · 1.61 KB
/
sunflower.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library sunflower;
import 'dart:html';
import 'dart:math';
const SEED_RADIUS = 2;
const SCALE_FACTOR = 4;
const TAU = PI * 2;
const MAX_D = 300;
const ORANGE = "orange";
num centerX, centerY;
int seeds = 0;
var PHI;
main() {
PHI = (sqrt(5) + 1) / 2;
CanvasElement canvas = query("#canvas");
centerX = centerY = MAX_D / 2;
var context = canvas.context2d;
InputElement slider = query("#slider");
slider.on.change.add((Event e) {
seeds = int.parse(slider.value);
drawFrame(context);
}, true);
seeds = int.parse(slider.value);
drawFrame(context);
}
/**
* Draw the complete figure for the current number of seeds.
*/
void drawFrame(CanvasRenderingContext2D context) {
context.clearRect(0, 0, MAX_D, MAX_D);
for (var i = 0; i < seeds; i++) {
var theta = i * TAU / PHI;
var r = sqrt(i) * SCALE_FACTOR;
var x = centerX + r * cos(theta);
var y = centerY - r * sin(theta);
drawSeed(context, x, y);
}
displaySeedCount(seeds);
}
/**
* Draw a small circle representing a seed centered at (x,y).
*/
void drawSeed(CanvasRenderingContext2D context, num x, num y) {
context.beginPath();
context.lineWidth = 2;
context.fillStyle = ORANGE;
context.strokeStyle = ORANGE;
context.arc(x, y, SEED_RADIUS, 0, TAU, false);
context.fill();
context.closePath();
context.stroke();
}
void displaySeedCount(num seedCount) {
query("#notes").text = "${seedCount} seeds";
}