Java Graphics Rotate rotate(final Point2D pos, final Point2D center, final double dist)

Here you can find the source of rotate(final Point2D pos, final Point2D center, final double dist)

Description

Rotates a point around the center such that the result has the given distance to the original point.

License

Open Source License

Parameter

Parameter Description
pos The point to rotate.
center The center.
dist The distance. Positive values rotate in clockwise direction.

Exception

Parameter Description
IllegalArgumentException When the distance is longer than thediameter.

Return

The point that has the given distance to the original point.

Declaration

public static Point2D rotate(final Point2D pos, final Point2D center, final double dist) 

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 {
    /**/* w w  w . j a v a 2  s  .com*/
     * 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 clockwise direction.
     * @return The point that has the given distance to the original point.
     * @throws IllegalArgumentException When the distance is longer than the
     *           diameter.
     */
    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.
     * 
     * @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.
     * @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 vec, final double theta)
  5. rotate(Point p, double angle)
  6. rotate(Point toRotate, float tetta)
  7. rotate(Point2D.Double point, double dAngle)