Java Graphics Rotate rotatePoint(Point reference, Point toRotate, int degrees)

Here you can find the source of rotatePoint(Point reference, Point toRotate, int degrees)

Description

Rotates a point with respect to a center point.

License

Open Source License

Parameter

Parameter Description
reference - The point at which the text was drawn
toRotate - The approximate center of the drawn text
degrees - The degrees by whith the Text/Font is rotated

Declaration

public static Point rotatePoint(Point reference, Point toRotate,
        int degrees) 

Method Source Code

//package com.java2s;
/*//w w  w. j ava2  s .c o  m
 * Copyright (c) 2017, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

import java.awt.Point;

public class Main {
    /**
     * Rotates a point with respect to a center point. This is used for calculating
     * the new center of a character after it has been rotated.
     * http://www.gamefromscratch.com/post/2012/11/24/GameDev-math-recipes-Rotating-one-point-around-another-point.aspx
     *
     * @param reference - The point at which the text was drawn
     * @param toRotate  - The approximate center of the drawn text
     * @param degrees   - The degrees by whith the Text/Font is rotated
     * @return
     */
    public static Point rotatePoint(Point reference, Point toRotate,
            int degrees) {
        double angle = degrees;
        angle = (angle) * (Math.PI / 180);
        double rotatedX = Math.cos(angle) * (toRotate.x - reference.x)
                - Math.sin(angle) * (toRotate.y - reference.y)
                + reference.x;
        double rotatedY = Math.sin(angle) * (toRotate.x - reference.x)
                + Math.cos(angle) * (toRotate.y - reference.y)
                + reference.y;
        return new Point((int) rotatedX, (int) rotatedY);
    }
}

Related

  1. rotatePoint(double x0, double y0, double x, double y, double a)
  2. rotatePoint(double x0, double y0, double x, double y, double angle)
  3. rotatePoint(int x, int y, int xC, int yC, float angle)
  4. rotatePoint(Point center, Point p, double angle)
  5. rotatePoint(Point point, Point centerPoint, double angle)
  6. rotatePoint(Point target, Point origin, double theta)
  7. rotatePoint2D(final Point aPoint, final float aDeg)
  8. rotateRAD(double x, double y, double a)
  9. rotateShape(final Shape base, final double angle, final float x, final float y)