| 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('sunflower'); | 5 #library('sunflower'); |
| 6 | 6 |
| 7 #import('dart:dom'); | 7 #import('dart:dom'); |
| 8 | 8 |
| 9 #resource('sunflower.css'); | 9 #resource('sunflower.css'); |
| 10 | 10 |
| 11 main() { | 11 main() { |
| 12 new Sunflower(); | 12 new Sunflower(); |
| 13 } | 13 } |
| 14 | 14 |
| 15 class Sunflower { | 15 class Sunflower { |
| 16 | 16 |
| 17 Sunflower() { | 17 Sunflower() { |
| 18 PHI = (Math.sqrt(5) + 1) / 2; |
| 18 var doc = window.document; | 19 var doc = window.document; |
| 19 | 20 |
| 20 HTMLCanvasElement canvas = doc.getElementById("canvas"); | 21 HTMLCanvasElement canvas = doc.getElementById("canvas"); |
| 21 xc = yc = MAX_D / 2; | 22 xc = yc = MAX_D / 2; |
| 22 ctx = canvas.getContext("2d"); | 23 ctx = canvas.getContext("2d"); |
| 23 | 24 |
| 24 HTMLInputElement slider = doc.getElementById("slider"); | 25 HTMLInputElement slider = doc.getElementById("slider"); |
| 25 slider.onchange = (Event e) { | 26 slider.onchange = (Event e) { |
| 26 seeds = Math.parseInt(slider.value); | 27 seeds = Math.parseInt(slider.value); |
| 27 drawFrame(); | 28 drawFrame(); |
| 28 }; | 29 }; |
| 29 | 30 |
| 30 drawFrame(); | 31 drawFrame(); |
| 31 } | 32 } |
| 32 | 33 |
| 33 // Draw the complete figure for the current number of seeds. | 34 // Draw the complete figure for the current number of seeds. |
| 34 void drawFrame() { | 35 void drawFrame() { |
| 35 ctx.clearRect(0, 0, MAX_D, MAX_D); | 36 ctx.clearRect(0, 0, MAX_D, MAX_D); |
| 36 for (var i=0; i<seeds; i++) { | 37 for (var i=0; i<seeds; i++) { |
| 37 var theta = i * PI2 / PHI; | 38 var theta = i * TAU / PHI; |
| 38 var r = Math.sqrt(i) * SCALE_FACTOR; | 39 var r = Math.sqrt(i) * SCALE_FACTOR; |
| 39 var x = xc + r * Math.cos(theta); | 40 var x = xc + r * Math.cos(theta); |
| 40 var y = yc - r * Math.sin(theta); | 41 var y = yc - r * Math.sin(theta); |
| 41 drawSeed(x,y); | 42 drawSeed(x,y); |
| 42 } | 43 } |
| 43 } | 44 } |
| 44 | 45 |
| 45 // Draw a small circle representing a seed centered at (x,y). | 46 // Draw a small circle representing a seed centered at (x,y). |
| 46 void drawSeed(num x, num y) { | 47 void drawSeed(num x, num y) { |
| 47 ctx.beginPath(); | 48 ctx.beginPath(); |
| 48 ctx.setLineWidth(2); | 49 ctx.setLineWidth(2); |
| 49 ctx.setFillColor(ORANGE); | 50 ctx.setFillColor(ORANGE); |
| 50 ctx.setStrokeColor(ORANGE); | 51 ctx.setStrokeColor(ORANGE); |
| 51 ctx.arc(x, y, SEED_RADIUS, 0, PI2, false); | 52 ctx.arc(x, y, SEED_RADIUS, 0, TAU, false); |
| 52 ctx.fill(); | 53 ctx.fill(); |
| 53 ctx.closePath(); | 54 ctx.closePath(); |
| 54 ctx.stroke(); | 55 ctx.stroke(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 CanvasRenderingContext2D ctx; | 58 CanvasRenderingContext2D ctx; |
| 58 num xc, yc; | 59 num xc, yc; |
| 59 num seeds = 0; | 60 num seeds = 0; |
| 60 | 61 |
| 61 static final SEED_RADIUS = 2; | 62 static final SEED_RADIUS = 2; |
| 62 static final SCALE_FACTOR = 4; | 63 static final SCALE_FACTOR = 4; |
| 63 static final PI2 = Math.PI * 2; | 64 static final TAU = Math.PI * 2; |
| 64 static final PHI = (Math.sqrt(5)+1) / 2; | 65 var PHI; |
| 65 static final MAX_D = 300; | 66 static final MAX_D = 300; |
| 66 static final String ORANGE = "orange"; | 67 static final String ORANGE = "orange"; |
| 67 | 68 |
| 68 } | 69 } |
| OLD | NEW |