Skip to content

Commit

Permalink
make vector2 rotation direction explicit, deprecate CW default (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmwsharp authored Aug 5, 2024
1 parent 2e6964a commit 27d6c18
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docs/docs/utilities/vector2.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ vec = vec.rotate90(); // much better
Alias for `normalize()`.
??? func "`#!cpp Vector2 Vector2::rotate(double theta)`"
??? func "`#!cpp Vector2 Vector2::rotateCW(double theta)`"
Rotate the vector by angle $\theta$ in the clockwise direction.
??? func "`#!cpp Vector2 Vector2::rotateCCW(double theta)`"
Rotate the vector by angle $\theta$ in the counter-clockwise direction.
Expand Down
8 changes: 5 additions & 3 deletions include/geometrycentral/utilities/vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ struct Vector2 {

Vector2 normalize() const;
Vector2 normalizeCutoff(double mag = 0.) const;
Vector2 unit() const; // alias for normalize
Vector2 rotate(double theta) const;
Vector2 rotate90() const;
Vector2 unit() const; // alias for normalize
Vector2 rotate(double theta) const; // DEPRECATED, WARNING: rotates clockwise
Vector2 rotateCW(double theta) const; // clockwise
Vector2 rotateCCW(double theta) const; // counter-clockwise
Vector2 rotate90() const; // counter-clockwise

// Complex functions
Vector2 pow(double p) const; // complex power
Expand Down
8 changes: 8 additions & 0 deletions include/geometrycentral/utilities/vector2.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ inline Vector2 unit(const Vector2& v) { return normalize(v); }
inline Vector2 normalizeCutoff(const Vector2& v, double mag) { return v.normalizeCutoff(mag); }

inline Vector2 Vector2::rotate(double theta) const {
return rotateCW(theta);
}

inline Vector2 Vector2::rotateCW(double theta) const {
double cosTh = std::cos(theta);
double sinTh = std::sin(theta);
return Vector2{cosTh * x + sinTh * y, -sinTh * x + cosTh * y};
}

inline Vector2 Vector2::rotateCCW(double theta) const {
return rotateCW(-theta);
}

inline Vector2 Vector2::rotate90() const { return Vector2{-y, x}; }

inline Vector2 Vector2::pow(double p) const {
Expand Down

0 comments on commit 27d6c18

Please sign in to comment.