Rotates a point around another specified point. - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

Rotates a point around another specified point.

Demo Code



public class Main{
    /**/*from w  w w  . j a  v  a  2 s  .com*/
     * Rotates a point around another specified point.
     * 
     * @param point
     *        The point to rotate.
     * @param origin
     *        The rotation origin or "axis".
     * @param rotation
     *        The rotation amount in radians.
     * @return The position of the point after rotating it.
     */
    public static Vector2 rotateAboutOrigin(Vector2 point, Vector2 origin,
            float rotation) {
        // Point relative to origin
        Vector2 u = new Vector2(Vector2.subtract(point, origin));

        if (u.equals(Vector2.ZERO))
            return point;

        // Angle relative to origin
        float a = (float) Math.atan2(u.getY(), u.getX());

        // Rotate
        a += rotation;

        // U is now the new point relative to origin
        u = new Vector2((float) Math.cos(a) * u.length(),
                (float) Math.sin(a) * u.length());

        return u.add(origin);
    }
}

Related Tutorials