Java Graphics Rotate rotatePoint(int x, int y, int xC, int yC, float angle)

Here you can find the source of rotatePoint(int x, int y, int xC, int yC, float angle)

Description

Rotates a 2-dimensional point with the coordinates x,y arount another point xC,yC with the specified angle.

License

Open Source License

Parameter

Parameter Description
x the x-coordinate of the point
y the y-coordinate of the point
xC the x-coordinate of the rotation center
yC the y-coordinate of the rotation center
angle the angle of rotation in degrees

Return

the rotated

Declaration

public static Point rotatePoint(int x, int y, int xC, int yC, float angle) 

Method Source Code

//package com.java2s;
/**//from  ww  w  .j a  v a 2s.  c  o m
    
   BreadBoard Editor
   Written and maintained by Matthias Pueski 
       
   Copyright (c) 2010-2012 Matthias Pueski
       
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.
       
   This program 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 General Public License for more details.
       
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    
 */

import java.awt.Point;

public class Main {
    /**
     * Rotates a 2-dimensional point with the coordinates x,y arount another
     * point xC,yC with the specified angle.
     * 
     * @param x the x-coordinate of the point
     * @param y the y-coordinate of the point
     * @param xC the x-coordinate of the rotation center
     * @param yC the y-coordinate of the rotation center
     * @param angle the angle of rotation in degrees
     * 
     * @return the rotated {@link Point}
     */
    public static Point rotatePoint(int x, int y, int xC, int yC, float angle) {
        float rot = (float) Math.toRadians(angle);
        double dx = xC + (x - xC) * Math.cos(rot) + (y - yC) * Math.sin(rot);
        double dy = yC - (x - xC) * Math.sin(rot) + (y - yC) * Math.cos(rot);
        int x_ = (int) dx;
        int y_ = (int) dy;
        return new Point(x_, y_);
    }

    public static Point rotatePoint(double x, double y, int xC, int yC, float angle) {
        float rot = (float) Math.toRadians(angle);
        double dx = xC + (x - xC) * Math.cos(rot) + (y - yC) * Math.sin(rot);
        double dy = yC - (x - xC) * Math.sin(rot) + (y - yC) * Math.cos(rot);
        int x_ = (int) dx;
        int y_ = (int) dy;
        return new Point(x_, y_);
    }
}

Related

  1. rotateHue(Color color, float fraction)
  2. rotateHue(Color color, float fraction)
  3. rotateMoveClockwise(Point move, int size)
  4. rotatePoint(double x0, double y0, double x, double y, double a)
  5. rotatePoint(double x0, double y0, double x, double y, double angle)
  6. rotatePoint(Point center, Point p, double angle)
  7. rotatePoint(Point point, Point centerPoint, double angle)
  8. rotatePoint(Point reference, Point toRotate, int degrees)
  9. rotatePoint(Point target, Point origin, double theta)