Java Geometry Polygon regularPolygon(int sides, double x, double y, double width, double height)

Here you can find the source of regularPolygon(int sides, double x, double y, double width, double height)

Description

regular Polygon

License

Open Source License

Declaration

static private Shape regularPolygon(int sides, double x, double y, double width, double height) 

Method Source Code

//package com.java2s;
/*/*from ww  w.j  a v a2 s  .  c  o  m*/
 * #%L
 * Cytoscape Ding View/Presentation Impl (ding-presentation-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

import java.awt.Shape;

import java.awt.geom.Path2D;

import java.awt.geom.Point2D;

public class Main {
    private static double halfPI = Math.PI / 2.0;

    static private Shape regularPolygon(int sides, double x, double y, double width, double height) {
        Path2D poly = new Path2D.Double(Path2D.WIND_EVEN_ODD, 12);
        width = width / 2;
        height = height / 2;
        x = x + width;
        y = y + height;
        Point2D.Double points[] = new Point2D.Double[sides];
        for (int i = 0; i < sides; i++) {
            double x1 = circleX(sides, i) * width + x;
            double y1 = circleY(sides, i) * height + y;
            double x2 = circleX(sides, (i + 1) % sides) * width + x;
            double y2 = circleY(sides, (i + 1) % sides) * height + y;
            points[i] = new Point2D.Double(x1, y1);
            points[(i + 1) % sides] = new Point2D.Double(x2, y2);
        }
        // Now, add the points
        poly.moveTo(points[0].getX(), points[0].getY());
        for (int i = 1; i < sides; i++) {
            poly.lineTo(points[i].getX(), points[i].getY());
        }
        poly.closePath();
        return poly;
    }

    static double circleX(int sides, int angle) {
        double coeff = (double) angle / (double) sides;
        return epsilon(Math.cos(2 * coeff * Math.PI - halfPI));
    }

    static double circleY(int sides, int angle) {
        double coeff = (double) angle / (double) sides;
        return epsilon(Math.sin(2 * coeff * Math.PI - halfPI));
    }

    static double epsilon(double v) {
        if (Math.abs(v) < 1.0E-10)
            return 0.0;
        return v;
    }
}

Related

  1. boundingTest(Polygon p, int x, int y, int w, int h)
  2. inside_polygon(float[] xpts, float[] ypts, double ptx, double pty)
  3. makeArea(Polygon polygon, int steps, int dX, int dY)
  4. polyArea(Polygon target)
  5. sparsifyPolygon(java.awt.Polygon p, double minDistance)