Exemple #1
0
        public static Vector3 CalculateFloorplanCenter(IVolume plan)
        {
            Vector2Int center = GetCentroid(plan);

            Vector2[] points = ConvertPoints(plan.AllPoints());
            Vector3   planUp = plan.baseHeight * Vector3.up;

            if (JMath.PointInsidePoly(center.vector2, points))
            {
                return(center.vector3XZ + planUp);
            }

            int pointCount = points.Length;

            if (pointCount == 0)
            {
                return(Vector3.zero);
            }
            float[] dists = new float[pointCount];
            float   dist  = Mathf.Infinity;
            int     index = -1;

            for (int p = 0; p < pointCount; p++)
            {
                float currentDist = Vector2.Distance(center.vector2, points[p]);
                if (currentDist < dist)
                {
                    index = p;
                    dist  = currentDist;
                }
                dists[p] = currentDist;
            }

            Vector2 pa     = points[index];
            int     indexA = (index - 1 + pointCount) % pointCount;
            int     indexB = (index + 1) % pointCount;

            Vector2 pb        = (dists[indexA] < dists[indexB]) ? points[indexA] : points[indexB];
            Vector2 pc        = Vector2.Lerp(pa, pb, 0.5f);
            float   cDist     = Vector2.Distance(center.vector2, pc);
            Vector2 dir       = (pc - center.vector2).normalized;
            Vector2 newCenter = pc + dir * cDist * 0.5f;

            if (JMath.PointInsidePoly(newCenter, points))
            {
                return(new Vector3(newCenter.x, planUp.y, newCenter.y));
            }

            return(center.vector3XZ + planUp);
        }