Reputation: 237
I am using a class to represent a rotation angle...
class RotationAngle
{
protected:
float cosval;
float sinval;
__forceinline RotationAngle(float c, float s) { cosval = c; sinval = s; }
public:
__forceinline RotationAngle(float radians) { Set(radians); }
__forceinline RotationAngle(const RotationAngle& r) { Set(r); }
__forceinline void Set(float radians) { cosval = cos(radians); sinval = sin(radians); }
__forceinline void Set(const RotationAngle& r) { cosval = r.cosval; sinval = r.sinval; }
__forceinline float Cos() const { return cosval; }
__forceinline float Sin() const { return sinval; }
__forceinline RotationAngle& operator = (float radians) { Set(radians); return *this; }
__forceinline int operator == (const RotationAngle& r) const { return (r.cosval == cosval) && (r.sinval == sinval); }
__forceinline int operator != (const RotationAngle& r) const { return (r.cosval != cosval) || (r.sinval != sinval); }
};
I use this so I can add an angle to a point or line using an operator, and have the sine and cosine pre-calculated. It would be redundant to recalculate the sine and cosine of the angles every time the point was rotated by the same angle.
class Point
{
public:
float x;
float y;
__forceinline void Set(float sx, float sy) { x = sx; y = sy; }
__forceinline void Rotate(const RotationAngle& a) { Set(x * a.Cos() - y * a.Sin(), x * a.Sin() + y * a.Cos()); }
__forceinline void operator += (const RotationAngle& a) { Rotate(a); }
};
I would like to include a -= operator to use the negative representation of a RotationAngle (ie counterclockwise rotation). Will simply using -cos and -sin work? I'm not sure considering the formula for adding sin/cos is not as straightforward. Is there a quick way to accomplish this?
Upvotes: 0
Views: 226
Reputation: 254431
Negating an angle will negate its sine, but leave its cosine unchanged. So it might make sense to define a unary negation:
RotationAngle operator-() const {return RotationAngle(cosval, -sinval);}
and then define -=
in terms of that
void operator -= (const RotationAngle& a) { Rotate(-a); }
Upvotes: 1