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

21st Jun 2017, 12:04 PM
clement
clement - avatar
9 Respostas
+ 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); }
21st Jun 2017, 1:49 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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); }
21st Jun 2017, 3:29 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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...
21st Jun 2017, 3:54 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
21st Jun 2017, 4:01 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
Yeah, even 20 is being displayed as 18. Ill try debugging the problem...
21st Jun 2017, 3:45 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
thank you bro !!
21st Jun 2017, 3:28 PM
clement
clement - avatar
+ 1
Allright, Ill do it ! Thank again
21st Jun 2017, 3:58 PM
clement
clement - avatar
+ 1
<3
21st Jun 2017, 4:13 PM
clement
clement - avatar
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.
21st Jun 2017, 3:38 PM
clement
clement - avatar