Box2DX.Collision.PolygonShape.ComputeCentroid C# (CSharp) Метод

ComputeCentroid() публичный статический Метод

public static ComputeCentroid ( Vec2 vs, int count ) : Vec2
vs Box2DX.Common.Vec2
count int
Результат Box2DX.Common.Vec2
		public static Vec2 ComputeCentroid(Vec2[] vs, int count)
		{
			Box2DXDebug.Assert(count >= 3);

			Vec2 c = new Vec2(); c.Set(0.0f, 0.0f);
			float area = 0.0f;

			// pRef is the reference point for forming triangles.
			// It's location doesn't change the result (except for rounding error).
			Vec2 pRef = new Vec2(0.0f, 0.0f);
#if O
			// This code would put the reference point inside the polygon.
			for (int i = 0; i < count; ++i)
			{
				pRef += vs[i];
			}
			pRef *= 1.0f / count;
#endif

			float inv3 = 1.0f / 3.0f;

			for (int i = 0; i < count; ++i)
			{
				// Triangle vertices.
				Vec2 p1 = pRef;
				Vec2 p2 = vs[i];
				Vec2 p3 = i + 1 < count ? vs[i + 1] : vs[0];

				Vec2 e1 = p2 - p1;
				Vec2 e2 = p3 - p1;

				float D = Vec2.Cross(e1, e2);

				float triangleArea = 0.5f * D;
				area += triangleArea;

				// Area weighted centroid
				c += triangleArea * inv3 * (p1 + p2 + p3);
			}

			// Centroid
			Box2DXDebug.Assert(area > Common.Settings.FLT_EPSILON);
			c *= 1.0f / area;
			return c;
		}

Usage Example

Пример #1
0
        internal PolygonShape(ShapeDef def) : base(def)
        {
            Box2DXDebug.Assert(def.Type == ShapeType.PolygonShape);
            this._type = ShapeType.PolygonShape;
            PolygonDef polygonDef = (PolygonDef)def;

            this._vertexCount = polygonDef.VertexCount;
            Box2DXDebug.Assert(3 <= this._vertexCount && this._vertexCount <= Settings.MaxPolygonVertices);
            for (int i = 0; i < this._vertexCount; i++)
            {
                this._vertices[i] = polygonDef.Vertices[i];
            }
            for (int i = 0; i < this._vertexCount; i++)
            {
                int  num  = i;
                int  num2 = (i + 1 < this._vertexCount) ? (i + 1) : 0;
                Vec2 a    = this._vertices[num2] - this._vertices[num];
                Box2DXDebug.Assert(a.LengthSquared() > Settings.FLT_EPSILON * Settings.FLT_EPSILON);
                this._normals[i] = Vec2.Cross(a, 1f);
                this._normals[i].Normalize();
            }
            for (int i = 0; i < this._vertexCount; i++)
            {
                for (int j = 0; j < this._vertexCount; j++)
                {
                    if (j != i && j != (i + 1) % this._vertexCount)
                    {
                        float num3 = Vec2.Dot(this._normals[i], this._vertices[j] - this._vertices[i]);
                        Box2DXDebug.Assert(num3 < -Settings.LinearSlop);
                    }
                }
            }
            for (int i = 1; i < this._vertexCount; i++)
            {
                float num4 = Vec2.Cross(this._normals[i - 1], this._normals[i]);
                num4 = Box2DX.Common.Math.Clamp(num4, -1f, 1f);
                float num5 = (float)System.Math.Asin((double)num4);
                Box2DXDebug.Assert(num5 > Settings.AngularSlop);
            }
            this._centroid = PolygonShape.ComputeCentroid(polygonDef.Vertices, polygonDef.VertexCount);
            PolygonShape.ComputeOBB(out this._obb, this._vertices, this._vertexCount);
            for (int i = 0; i < this._vertexCount; i++)
            {
                int  num  = (i - 1 >= 0) ? (i - 1) : (this._vertexCount - 1);
                int  num2 = i;
                Vec2 a2   = this._normals[num];
                Vec2 a3   = this._normals[num2];
                Vec2 b    = this._vertices[i] - this._centroid;
                Vec2 b2   = default(Vec2);
                b2.X = Vec2.Dot(a2, b) - Settings.ToiSlop;
                b2.Y = Vec2.Dot(a3, b) - Settings.ToiSlop;
                Box2DXDebug.Assert(b2.X >= 0f);
                Box2DXDebug.Assert(b2.Y >= 0f);
                Mat22 mat = default(Mat22);
                mat.Col1.X            = a2.X;
                mat.Col2.X            = a2.Y;
                mat.Col1.Y            = a3.X;
                mat.Col2.Y            = a3.Y;
                this._coreVertices[i] = mat.Solve(b2) + this._centroid;
            }
        }