How to rotate a vector without error?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
13 views
There are the following elements:
Vector
a = {x: 1, y: 0}
The function of rotating coordinates
function rotate(angle) { a.x = a.x * Math.cos(angle) - a.y * Math.sin(angle); a.y = a.x * Math.sin(angle) + a.y * Math.cos(angle); }

That is called this way
rotate(1 * Math.PI / 180);

The problem is that with every turn of lost accuracy. This can be detected if we check the length of the vector.
Math.sqrt( Math.pow(a.x, 2) + Math.pow(a.y, 2) )
Initially, the length is equal to 1
after 10 calls 0.9999671067997294
100 - 0.9831222411821932
And so on. What if you increase the angle of rotation when the rotate call, the error increases.

Tell me, please, what is the reason and how to get rid of this error? Maybe you should use a different method to rotate the coordinates? What?
by | 13 views

1 Answer

0 like 0 dislike
To preserve accuracy, you will have to store the history of turns with preservation of the sign (clockwise + anticlockwise -) and for the calculation of the latter is to summarize the degree.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,186 answers
0 comments
35,406 users