Esempio n. 1
0
        private (float, Vector2) _AreaCentroid( )
        {
            Vector2 centroid = Vector2.Zero;
            float   area     = 0.0f;

            for (int i = 1; i < OuterPoints.Length - 1; i++)
            {
                int i0 = 0;
                int i1 = 1;
                int i2 = i + 1;

                Vector2 v0 = OuterPoints[i0];
                Vector2 v1 = OuterPoints[i1];
                Vector2 v2 = OuterPoints[i2];

                Vector2 cenTri  = (v0 + v1 + v2) / 3.0f;
                float   areaTri = 0.5f * FloatMath.Fabs(Vector2.Dot(v1 - v0, (v2 - v0).PerpLeft));

                area     += areaTri;
                centroid += cenTri * areaTri;
            }

            centroid /= area;
            return(area, centroid);
        }
Esempio n. 2
0
        public static FindLineLineResult Intersect(Line line0, Line line1)
        {
            FindLineLineResult lineLineResult = new FindLineLineResult();


            Vector2 diff        = line1.Origin - line0.Origin;
            float   D0DotPerpD1 = Vector2.Dot(line0.Direction.PerpLeft, line1.Direction);

            if (D0DotPerpD1 != 0.0f)
            {
                // The lines are not parallel.
                lineLineResult.Intersect        = true;
                lineLineResult.NumIntersections = 1;
                float invD0DotPerpD1 = 1.0f / D0DotPerpD1;
                float diffDotPerpD0  = Vector2.Dot(diff.PerpLeft, line0.Direction);
                float diffDotPerpD1  = Vector2.Dot(diff.PerpLeft, line1.Direction);
                float s0             = diffDotPerpD1 * invD0DotPerpD1;
                float s1             = diffDotPerpD0 * invD0DotPerpD1;
                lineLineResult.Line0Parameter = s0;
                lineLineResult.Line1Parameter = s1;
                lineLineResult.Point          = line0.Origin + s0 * line0.Direction;
            }
            else
            {
                // The lines are parallel.
                diff.Normalize();
                float diffNDotPerpD1 = Vector2.Dot(diff.PerpLeft, line1.Direction);
                if (FloatMath.Fabs(diffNDotPerpD1) != 0.0f)
                {
                    // The lines are parallel but distinct.
                    lineLineResult.Intersect        = false;
                    lineLineResult.NumIntersections = 0;
                }
                else
                {
                    // The lines are the same.
                    lineLineResult.Intersect        = true;
                    lineLineResult.NumIntersections = Int32.MaxValue;
                    float maxfloat = float.MaxValue;
                    lineLineResult.Line0Parameter = maxfloat;
                    lineLineResult.Line1Parameter = maxfloat;
                }
            }

            return(lineLineResult);
        }