Rotates a vector around the origin. - Java java.lang

Java examples for java.lang:Math Vector

Description

Rotates a vector around the origin.

Demo Code


//package com.java2s;
import java.awt.geom.AffineTransform;

import java.awt.geom.Point2D;

public class Main {
    /**/*  ww  w .  j ava2  s . co  m*/
     * Rotates a vector around the origin. The angle is measured in radians and
     * positive angles are counter-clockwise or in negative y direction. Note that
     * positive y direction points downwards.
     * 
     * @param vec The vector to rotate.
     * @param theta The angle in radians.
     * @return The rotated vector.
     */
    public static Point2D rotate(final Point2D vec, final double theta) {
        final double cos = Math.cos(-theta);
        final double sin = Math.sin(-theta);
        final double x = vec.getX();
        final double y = vec.getY();
        return new Point2D.Double(x * cos - y * sin, x * sin + y * cos);
    }

    /**
     * Rotates a point around the center such that the result has the given
     * distance to the original point.
     * 
     * @param pos The point to rotate.
     * @param center The center.
     * @param dist The distance. Positive values rotate in counter-clockwise or
     *          negative y direction.
     * @return The point that has the given distance to the original point. Or is
     *         at the exact opposite position if the distance is larger than the
     *         diameter. Note that positive y direction points downwards.
     */
    public static Point2D rotate(final Point2D pos, final Point2D center,
            final double dist) {
        final double f = dist > 0 ? 1 : -1;
        final double dSq = dist * dist;
        final Point2D rad = subVec(pos, center);
        final double radSq = getLengthSq(rad);
        if (dSq > 4 * radSq)
            return subVec(center, rad);
        return rotateByAngle(pos, center,
                f * Math.acos(1 - dSq * 0.5 / radSq));
    }

    /**
     * Subtracts two points. <code>a - b</code>
     * 
     * @param a Point.
     * @param b Point.
     * @return The difference vector.
     */
    public static Point2D subVec(final Point2D a, final Point2D b) {
        return new Point2D.Double(a.getX() - b.getX(), a.getY() - b.getY());
    }

    /**
     * Calculates the squared length of a vector.
     * 
     * @param v The vector.
     * @return The squared length.
     */
    public static double getLengthSq(final Point2D v) {
        return v.getX() * v.getX() + v.getY() * v.getY();
    }

    /**
     * Rotates a point a given angle around the center.
     * 
     * @param pos The point to rotate.
     * @param center The center.
     * @param angle The angle in radians and interpreted counter-clockwise or
     *          negative y direction. Note that the y direction is downwards.
     * @return The rotated point.
     */
    public static Point2D rotateByAngle(final Point2D pos,
            final Point2D center, final double angle) {
        final AffineTransform at = AffineTransform.getRotateInstance(
                -angle, center.getX(), center.getY());
        return at.transform(pos, null);
    }
}

Related Tutorials