Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sunflower
<!-- Copyright 2011 the Dart project authors. All rights reserved.
Use of this source code is governed by a BSD-style license
that can be found in the LICENSE file. -->
<h2>Dr. Fibonacci's Sunflower Spectacular</h2>
<div>
<canvas id="canvas" width="300" height="300"></canvas>
</div>
<div>
<label>0</label>
<input id="slider" type="range" max="1000" value="500" />
<label>1000</label>
</div>
// Copyright 2011 the Dart project authors. 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' as math;
void main() {
new Sunflower();
}
class Sunflower {
static const String ORANGE = "orange";
static const SEED_RADIUS = 2;
static const SCALE_FACTOR = 4;
static const TAU = math.pi * 2;
static const MAX_D = 300;
CanvasRenderingContext2D ctx;
num xc, yc;
num seeds = 0;
num PHI;
Sunflower() {
PHI = (math.sqrt(5) + 1) / 2;
CanvasElement canvas = querySelector("#canvas");
xc = yc = MAX_D / 2;
ctx = canvas.getContext("2d");
InputElement slider = querySelector("#slider");
slider.onChange.listen((Event e) {
seeds = int.parse(slider.value);
drawFrame();
});
seeds = int.parse(slider.value);
drawFrame();
}
// Draw the complete figure for the current number of seeds.
void drawFrame() {
print('seed value = ${seeds}');
ctx.clearRect(0, 0, MAX_D, MAX_D);
for (var i = 0; i < seeds; i++) {
var theta = i * TAU / PHI;
var r = math.sqrt(i) * SCALE_FACTOR;
var x = xc + r * math.cos(theta);
var y = yc - r * math.sin(theta);
drawSeed(x, y);
}
}
// Draw a small circle representing a seed centered at (x,y).
void drawSeed(num x, num y) {
ctx.beginPath();
ctx.lineWidth = 2;
ctx.fillStyle = ORANGE;
ctx.strokeStyle = ORANGE;
ctx.arc(x, y, SEED_RADIUS, 0, TAU, false);
ctx.fill();
ctx.closePath();
ctx.stroke();
}
}
/* Copyright 2011 the Dart project authors. All rights reserved. */
/* Use of this source code is governed by a BSD-style license */
/* that can be found in the LICENSE file. */
h2 {
margin-bottom: 0;
text-align: center;
}
div {
text-align: center;
}
@simon511000

This comment has been minimized.

Copy link

@simon511000 simon511000 commented May 11, 2019

Wow !

@DrBeta

This comment has been minimized.

Copy link

@DrBeta DrBeta commented May 15, 2019

WOW

@debroq

This comment has been minimized.

Copy link

@debroq debroq commented Dec 6, 2019

Very cool!

@rihannaKe

This comment has been minimized.

Copy link

@rihannaKe rihannaKe commented Jan 9, 2020

Cool!!

@romancarrasco

This comment has been minimized.

Copy link

@romancarrasco romancarrasco commented Jan 31, 2020

Awesome

@NoobSolver

This comment has been minimized.

Copy link

@NoobSolver NoobSolver commented Mar 6, 2020

very good

@xgqfrms

This comment has been minimized.

Copy link

@xgqfrms xgqfrms commented Jul 2, 2020

https://github.com/dart-lang/dart-pad/wiki/Sharing-Guide

DartPad gist share tutorial

  1. create index.html

image

  1. edit, add files main.dart & styles.css

image

demo

OK

https://dartpad.dev/2ada2477a76d65cbd4b16558131fcbc0

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment