From a423f29e4c7fece1eff78a6d24f1a0a96acea963 Mon Sep 17 00:00:00 2001 From: NIBU Date: Thu, 30 Mar 2023 10:42:17 +0530 Subject: [PATCH] added a method for creating polygon --- lib/p5.dart | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) 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) {