Skip to content

Instantly share code, notes, and snippets.

@MinSomai
Created February 22, 2020 18:26
Show Gist options
  • Save MinSomai/5224783639ba6ef6e0e9c391e0eed2bf to your computer and use it in GitHub Desktop.
Save MinSomai/5224783639ba6ef6e0e9c391e0eed2bf to your computer and use it in GitHub Desktop.
package hw;
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
public class RotationNegativeAboutOrigin13 extends JFrame{
int xPoly[]= {30,65,86,100,145,95,60};
int yPoly[]= {30,20,36,56,75,105,60};
int i;
RotationNegativeAboutOrigin13(){
setTitle("Rotation On Negative");
setSize(1200,1200);
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.translate(400,400);
g.drawPolygon(xPoly, yPoly, xPoly.length);
rotateAt(-45);
}
public void rotateAt(double deg) {
double angleInRadian=Math.toRadians(deg);
for(i=0;i<xPoly.length;i++) {
xPoly[i]=(int)Math.round(xPoly[i]*Math.cos(angleInRadian)-yPoly[i]*Math.sin(angleInRadian));
yPoly[i]=(int)Math.round(xPoly[i]*Math.sin(angleInRadian)+yPoly[i]*Math.cos(angleInRadian));
}
Graphics g=this.getGraphics();
g.translate(400, 400);
g.drawPolygon(xPoly, yPoly, xPoly.length);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
new RotationNegativeAboutOrigin13();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment