Moves a Polygon (with respect to it's origin) to specified point. - Android Graphics

Android examples for Graphics:Path Point

Description

Moves a Polygon (with respect to it's origin) to specified point.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*  w ww . j  a v a 2  s. com*/
 * 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{
    /**
     * Moves a Polygon (with respect to it's origin) to specified point.
     * 
     * @param x
     *            polygon's x coordinates
     * @param y
     *            polygon's y coordinates
     * @param point
     *            the point to move to
     */
    public static void moveTo(float[] x, float[] y, PointF point) {
        translate(x, y, point.x, point.y);
    }
    /**
     * 
     * 
     * @param x
     * @param y
     * @param dx
     * @param dy
     * @throws IllegalArgumentException
     *             if <code>x</code> or <code>y</code> is null or their array
     *             lengths do not match
     */
    public static void translate(float[] x, float[] y, float dx, float dy)
            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 = new PointF(x[i], y[i]);
            PointUtils.translate(temp, dx, dy);
            x[i] = temp.x;
            y[i] = temp.y;
        }
    }
    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