Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Added a method for creating polygons. #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion lib/p5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) {
Expand Down