Permalink
Browse files

Merge pull request #1 from Cloudef/kmVec2Rotate

Add kmVec2RotateBy function.
  • Loading branch information...
2 parents b27ee3c + 90b077e commit 05c53ad72e5c0c6acbde7338fd8da4d86a8a1d04 @Kazade committed Apr 14, 2012
Showing with 26 additions and 0 deletions.
  1. +25 −0 kazmath/vec2.c
  2. +1 −0 kazmath/vec2.h
View
@@ -131,3 +131,28 @@ kmVec2* kmVec2Assign(kmVec2* pOut, const kmVec2* pIn) {
return pOut;
}
+
+/**
+ * Rotates the point anticlockwise around a center
+ * by an amount of degrees.
+ *
+ * Code ported from Irrlicht: http://irrlicht.sourceforge.net/
+ */
+kmVec2* kmVec2RotateBy(kmVec2* pOut, kmVec2* pIn,
+ const kmScalar degrees, const kmVec2* center)
+{
+ kmScalar x, y;
+ const kmScalar radians = kmDegreesToRadians(degrees);
+ const kmScalar cs = cos(radians), sn = sin(radians);
+
+ pOut->x = pIn->x - center->x;
+ pOut->y = pIn->y - center->y;
+
+ x = pOut->x * cs - pOut->y * sn;
+ y = pOut->x * sn + pOut->y * cs;
+
+ pOut->x = x + center->x;
+ pOut->y = y + center->y;
+
+ return pOut;
+}
View
@@ -57,6 +57,7 @@ kmVec2* kmVec2TransformCoord(kmVec2* pOut, const kmVec2* pV, const struct kmMat3
kmVec2* kmVec2Scale(kmVec2* pOut, const kmVec2* pIn, const kmScalar s); ///< Scales a vector to length s
int kmVec2AreEqual(const kmVec2* p1, const kmVec2* p2); ///< Returns 1 if both vectors are equal
kmVec2* kmVec2Assign(kmVec2* pOut, const kmVec2* pIn);
+kmVec2* kmVec2RotateBy(kmVec2* pOut, kmVec2* pIn, const kmScalar degrees, const kmVec2* center); ///<Rotates the point anticlockwise around a center by an amount of degrees.
#ifdef __cplusplus
}

0 comments on commit 05c53ad

Please sign in to comment.