Java Graphics Rotate rotateBy(Vector points, double theta, Vector newPoints)

Here you can find the source of rotateBy(Vector points, double theta, Vector newPoints)

Description

rotate By

License

Open Source License

Parameter

Parameter Description
points the points to rotate
theta the angle in radians
newPoints the points where to store rotated points

Declaration

public static void rotateBy(Vector<Point2D> points, double theta, Vector<Point2D> newPoints) 

Method Source Code

//package com.java2s;
/*  //from ww w .j  a v  a  2 s  .  com
 *   Authors: Caroline Appert (caroline.appert@lri.fr)
 *   Copyright (c) Universite Paris-Sud XI, 2007. All Rights Reserved
 *   Licensed under the GNU LGPL. For full terms see the file COPYING.
*/

import java.awt.geom.Point2D;

import java.util.Iterator;
import java.util.Vector;

public class Main {
    /**
     * @param points
     *            the points to rotate
     * @param theta
     *            the angle in radians
     * @param newPoints
     *            the points where to store rotated points
     */
    public static void rotateBy(Vector<Point2D> points, double theta, Vector<Point2D> newPoints) {
        Point2D c = centroid(points);
        Point2D ptSrc, ptDest;
        for (int i = 0; i < points.size(); i++) {
            ptSrc = points.get(i);
            if (newPoints.size() > i) {
                ptDest = newPoints.get(i);
            } else {
                ptDest = new Point2D.Double();
                newPoints.add(i, ptDest);
            }
            ptDest.setLocation(
                    (ptSrc.getX() - c.getX()) * Math.cos(theta) - (ptSrc.getY() - c.getY()) * Math.sin(theta)
                            + c.getX(),
                    (ptSrc.getX() - c.getX()) * Math.sin(theta) + (ptSrc.getY() - c.getY()) * Math.cos(theta)
                            + c.getY());
        }
    }

    public static Point2D centroid(Vector<Point2D> points) {
        double sumX = 0;
        double sumY = 0;
        for (Iterator<Point2D> iterator = points.iterator(); iterator.hasNext();) {
            Point2D next = iterator.next();
            sumX += next.getX();
            sumY += next.getY();
        }
        int length = points.size();
        return new Point2D.Double(sumX / length, sumY / length);
    }
}

Related

  1. rotate(Point toRotate, float tetta)
  2. rotate(Point2D.Double point, double dAngle)
  3. rotate(Shape shape, float angle, float x, float y)
  4. rotate_point(double rx, double ry, double angle, double cx, double cy)
  5. rotateArea(Area a, double rotation, Point2D rotateAround)
  6. rotateByAngle(final Point2D pos, final Point2D center, final double angle)
  7. rotateClockwise(Path source, Path target)
  8. rotateCoor(float x, float y, float theta)
  9. rotateCoorAroundPoint(float x, float y, float xctr, float yctr, float theta)