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?
| 13 views

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

0 like 0 dislike