Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: test/codegen/sunflower/sunflower.dart

Issue 1147053003: Modified sunflower (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Rebased Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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:math'; 7 import 'dart:math';
8
9 import 'circle.dart';
8 import 'dom.dart'; 10 import 'dom.dart';
11 import 'painter.dart';
9 12
10 const ORANGE = "orange";
11 const SEED_RADIUS = 2; 13 const SEED_RADIUS = 2;
12 const SCALE_FACTOR = 4; 14 const SCALE_FACTOR = 4;
13 const TAU = PI * 2;
14 const MAX_D = 300; 15 const MAX_D = 300;
15 const centerX = MAX_D / 2; 16 const centerX = MAX_D / 2;
16 const centerY = centerX; 17 const centerY = centerX;
17 18
18 Element querySelector(String selector) => document.querySelector(selector); 19 final slider = querySelector("#slider") as InputElement;
19
20 final InputElement slider = querySelector("#slider");
21 final notes = querySelector("#notes"); 20 final notes = querySelector("#notes");
22 final PHI = (sqrt(5) + 1) / 2; 21 final PHI = (sqrt(5) + 1) / 2;
23 int seeds = 0; 22 int seeds = 0;
24 final CanvasRenderingContext2D context =
25 (querySelector("#canvas") as CanvasElement).getContext('2d');
26 23
27 void main() { 24 void main() {
28 slider.addEventListener('change', (e) => draw()); 25 slider.addEventListener('change', (e) => draw());
29 draw(); 26 draw();
30 } 27 }
31 28
32 /// Draw the complete figure for the current number of seeds. 29 /// Draw the complete figure for the current number of seeds.
33 void draw() { 30 void draw() {
34 seeds = int.parse(slider.value); 31 seeds = int.parse(slider.value);
35 context.clearRect(0, 0, MAX_D, MAX_D); 32 context.clearRect(0, 0, MAX_D, MAX_D);
36 for (var i = 0; i < seeds; i++) { 33 for (var i = 0; i < seeds; i++) {
37 final theta = i * TAU / PHI; 34 final theta = i * TAU / PHI;
38 final r = sqrt(i) * SCALE_FACTOR; 35 final r = sqrt(i) * SCALE_FACTOR;
39 final x = centerX + r * cos(theta); 36 final x = centerX + r * cos(theta);
40 final y = centerY - r * sin(theta); 37 final y = centerY - r * sin(theta);
41 new SunflowerSeed(x, y, SEED_RADIUS).draw(); 38 new SunflowerSeed(x, y, SEED_RADIUS).draw();
42 } 39 }
43 notes.textContent = "$seeds seeds"; 40 notes.textContent = "$seeds seeds";
44 } 41 }
45 42
46 // This example was modified to use classes and mixins. 43 // This example was modified to use classes and mixins.
47 class SunflowerSeed extends Circle with CirclePainter { 44 class SunflowerSeed extends Circle with CirclePainter {
48 SunflowerSeed(num x, num y, num radius, [String color]) 45 SunflowerSeed(num x, num y, num radius, [String color])
49 : super(x, y, radius) { 46 : super(x, y, radius) {
50 if (color != null) this.color = color; 47 if (color != null) this.color = color;
51 } 48 }
52 } 49 }
53 50
54 abstract class CirclePainter implements Circle {
55 // This demonstrates a field in a mixin.
56 String color = ORANGE;
57 51
58 /// Draw a small circle representing a seed centered at (x,y).
59 void draw() {
60 context
61 ..beginPath()
62 ..lineWidth = 2
63 ..fillStyle = color
64 ..strokeStyle = color
65 ..arc(x, y, radius, 0, TAU, false)
66 ..fill()
67 ..closePath()
68 ..stroke();
69 }
70 }
71
72 class Circle {
73 final num x, y, radius;
74
75 Circle(this.x, this.y, this.radius);
76 }
OLDNEW
« test/codegen/sunflower/painter.dart ('K') | « test/codegen/sunflower/painter.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698