Java Graphics Rotate rotateCoorAroundPoint(float x, float y, float xctr, float yctr, float theta)

Here you can find the source of rotateCoorAroundPoint(float x, float y, float xctr, float yctr, float theta)

Description

rotates a point theta radians around a point (xctr, yctr), returns a new point rotation is counterclockwise for positive theta in Cartesian system, clockwise in screen display coordinate system

License

Open Source License

Parameter

Parameter Description
x x coordinate of point
y y coordinate of point
xctr x coordinate of center of rotation
yctr y coordinate of center of rotation
theta angle to rotate, in radians

Return

a new rotated point

Declaration

public static Point2D.Float rotateCoorAroundPoint(float x, float y, float xctr, float yctr, float theta) 

Method Source Code


//package com.java2s;
/*//ww  w  .  j av  a 2s .co m
 * Copyright (c) 2011, Paul Hertz This library is free software; you can
 * redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation; either version
 * 3.0 of the License, or (at your option) any later version.
 * http://www.gnu.org/licenses/lgpl.html This library is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU Lesser General Public License for more details. You should have
 * received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301, USA
 * 
 * @author      ##author##
 * @modified   ##date##
 * @version      ##version##
 * 
 */

import java.awt.geom.*;

public class Main {
    /**
     * rotates a point theta radians around a point (xctr, yctr), returns a new point
     * rotation is counterclockwise for positive theta in Cartesian system, 
     * clockwise in screen display coordinate system
     * @param x       x coordinate of point
     * @param y       y coordinate of point
     * @param xctr    x coordinate of center of rotation
     * @param yctr    y coordinate of center of rotation
     * @param theta   angle to rotate, in radians
     * @return        a new rotated point
     */
    public static Point2D.Float rotateCoorAroundPoint(float x, float y, float xctr, float yctr, float theta) {
        // Rotate vector or point (x,y) around point (xctr, yctr) through an angle theta
        // degrees in radians, rotation is counterclockwise from the coordinate axis
        // returns a new point
        double sintheta = Math.sin(theta);
        double costheta = Math.cos(theta);
        Point2D.Float pt = translateCoor(x, y, -xctr, -yctr);
        pt.setLocation((pt.x * costheta - pt.y * sintheta), (pt.x * sintheta + pt.y * costheta));
        return translateCoor(pt, xctr, yctr);
    }

    public static Point2D.Double rotateCoorAroundPoint(double x, double y, double xctr, double yctr, double theta) {
        // Rotate vector or point (x,y) around point (xctr, yctr) through an angle theta
        // degrees in radians, rotation is counterclockwise from the coordinate axis
        // returns a new point
        double sintheta = Math.sin(theta);
        double costheta = Math.cos(theta);
        Point2D.Double pt = translateCoor(x, y, -xctr, -yctr);
        pt.setLocation((pt.x * costheta - pt.y * sintheta), (pt.x * sintheta + pt.y * costheta));
        return translateCoor(pt, xctr, yctr);
    }

    /**
     * rotates a point theta radians around a point (xctr, yctr), modifies and returns the point
     * rotation is counterclockwise for positive theta in Cartesian system, 
     * clockwise in screen display coordinate system
     * @param pt      the point to rotate
     * @param xctr    x coordinate of center of rotation
     * @param yctr    y coordinate of center of rotation
     * @param theta   angle to rotate, in radians
     * @return        the rotated point
     */
    public static Point2D.Float rotateCoorAroundPoint(Point2D.Float pt, float xctr, float yctr, float theta) {
        // Rotate vector or point (x,y) around point (xctr, yctr) through an angle theta
        // degrees in radians, rotation is counterclockwise from the coordinate axis
        // modifies and returns same point
        // center point around 0,0
        translateCoor(pt, -xctr, -yctr);
        // rotate
        rotateCoor(pt, theta);
        // translate back to xctr, yctr
        translateCoor(pt, xctr, yctr);
        return pt;
    }

    public static Point2D.Double rotateCoorAroundPoint(Point2D.Double pt, double xctr, double yctr, double theta) {
        // Rotate vector or point (x,y) around point (xctr, yctr) through an angle theta
        // degrees in radians, rotation is counterclockwise from the coordinate axis
        // modifies and returns same point
        // center point around 0,0
        translateCoor(pt, -xctr, -yctr);
        // rotate
        rotateCoor(pt, theta);
        // translate back to xctr, yctr
        translateCoor(pt, xctr, yctr);
        return pt;
    }

    /**
     * translates a point by xOffset and yOffset, returns a new point
     * @param x         x coordinate of point
     * @param y         y coordinate of point
     * @param xOffset   distance to translate on x-xis
     * @param yOffset   distance to translate on y-axis
     * @return          a new translated point
     */
    public static Point2D.Float translateCoor(float x, float y, float xOffset, float yOffset) {
        return new Point2D.Float(x + xOffset, y + yOffset);
    }

    public static Point2D.Double translateCoor(double x, double y, double xOffset, double yOffset) {
        return new Point2D.Double(x + xOffset, y + yOffset);
    }

    /**
     * translates a point by xOffset and yOffset, modifies and returns the point
     * @param pt        the point to translate
     * @param xOffset   distance to translate on x-xis
     * @param yOffset   distance to translate on y-axis
     * @return          the translated point
     */
    public static Point2D.Float translateCoor(Point2D.Float pt, float xOffset, float yOffset) {
        // move a vector or point (x,y) distances xOffset,yOffset
        // modifies and returns point
        pt.setLocation(pt.x + xOffset, pt.y + yOffset);
        return pt;
    }

    public static Point2D.Double translateCoor(Point2D.Double pt, double xOffset, double yOffset) {
        // move a vector or point (x,y) distances xOffset,yOffset
        // modifies and returns point
        pt.setLocation(pt.x + xOffset, pt.y + yOffset);
        return pt;
    }

    /**
     * rotates a point theta radians, returns a new point
     * rotation is counterclockwise for positive theta in Cartesian system, 
     * clockwise in screen display coordinate system
     * @param x       x coordinate of point
     * @param y       y coordinate of point
     * @param theta   angle to rotate, in radians
     * @return        a new point
     */
    public static Point2D.Float rotateCoor(float x, float y, float theta) {
        double sintheta = Math.sin(theta);
        double costheta = Math.cos(theta);
        return new Point2D.Float((float) (x * costheta - y * sintheta), (float) (x * sintheta + y * costheta));
    }

    public static Point2D.Double rotateCoor(double x, double y, double theta) {
        double sintheta = Math.sin(theta);
        double costheta = Math.cos(theta);
        return new Point2D.Double((x * costheta - y * sintheta), (x * sintheta + y * costheta));
    }

    /**
     * rotates a point theta radians, modifies and returns the point
     * rotation is counterclockwise for positive theta in Cartesian system, 
     * clockwise in screen display coordinate system
     * @param pt      the point to rotate
     * @param theta   angle to rotate, in radians
     * @return        the rotated point
     */
    public static Point2D.Float rotateCoor(Point2D.Float pt, float theta) {
        // Rotate vector or point (x,y) through an angle theta
        // degrees in radians, rotation is counterclockwise from the coordinate axis
        // modifies and returns same point
        double sintheta = Math.sin(theta);
        double costheta = Math.cos(theta);
        pt.setLocation((pt.x * costheta - pt.y * sintheta), (pt.x * sintheta + pt.y * costheta));
        return pt;
    }

    public static Point2D.Double rotateCoor(Point2D.Double pt, double theta) {
        // Rotate vector or point (x,y) through an angle theta
        // degrees in radians, rotation is counterclockwise from the coordinate axis
        // modifies and returns same point
        double sintheta = Math.sin(theta);
        double costheta = Math.cos(theta);
        pt.setLocation((pt.x * costheta - pt.y * sintheta), (pt.x * sintheta + pt.y * costheta));
        return pt;
    }
}

Related

  1. rotateArea(Area a, double rotation, Point2D rotateAround)
  2. rotateBy(Vector points, double theta, Vector newPoints)
  3. rotateByAngle(final Point2D pos, final Point2D center, final double angle)
  4. rotateClockwise(Path source, Path target)
  5. rotateCoor(float x, float y, float theta)
  6. rotateHue(Color color, float fraction)
  7. rotateHue(Color color, float fraction)
  8. rotateMoveClockwise(Point move, int size)
  9. rotatePoint(double x0, double y0, double x, double y, double a)