0
I am not great in math, can someone tell me how to rotate a 2d vector (not the array)
Here is my vec2 class : https://code.sololearn.com/cuAnHqsVJZzf/?ref=app
9 Réponses
+ 3
Change your setangle() method to this:
void vec2::setangle(double angle)
//So that one can input angles like 125.5 as well...
{
double radangle=(pi*angle)/180.0;
double mag = this->getLength();
x = mag*cos(radangle);
y = mag*sin(radangle);
}
+ 3
Welcome, but there is another problem in the code above. Its printing the angle as -89 I don't know why...
Edit:
In the getangle method,
vec2 ab stores ab.x = b.x - x, a negative value.
So change the function to:
int vec2::getAngle(vec2 const& axe) const {
vec2 b(0, 0);
vec2 ab(x-b.x, y-b.y);
vec2 cb(axe.x-b.x, axe.y-b.y);
float dot = (ab.x * cb.x + ab.y * cb.y);
float cross = (ab.x * cb.y - ab.y * cb.x);
float alpha = atan2(cross, dot);
return floor(alpha * 180. / pi + 0.5);
}
+ 3
I understood why there is a huge difference in getangle and setangle values...
The problem's cause - x and y are integers.
The setangle method may return float x and y (cos and sin usually give decimal values), but they are rounded off to the nearest integers, and these integers contribute to a different angle, as 2.7 != 3 and atan(2.7) != atan(3)...
So the values are different.
You must rewrite the class with x and y being doubles, instead of plain ints for everything to work properly...
+ 2
Yeah, even 20 is being displayed as 18.
Ill try debugging the problem...
+ 1
thank you bro !!
+ 1
Allright, Ill do it ! Thank again
+ 1
<3
0
Yes but Ive put radangle = pi * angle / -180. and its allright... Also when I set an angle like 9, and then I get the angle, I find 6.