Java Graphics Rotate rotate(final Point2D vec, final double theta)

Here you can find the source of rotate(final Point2D vec, final double theta)

Description

Rotates a vector around the origin.

License

Open Source License

Parameter

Parameter Description
vec The vector to rotate.
theta The angle in radians.

Return

The rotated vector.

Declaration

public static Point2D rotate(final Point2D vec, final double theta) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.geom.AffineTransform;

import java.awt.geom.Point2D;

public class Main {
    /**/*from   w w w . j  a va2 s  . c  o  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

  1. rotate(AffineTransform transform, float angleInDegrees)
  2. rotate(byte[] bytes)
  3. rotate(Dimension rect)
  4. rotate(final Point2D pos, final Point2D center, final double dist)
  5. rotate(Point p, double angle)
  6. rotate(Point toRotate, float tetta)
  7. rotate(Point2D.Double point, double dAngle)
  8. rotate(Shape shape, float angle, float x, float y)