Create the geometry of a sector of an ellipse. - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

Create the geometry of a sector of an ellipse.

Demo Code

/*//from   w w  w.  j a  va  2 s.  co 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 the geometry of a sector of an ellipse.
     * 
     * @param x0      X coordinate of center of ellipse.
     * @param y0      Y coordinate of center of ellipse.
     * @param dx      X radius of ellipse.
     * @param dy      Y radius of ellipse.
     * @param angle0  First angle of sector (in radians).
     * @param angle1  Second angle of sector (in radians).
     * @return        Geometry of secor [x,y,...]
     */
    public static int[] createSector(int x0, int y0, int dx, int dy,
            double angle0, double angle1) {
        // Determine a sensible number of points for arc
        double angleSpan = Math.abs(angle1 - angle0);
        double arcDistance = Math.max(dx, dy) * angleSpan;
        int nPoints = (int) Math.round(arcDistance / 15);
        double angleStep = angleSpan / (nPoints - 1);

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

        int index = 0;
        for (int i = 0; i < nPoints; i++) {
            double angle = angle0 + angleStep * i;
            double x = dx * Math.cos(angle);
            double y = dy * Math.sin(angle);

            xy[index + 0] = x0 + (int) Math.round(x);
            xy[index + 1] = y0 - (int) Math.round(y);

            index += 2;
        }

        // Start and end geometry at center of ellipse to make it a closed polygon
        xy[nPoints * 2 + 0] = x0;
        xy[nPoints * 2 + 1] = y0;
        xy[nPoints * 2 + 2] = xy[0];
        xy[nPoints * 2 + 3] = xy[1];

        return xy;
    }

    /**
     * Create the geometry of a sector of a circle.
     * 
     * @param x0      X coordinate of center of ellipse.
     * @param y0      Y coordinate of center of ellipse.
     * @param dx      X radius of ellipse.
     * @param dy      Y radius of ellipse.
     * @param angle0  First angle of sector (in radians).
     * @param angle1  Second angle of sector (in radians).
     * @return        Geometry of secor [x,y,...]
     */
    public static int[] createSector(int x0, int y0, int radius,
            double angle0, double angle1) {
        return createSector(x0, y0, radius, radius, angle0, angle1);
    }

    /**
     * Return largest of four numbers.
     * 
     * @param a  First number to find largest among.
     * @param b  Second number to find largest among.
     * @param c  Third number to find largest among.
     * @param d  Fourth number to find largest among.   
     * @return   Largest of a, b, c and d.
     */
    private static double max(double a, double b, double c, double d) {
        return Math.max(Math.max(a, b), Math.max(c, d));
    }
}

Related Tutorials