Rotate By Radians - Android java.lang

Android examples for java.lang:Math

Description

Rotate By Radians

Demo Code


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Enumeration;
import java.util.Vector;
import com.android.main.Log;

public class Main{
    protected int x, y;
    public static Vector RotateByRadians(Vector points, double radians) {
        Vector newPoints = new Vector(points.size());
        Point c = Centroid(points);

        double _cos = Math.cos(radians);
        double _sin = Math.sin(radians);

        double cx = c.x;
        double cy = c.y;

        for (int i = 0; i < points.size(); i++) {
            Point p = (Point) points.elementAt(i);

            double dx = p.x - cx;
            double dy = p.y - cy;

            newPoints.addElement(new Point((dx * _cos) - (dy * _sin) + cx,
                    (dx * _sin) + (dy * _cos) + cy));
        }/*  w  w w  . j  a  v a2s.c o  m*/
        return newPoints;
    }
    public static Point Centroid(Vector points) {
        double xsum = 0.0;
        double ysum = 0.0;

        Enumeration e = points.elements();

        while (e.hasMoreElements()) {
            Point p = (Point) e.nextElement();
            xsum += p.x;
            ysum += p.y;
        }
        return new Point(xsum / points.size(), ysum / points.size());
    }
}

Related Tutorials