Here you can find the source of rotateBy(Vector
Parameter | Description |
---|---|
points | the points to rotate |
theta | the angle in radians |
newPoints | the points where to store rotated points |
public static void rotateBy(Vector<Point2D> points, double theta, Vector<Point2D> newPoints)
//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); } }