0
votes

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));
1
What language are you using ? - sinsedrix
java.I just want to rotate a matrix for a graphic engine using pixels - Ionel Lupu
I'd say your second example image is rotated -35 degrees, or 325 degrees (positive rotation is typically counter clockwise). Also, your variable name is a bit misleading now, i'd call it "angle" or "radians" since it now is representing an angle expressed in radians. - Anders Forsgren

1 Answers

2
votes

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.