Rotates a Line2D object around a Point2D object. - Android java.lang

Android examples for java.lang:Math Geometry

Description

Rotates a Line2D object around a Point2D object.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//from   w  ww.j av  a2  s  .c o m
 * 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 java.util.Arrays;
import android.graphics.PointF;

public class Main{
    /**
     * Rotates a Line2D object around a Point2D object.
     * 
     * @param line
     *            the line to rotate
     * @param origin
     *            the point around which to rotate
     * @param angle
     *            the angle (in degrees) of rotation
     */
    public static void rotate(PointF lineStart, PointF lineEnd,
            PointF origin, double angle) {
        float cos = (float) Math.cos((Math.PI * angle) / 180);
        float sin = (float) Math.sin((Math.PI * angle) / 180);
        rotate(lineStart, lineEnd, origin, cos, sin);
    }
    /**
     * Rotates a Line2D object around a Point2D object.
     * 
     * @param line
     *            the line to rotate
     * @param origin
     *            the point around which to rotate
     * @param cos
     *            the cosine of the rotation angle
     * @param sin
     *            the sine of the rotation angle
     */
    private static void rotate(PointF lineStart, PointF lineEnd,
            PointF origin, float cos, float sin) {
        PointF test = PointUtils.rotate(lineStart, origin, cos, sin);
        lineStart.x = test.x;
        lineStart.y = test.y;
        test = PointUtils.rotate(lineEnd, origin, cos, sin);
        lineEnd.x = test.x;
        lineEnd.y = test.y;
    }
}

Related Tutorials