removed VectorTools namespace
[k8-jellyphysics.git] / src / jelly / ClosedShape.cpp
blob1ac686ce7bf979c44b586d8369f70443546702c8
1 /*
2 * Copyright (c) 2007 Walaber
3 * Modified by Ketmar // Invisible Vector
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
23 #include "ClosedShape.h"
25 #include "VectorTools.h"
28 namespace JellyPhysics {
30 void ClosedShape::begin () {
31 mLocalVertices.clear();
35 int ClosedShape::addVertex (const Vector2 &vec) {
36 mLocalVertices.push_back(vec);
37 return mLocalVertices.size();
41 void ClosedShape::finish (bool recenter) {
42 if (recenter) {
43 // find the average location of all of the vertices, this is our geometrical center
44 Vector2 center = Vector2::Zero;
45 unsigned int c = mLocalVertices.size();
46 for (unsigned int i = 0; i < c; ++i) center += mLocalVertices[i];
47 center /= (float)c;
48 // now subtract this from each element, to get proper "local" coordinates
49 for (unsigned int i = 0; i < c; ++i) mLocalVertices[i] -= center;
54 Vector2List ClosedShape::transformVertices (const Vector2 &worldPos, float angleInRadians, const Vector2 &scale) const {
55 Vector2List ret = mLocalVertices;
56 Vector2 v;
57 unsigned int c = ret.size();
58 for (unsigned int i = 0; i < c; ++i) {
59 // rotate the point, and then translate
60 v = ret[i]*scale;
61 v = rotateVector(v, angleInRadians);
62 v += worldPos;
63 ret[i] = v;
65 return ret;
69 void ClosedShape::transformVertices (const Vector2 &worldPos, float angleInRadians, const Vector2 &scale, Vector2List &outList) const {
70 float c = cosf(angleInRadians);
71 float s = sinf(angleInRadians);
72 Vector2List::iterator out = outList.begin();
73 for (Vector2List::const_iterator it = mLocalVertices.begin(); it != mLocalVertices.end(); ++it, ++out) {
74 // rotate the point, and then translate
75 Vector2 v =(*it)*scale;
76 v = rotateVector(v, c, s);
77 v += worldPos;
78 (*out) = v;