Java Graphics Rotate rotateToZero(Vector points, Vector newPoints)

Here you can find the source of rotateToZero(Vector points, Vector newPoints)

Description

rotate To Zero

License

Open Source License

Declaration

public static void rotateToZero(Vector<Point2D> points, Vector<Point2D> newPoints) 

Method Source Code

//package com.java2s;
/*  //from w  ww.  jav  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 {
    public static void rotateToZero(Vector<Point2D> points, Vector<Point2D> newPoints) {
        Point2D c = centroid(points);
        double theta = Math.atan2(c.getY() - points.get(0).getY(), c.getX() - points.get(0).getX());
        rotateBy(points, -theta, newPoints);
    }

    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);
    }

    /**
     * @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());
        }
    }
}

Related

  1. rotatePoint(Point reference, Point toRotate, int degrees)
  2. rotatePoint(Point target, Point origin, double theta)
  3. rotatePoint2D(final Point aPoint, final float aDeg)
  4. rotateRAD(double x, double y, double a)
  5. rotateShape(final Shape base, final double angle, final float x, final float y)