I am having a problem in my program. I want to rotate a rectangular polygon. The problem is that the polygon shrinks in size automatically with every iteration. I have no idea what is wrong. This is what I have done :-
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;
public class Player {
/* fields required for making a rectangular polygon */
private Polygon polyrec;
private int wd;
private int hi;
private int npoint;
private int[] xpoint;
private int[] ypoint;
private Color color;
private int angle;
public Player() {
color = new Color(255, 0, 0);
/* making the rectangular polygon */
wd = 150;
hi = 150;
npoint = 4;
xpoint = new int[npoint];
ypoint = new int[npoint];
xpoint[0] = 200;
ypoint[0] = 300;
xpoint[1] = 200 + wd;
ypoint[1] = 300;
xpoint[2] = 200 + wd;
ypoint[2] = 300 + hi;
xpoint[3] = 200;
ypoint[3] = 300 + hi;
polyrec = new Polygon(xpoint, ypoint, npoint);
//how much rotate
angle = 5;
}
public void paint(Graphics g) {
/* drawing the rectangular polygon */
g.setColor(color);
g.drawPolygon(polyrec);
}
public void update() {
/* updating the rectangular polygon */
updatePolyRec();
}
private void updatePolyRec() {
/* translating the rectangular polygon to the origin */
xpoint[0] = xpoint[0] - (200 + wd/2);
ypoint[0] = ypoint[0] - (300 + hi/2);
xpoint[1] = xpoint[1] - (200 + wd/2);
ypoint[1] = ypoint[1] - (300 + hi/2);
xpoint[2] = xpoint[2] - (200 + wd/2);
ypoint[2] = ypoint[2] - (300 + hi/2);
xpoint[3] = xpoint[3] - (200 + wd/2);
ypoint[3] = ypoint[3] - (300 + hi/2);
polyrec = new Polygon(xpoint, ypoint, npoint);
/* rotating the polygon at the origin (rotating from its center) */
// angles which the x axis make with respective points of the polygon in radian measure
double angle1 = Math.atan2(-ypoint[0], xpoint[0]);
double angle2 = Math.atan2(-ypoint[1], xpoint[1]);
double angle3 = Math.atan2(-ypoint[2], xpoint[2]);
double angle4 = Math.atan2(-ypoint[3], xpoint[3]);
// converting radians to degrees
angle1 = Math.toDegrees(angle1);
angle2 = Math.toDegrees(angle2);
angle3 = Math.toDegrees(angle3);
angle4 = Math.toDegrees(angle4);
// degrees in anti-clockwise direction from 0 to 360 degrees
if(angle1 < 0) {
angle1 = 180 + (180 - Math.abs(angle1));
}
if(angle2 < 0) {
angle2 = 180 + (180 - Math.abs(angle2));
}
if(angle3 < 0) {
angle3 = 180 + (180 - Math.abs(angle3));
}
if(angle4 < 0) {
angle4 = 180 + (180 - Math.abs(angle4));
}
//adding required amount to the angles for rotation
angle1 += angle;
angle2 += angle;
angle3 += angle;
angle4 += angle;
// respective dx and dy
double dx1 = Math.cos(Math.toRadians(angle1));
double dy1 = Math.sin(Math.toRadians(angle1));
double dx2 = Math.cos(Math.toRadians(angle2));
double dy2 = Math.sin(Math.toRadians(angle2));
double dx3 = Math.cos(Math.toRadians(angle3));
double dy3 = Math.sin(Math.toRadians(angle3));
double dx4 = Math.cos(Math.toRadians(angle4));
double dy4 = Math.sin(Math.toRadians(angle4));
//distances between origin & x (distanceX) , and origin & y (distanceY) of the points respectively
double distancex1 = Math.abs((0 + xpoint[0]) / 2);
double distancey1 = Math.abs((0 + ypoint[0]) / 2);
double distancex2 = Math.abs((0 + xpoint[1]) / 2);
double distancey2 = Math.abs((0 + ypoint[1]) / 2);
double distancex3 = Math.abs((0 + xpoint[2]) / 2);
double distancey3 = Math.abs((0 + ypoint[2]) / 2);
double distancex4 = Math.abs((0 + xpoint[3]) / 2);
double distancey4 = Math.abs((0 + ypoint[3]) / 2);
//multiplying dx with distanceX and dy with distanceY, respectively
xpoint[0] = (int)(distancex1 * dx1);
ypoint[0] = (int)(distancey1 * dy1);
xpoint[1] = (int)(distancex2 * dx2);
ypoint[1] = (int)(distancey2 * dy2);
xpoint[2] = (int)(distancex3 * dx3);
ypoint[2] = (int)(distancey3 * dy3);
xpoint[3] = (int)(distancex4 * dx4);
ypoint[3] = (int)(distancey4 * dy4);
//translating back the rotated polygon to where it was before
xpoint[0] = xpoint[0] + (200 + wd/2);
ypoint[0] = ypoint[0] + (300 + hi/2);
xpoint[1] = xpoint[1] + (200 + wd/2);
ypoint[1] = ypoint[1] + (300 + hi/2);
xpoint[2] = xpoint[2] + (200 + wd/2);
ypoint[2] = ypoint[2] + (300 + hi/2);
xpoint[3] = xpoint[3] + (200 + wd/2);
ypoint[3] = ypoint[3] + (300 + hi/2);
polyrec = new Polygon(xpoint, ypoint, npoint);
}
}