Uniform edge length for NGon
in
Programming Questions
•
1 year ago
I have a sketch that can increase / decrease the number of vertices for a polygon. Right now I have a variable, "unit", which is set to 50. All of the polygons' vertices are drawn from the center of the polygon with a distance of 50 from the center. This is not what I actually want, I want all of the edges of the polygons to have the length of 50 (polygons with more vertices will be bigger than polygons with less vertices). How can I do this?
- float unit = 50;
- int selected = 0;
- ArrayList<Polygon> polys = new ArrayList<Polygon>();
- void setup() {
- size(800, 400);
- smooth();
- polys.add(new Polygon(-HALF_PI, width/5, height/2, 3));
- polys.add(new Polygon(-HALF_PI, width/2, height/2, 4));
- polys.add(new Polygon(-HALF_PI, width-width/5, height/2, 5));
- }
- void draw() {
- background(255);
- for (int i = 0; i < polys.size(); i++) {
- if (selected == i) fill(255, 0, 0);
- else fill(255);
- beginShape();
- for (int j = 0; j < polys.get(i).verts; j++) {
- vertex(polys.get(i).x+cos(polys.get(i).rot+TWO_PI/polys.get(i).verts*j)*unit,
- polys.get(i).y+sin(polys.get(i).rot+TWO_PI/polys.get(i).verts*j)*unit);
- }
- endShape(CLOSE);
- }
- }
- class Polygon {
- float rot, x, y;
- int verts;
- Polygon(float inRo, float inX, float inY, int inVe) {
- rot = inRo;
- x = inX;
- y = inY;
- verts = inVe;
- }
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == LEFT && selected > 0) selected--;
- if (keyCode == RIGHT && selected < polys.size()-1) selected++;
- if (keyCode == UP) polys.get(selected).verts++;
- if (keyCode == DOWN && polys.get(selected).verts > 3) polys.get(selected).verts--;
- }
- }
1