//Given an edge and a list of points, find the point furthest away from the edge
        private static MyVector3 FindPointFurthestFromEdge(Edge3 edge, HashSet <MyVector3> pointsHashSet)
        {
            List <MyVector3> points = new List <MyVector3>(pointsHashSet);

            //Init with the first point
            MyVector3 pointFurthestAway = points[0];

            MyVector3 closestPointOnLine = _Geometry.GetClosestPointOnLine(edge, pointFurthestAway, withinSegment: false);

            float maxDistSqr = MyVector3.SqrDistance(pointFurthestAway, closestPointOnLine);

            //Try to find a better point
            for (int i = 1; i < points.Count; i++)
            {
                MyVector3 thisPoint = points[i];

                //TODO make sure that thisPoint is NOT colinear with the edge because then we wont be able to build a triangle

                closestPointOnLine = _Geometry.GetClosestPointOnLine(edge, thisPoint, withinSegment: false);

                float distSqr = MyVector3.SqrDistance(thisPoint, closestPointOnLine);

                if (distSqr > maxDistSqr)
                {
                    maxDistSqr = distSqr;

                    pointFurthestAway = thisPoint;
                }
            }


            return(pointFurthestAway);
        }
Example #2
0
        public float SqrLength()
        {
            //The edge points TO a vertex
            MyVector3 p2 = v.position;
            MyVector3 p1 = prevEdge.v.position;

            float length = MyVector3.SqrDistance(p1, p2);

            return(length);
        }
        //From a list of points, find the two points that are furthest away from each other
        private static Edge3 FindEdgeFurthestApart(HashSet <MyVector3> pointsHashSet)
        {
            List <MyVector3> points = new List <MyVector3>(pointsHashSet);


            //Instead of using all points, find the points on the AABB
            MyVector3 maxX = points[0]; //Cant use default because default doesnt exist and might be a min point
            MyVector3 minX = points[0];
            MyVector3 maxY = points[0];
            MyVector3 minY = points[0];
            MyVector3 maxZ = points[0];
            MyVector3 minZ = points[0];

            for (int i = 1; i < points.Count; i++)
            {
                MyVector3 p = points[i];

                if (p.x > maxX.x)
                {
                    maxX = p;
                }
                if (p.x < minX.x)
                {
                    minX = p;
                }

                if (p.y > maxY.y)
                {
                    maxY = p;
                }
                if (p.y < minY.y)
                {
                    minY = p;
                }

                if (p.z > maxZ.z)
                {
                    maxZ = p;
                }
                if (p.z < minZ.z)
                {
                    minZ = p;
                }
            }

            //Some of these might be the same point (like minZ and minY)
            //But we have earlier check that the points have a width greater than 0, so we should get the points we need
            HashSet <MyVector3> extremePointsHashSet = new HashSet <MyVector3>();

            extremePointsHashSet.Add(maxX);
            extremePointsHashSet.Add(minX);
            extremePointsHashSet.Add(maxY);
            extremePointsHashSet.Add(minY);
            extremePointsHashSet.Add(maxZ);
            extremePointsHashSet.Add(minZ);

            points = new List <MyVector3>(extremePointsHashSet);


            //Find all possible combinations of edges between all points
            List <Edge3> pointCombinations = new List <Edge3>();

            for (int i = 0; i < points.Count; i++)
            {
                MyVector3 p1 = points[i];

                for (int j = i + 1; j < points.Count; j++)
                {
                    MyVector3 p2 = points[j];

                    Edge3 e = new Edge3(p1, p2);

                    pointCombinations.Add(e);
                }
            }


            //Find the edge that is the furthest apart

            //Init by picking the first edge
            Edge3 eFurthestApart = pointCombinations[0];

            float maxDistanceBetween = MyVector3.SqrDistance(eFurthestApart.p1, eFurthestApart.p2);

            //Try to find a better edge
            for (int i = 1; i < pointCombinations.Count; i++)
            {
                Edge3 e = pointCombinations[i];

                float distanceBetween = MyVector3.SqrDistance(e.p1, e.p2);

                if (distanceBetween > maxDistanceBetween)
                {
                    maxDistanceBetween = distanceBetween;

                    eFurthestApart = e;
                }
            }

            return(eFurthestApart);
        }