Java Polygon Rotate rotatePolygon(Polygon pg, double rotAngle, Point centroid, Polygon original)

Here you can find the source of rotatePolygon(Polygon pg, double rotAngle, Point centroid, Polygon original)

Description

Rotates a polygon by an angle alpha.

License

Open Source License

Parameter

Parameter Description
pg The polygon to be rotated
rotAngle The rotation angle in <b>radians</b>.
centroid The centroid of the polygon.
original When rotating a polygon with a mouse, the original must be maintained.

Declaration


public static void rotatePolygon(Polygon pg, double rotAngle, Point centroid, Polygon original) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

public class Main {
    /**//from  w w w.  j a v  a2 s  . c om
     * Rotates a polygon by an angle alpha. <b>The polygon is passed by
     * reference.</b>
     *
     * @param pg       The polygon to be rotated
     * @param rotAngle The rotation angle in <b>radians</b>.
     * @param centroid The centroid of the polygon.
     * @param original When rotating a polygon with a mouse, the original must be
     *                 maintained.
     */

    public static void rotatePolygon(Polygon pg, double rotAngle, Point centroid, Polygon original) {

        double x, y;
        for (int i = 0; i < pg.npoints; i++) {
            if (original != null) {
                x = original.xpoints[i] - centroid.x;
                y = original.ypoints[i] - centroid.y;
            } else {
                x = pg.xpoints[i] - centroid.x;
                y = pg.ypoints[i] - centroid.y;
            }
            pg.xpoints[i] = centroid.x + (int) Math.round(((x * Math.cos(rotAngle)) - (y * Math.sin(rotAngle))));
            pg.ypoints[i] = centroid.y + (int) Math.round(((x * Math.sin(rotAngle)) + (y * Math.cos(rotAngle))));
        }
    }

    /**
     * Rotates the polygon and returns it's center of mass.
     *
     * @param pg       The polygon to rotate around it's center of mass. Verticies
     *                 are integer points. Used mainly for physical coordinate
     *                 rotation.
     * @param rotangle -
     *                 Rotation angle in radians.
     */
    public static Point rotatePolygon(Polygon pg, double rotAngle) {

        double x, y;
        Point centroid = polygonCenterOfMass(pg);
        for (int i = 0; i < pg.npoints; i++) {
            x = pg.xpoints[i] - centroid.x;
            y = pg.ypoints[i] - centroid.y;
            pg.xpoints[i] = centroid.x + (int) Math.round(((x * Math.cos(rotAngle)) - (y * Math.sin(rotAngle))));
            pg.ypoints[i] = centroid.y + (int) Math.round(((x * Math.sin(rotAngle)) + (y * Math.cos(rotAngle))));
        }
        return centroid;
    }

    /**
     * Finds the centroid of a polygon with integer verticies.
     *
     * @param pg The polygon to find the centroid of.
     *
     * @return The centroid of the polygon.
     */

    public static Point polygonCenterOfMass(Polygon pg) {

        if (pg == null) {
            return null;
        }

        int N = pg.npoints;
        Point[] polygon = new Point[N];

        for (int q = 0; q < N; q++) {
            polygon[q] = new Point(pg.xpoints[q], pg.ypoints[q]);
        }

        double cx = 0, cy = 0;
        double A = PolygonArea(polygon, N);
        int i, j;

        double factor = 0;
        for (i = 0; i < N; i++) {
            j = (i + 1) % N;
            factor = (polygon[i].x * polygon[j].y - polygon[j].x * polygon[i].y);
            cx += (polygon[i].x + polygon[j].x) * factor;
            cy += (polygon[i].y + polygon[j].y) * factor;
        }
        factor = 1.0 / (6.0 * A);
        cx *= factor;
        cy *= factor;
        return new Point((int) Math.abs(Math.round(cx)), (int) Math.abs(Math.round(cy)));
    }

    /**
     * Computes the area of any two-dimensional polygon.
     *
     * @param polygon The polygon to compute the area of input as an array of points
     * @param N       The number of points the polygon has, first and last point
     *                inclusive.
     *
     * @return The area of the polygon.
     */
    public static double PolygonArea(Point[] polygon, int N) {

        int i, j;
        double area = 0;

        for (i = 0; i < N; i++) {
            j = (i + 1) % N;
            area += polygon[i].x * polygon[j].y;
            area -= polygon[i].y * polygon[j].x;
        }

        area /= 2.0;
        return (Math.abs(area));
    }
}

Related

  1. rotate(Polygon poly, double theta)
  2. rotateCW(Polygon polygon, int height)
  3. rotatePolygon(Polygon poly, double degree)