diff --git a/lib/p5.dart b/lib/p5.dart index 527978d..f9b8c2e 100644 --- a/lib/p5.dart +++ b/lib/p5.dart @@ -2,7 +2,7 @@ library p5; import "dart:typed_data"; import "dart:ui"; - +import 'dart:math' show pi, sin, cos; import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; @@ -314,6 +314,35 @@ class PPainter extends ChangeNotifier implements CustomPainter { useFill = false; } + void polygon(double x, double y, double radius, int sides) { + final path = Path(); + + final center = Offset(x, y); + final angle = (2 * pi) / sides; + + final angles = List.generate(sides, (index) => index * angle); + + path.moveTo( + center.dx + radius * cos(0), + center.dy + radius * sin(0), + ); + + for (final angle in angles) { + path.lineTo( + center.dx + radius * cos(angle), + center.dy + radius * sin(angle), + ); + } + + path.close(); + if (useFill) { + paintCanvas.drawPath(path, fillPaint); + } + if (useStroke) { + paintCanvas.drawPath(path, strokePaint); + } + } + void ellipse(double x, double y, double w, double h) { final rect = new Offset(x - w / 2, y - h / 2) & new Size(w, h); if (useFill) {