Skip to content

Instantly share code, notes, and snippets.

@fdoyle
Created April 2, 2019 19:54
Show Gist options
  • Save fdoyle/d1267c6db7cfecf42f51abcd288f87cf to your computer and use it in GitHub Desktop.
Save fdoyle/d1267c6db7cfecf42f51abcd288f87cf to your computer and use it in GitHub Desktop.
some stuff for working with simplex noise in dart
class VisualizerPainter extends CustomPainter {
final double position;
var noise = new SimplexNoise();
NoiseLoop rLoop;
NoiseLoop gLoop;
NoiseLoop bLoop;
VisualizerPainter(this.position, this.rLoop, this.gLoop, this.bLoop);
@override
void paint(Canvas canvas, Size size) {
print(rLoop.value(position));
Offset center = Offset(size.width / 2, size.height / 2);
var radius = min(size.width, size.height);
Path path = Path();
path.moveTo(center.dx + radius * rLoop.value(position), center.dy);
for (int i = 0; i != 100; i++) {
var angle = map(0.0 + i, 0, 100, 0, 2 * pi);
path.lineTo(center.dx + cos(angle) * radius * rLoop.value(position),
center.dy + sin(angle) * radius * rLoop.value(position));
}
canvas.drawPath(path, Paint()..color = Colors.black);
// canvas.drawRect(
// Rect.fromLTWH(0, 0, size.width, size.height), Paint()..color = color);
}
@override
bool shouldRepaint(VisualizerPainter oldDelegate) =>
position != oldDelegate.position;
}
double map(
double value, double istart, double istop, double ostart, double ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}
class NoiseLoop {
var noise = new SimplexNoise();
final double radius;
var x;
var y;
NoiseLoop(this.radius) {
x = new Random().nextInt(100) + radius;
y = new Random().nextInt(100) + radius;
}
double value(double a) => map(
noise.getSimplex2(cos(map(a, 0, 1, 0, 2 * pi)) * radius + x,
sin(map(a, 0, 1, 0, 2 * pi)) * radius + y),
-0.8,
0.8,
0,
1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment