开发者

Modifying The Angle of a Unit Vector

I have an angle in the form of a unit vector. I need to be able to change this angle. Say the angle of the vector V = (x,y) is currently A. How would I go 开发者_Python百科about changing the angle to A+0.2 (that is just an example, it could be any value I'm adding) without converting the vector to an angle, modifying it, then calculating the appropriate vector again?


Using the following formulae to rotate your vector counter-clockwise:

Modifying The Angle of a Unit Vector

therefore to rotate the vector V = (x, y) pi an angle A you would do the following:

V' = (x * cos(A) - y * sin(A), x * sin(A) + y * cos(A))

or with code:

vec2 rotateVec2(const vec2 &vec, float angle)
{
    float newX = vec.x * cos(angle) - vec.y * sin(angle);
    float newY = vec.y * sin(angle) + vec.y * cos(angle);
    return vec2(newX, newY);
}


x2 = x * cos(angle) - y * sin(angle);
y2 = y * cos(angle) + x * sin(angle);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜