Rotate To Zero - Android java.lang

Android examples for java.lang:Math

Description

Rotate To Zero

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 RotateToZero(Vector points) {
        return RotateToZero(points, null, null);
    }//from www  .j  a  va2  s.  c  om
    public static Vector RotateToZero(Vector points, Point centroid,
            Rectangle boundingBox) {
        Point c = Centroid(points);
        Point first = (Point) points.elementAt(0);
        //angle between first element and centrood
        double theta = Math.atan2(c.y - first.y, c.x - first.x);

        if (centroid != null)
            centroid.copy(c);

        if (boundingBox != null)
            BoundingBox(points, boundingBox);
        //rotate points such that this line at 0 deg
        return RotateBy(points, -theta);
    }
    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());
    }
    public static Rectangle BoundingBox(Vector points) {
        double minX = Double.MAX_VALUE;
        double maxX = Double.MIN_VALUE;
        double minY = Double.MAX_VALUE;
        double maxY = Double.MIN_VALUE;

        Enumeration e = points.elements();

        //      foreach (Point p in points)
        while (e.hasMoreElements()) {
            Point p = (Point) e.nextElement();

            if (p.x < minX)
                minX = p.x;
            if (p.x > maxX)
                maxX = p.x;

            if (p.y < minY)
                minY = p.y;
            if (p.y > maxY)
                maxY = p.y;
        }

        return new Rectangle(minX, minY, maxX - minX, maxY - minY);
    }
    public static void BoundingBox(Vector points, Rectangle dst) {
        double minX = Double.MAX_VALUE;
        double maxX = Double.MIN_VALUE;
        double minY = Double.MAX_VALUE;
        double maxY = Double.MIN_VALUE;

        Enumeration e = points.elements();

        //      foreach (Point p in points)
        while (e.hasMoreElements()) {
            Point p = (Point) e.nextElement();

            if (p.x < minX)
                minX = p.x;
            if (p.x > maxX)
                maxX = p.x;

            if (p.y < minY)
                minY = p.y;
            if (p.y > maxY)
                maxY = p.y;
        }

        dst.X = minX;
        dst.Y = minY;
        dst.Width = maxX - minX;
        dst.Height = maxY - minY;
    }
    public static Vector RotateBy(Vector points, double theta) {
        return RotateByRadians(points, theta);
    }
    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));
        }
        return newPoints;
    }
}

Related Tutorials