Create geometry of a star. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Create geometry of a star.

Demo Code

/*//  www  .  j a va2  s .  c  o m
 * (C) 2004 - Geotechnical Software Services
 * 
 * This code 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 code 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 * MA  02111-1307, USA.
 */
//package com.java2s;

public class Main {
    /**
     * Create geometry of a star. Integer domain.
     * 
     * @param x0           X center of star.
     * @param y0           Y center of star.
     * @param innerRadius  Inner radis of arms.
     * @param outerRadius  Outer radius of arms.
     * @param nArms        Number of arms.
     * @return             Geometry of star [x,y,x,y,...].
     */
    public static int[] createStar(int x0, int y0, int innerRadius,
            int outerRadius, int nArms) {
        int nPoints = nArms * 2 + 1;

        int[] xy = new int[nPoints * 2];

        double angleStep = 2.0 * Math.PI / nArms / 2.0;

        for (int i = 0; i < nArms * 2; i++) {
            double angle = i * angleStep;
            double radius = (i % 2) == 0 ? innerRadius : outerRadius;

            double x = x0 + radius * Math.cos(angle);
            double y = y0 + radius * Math.sin(angle);

            xy[i * 2 + 0] = (int) Math.round(x);
            xy[i * 2 + 1] = (int) Math.round(y);
        }

        // Close polygon
        xy[nPoints * 2 - 2] = xy[0];
        xy[nPoints * 2 - 1] = xy[1];

        return xy;
    }

    /**
     * Create geometry of a star. Floating point domain.
     * 
     * @param x0           X center of star.
     * @param y0           Y center of star.
     * @param innerRadius  Inner radis of arms.
     * @param outerRadius  Outer radius of arms.
     * @param nArms        Number of arms.
     * @return             Geometry of star [x,y,x,y,...].
     */
    public static double[] createStar(double x0, double y0,
            double innerRadius, double outerRadius, int nArms) {
        int nPoints = nArms * 2 + 1;

        double[] xy = new double[nPoints * 2];

        double angleStep = 2.0 * Math.PI / nArms / 2.0;

        for (int i = 0; i < nArms * 2; i++) {
            double angle = i * angleStep;
            double radius = (i % 2) == 0 ? innerRadius : outerRadius;

            xy[i * 2 + 0] = x0 + radius * Math.cos(angle);
            xy[i * 2 + 1] = y0 + radius * Math.sin(angle);
        }

        // Close polygon
        xy[nPoints * 2 - 2] = xy[0];
        xy[nPoints * 2 - 1] = xy[1];

        return xy;
    }
}

Related Tutorials