Java Polygon Rotate rotatePolygon(Polygon poly, double degree)

Here you can find the source of rotatePolygon(Polygon poly, double degree)

Description

Rotates a polygon.

License

Apache License

Parameter

Parameter Description
poly a parameter
degree a parameter

Declaration

public static void rotatePolygon(Polygon poly, double degree) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.awt.Polygon;

public class Main {
    /**// w  w w  .  j ava 2  s.  co m
     * Rotates a polygon.
     * @param poly
     * @param degree 
     */
    public static void rotatePolygon(Polygon poly, double degree) {
        int[] nx = new int[poly.npoints];
        int[] ny = new int[poly.npoints];

        for (int i = 0; i < poly.npoints; i++) {
            // x'= x*cos(degree)+y*sin(degree)
            nx[i] = (int) Math.round(
                    ((double) poly.xpoints[i]) * Math.cos(degree) + ((double) poly.ypoints[i]) * Math.sin(degree));
            // y'= x*cos(degree)-y*sin(degree)
            ny[i] = (int) Math.round(
                    ((double) poly.xpoints[i]) * Math.sin(degree) - ((double) poly.ypoints[i]) * Math.cos(degree));
        }

        poly.xpoints = nx;
        poly.ypoints = ny;

    }
}

Related

  1. rotate(Polygon poly, double theta)
  2. rotateCW(Polygon polygon, int height)
  3. rotatePolygon(Polygon pg, double rotAngle, Point centroid, Polygon original)