Jonathan Costa
Jonathan Costa

Reputation: 29

Java - Get new points of rotation in Graphics2D

I need to get new points of rotation to know the true position and dimension of the object, for a example:

Example 01.

The context of my application is rendering 2D graphics, the main for it below:

    protected void paintComponent(Graphics g) {
        ...

        g2d.rotate(Test.angle * Math.PI / 180, Test.hero, Test.heroY); 
        g2d.drawImage(Main.hero, Test.hero - 15, Test.heroY - 15, this);
        ...

The image is drawn correctly, but I cannot get the new points.

Upvotes: 2

Views: 158

Answers (1)

double angle = ang * Math.PI / 180;
double sin = Math.sin(angle);
double cos = Math.cos(angle);       

double a = (centerX + ray) - x0;
double b = centerY - y0;
int xx = (int) (a * cos - b * -sin + x0);
int yy = (int) (a * -sin + b * cos + y0);

ang++;

it will rotate a point around centerX and centerY in a +ray from these origins, the code has tested and works from all angles.

Upvotes: 1

Related Questions