Rotate the polygon with the given angle - Android Graphics

Android examples for Graphics:Path

Description

Rotate the polygon with the given angle

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot.// ww w.  j  a v a  2s .  c  om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
import android.graphics.PointF;

public class Main{
    public static void rotate(float[] x, float[] y, float ox, float oy,
            float cos, float sin) throws IllegalArgumentException {
        if (isValidPolygon(x, y))
            throw new IllegalArgumentException(
                    "points length do not match or one of the points is null");

        PointF temp;
        for (int i = 0; i < x.length; i++) {
            temp = PointUtils.rotate(x[i], y[i], ox, oy, cos, sin);
            x[i] = temp.x;
            y[i] = temp.y;
        }
    }
    /**
     * Rotate the polygon with the given angle
     * 
     * @param x
     *            list of x coordinates of the polygon
     * @param y
     *            list of y coordinates of the polygon
     * @param origin
     *            Point around which to rotate
     * 
     * @param angle
     *            to rotate
     * @throws IllegalArgumentException
     *             if <code>x</code> or <code>y</code> is null or their array
     *             lengths do not match
     */
    public static void rotate(float[] x, float[] y, PointF origin,
            float angle) throws IllegalArgumentException {
        float cos = (float) Math.cos((Math.PI * angle) / 180);
        float sin = (float) Math.sin((Math.PI * angle) / 180);
        rotate(x, y, origin.x, origin.y, cos, sin);

    }
    public static boolean isValidPolygon(float[] x, float[] y) {
        if (x == null || y == null) {
            return false;
        } else if (x.length != y.length) {
            return false;
        }
        return true;
    }
}

Related Tutorials