Ionel Lupu
Ionel Lupu

Reputation: 2811

rotate a matrix using different angles

I searched on internet and I saw lots of posts about how to rotate a matrix or an image by 90 or 180 degrees.But how can I rotate a matrix with 12 degrees or 162 degrees? From: enter image description here

To:

enter image description here

This image is rotated with ~35 degrees.

As you can see my matrix is the horse image and the circle is the rotation path, and the big rectangle is the new matrix created after rotation.

How can i achieve this? Thanks!

PS: This does not work

int angle=35*Math.PI/180;
int x1 = (int)(x * cos(angle)) - (y * sin(angle));
int y1 = (int)(y * cos(angle)) + (x * sin(angle));

Upvotes: 0

Views: 1598

Answers (1)

sinsedrix
sinsedrix

Reputation: 4795

Maybe your code would work if you saved x value before using it to compute y.

  • deg should be in radian not in degrees: 35*PI/180
  • you shouldn't compute with integers since cos and sin are between [0,1], think about floats.

float angle = 35*Math.PI/180;
int x1 = round(x * cos(angle) - y * sin(angle));
int y1 = round(y * cos(angle) + x * sin(angle));

Note: casting is huggly.

Upvotes: 2

Related Questions