public List <Vector3> SolvePath(Vector3 origin, List <Vector3> nodes, Network.Cost cost)
        {
            // Initalize the line renderer nodes
            List <Vector3> lineNodes = new List <Vector3>();

            // The node we are currently at
            Vector3 currentPos = origin;

            int iterations = 0;

            // Wait until we have gone through every node
            for (int x = 0; x < nodes.Count; x++)
            {
                // Find the closest node that we have NOT yet visited
                float minDist = float.MaxValue;
                int   index   = 0;
                for (int i = 0; i < nodes.Count; i++)
                {
                    iterations++;

                    if (lineNodes.Contains(nodes[i]))
                    {
                        continue;
                    }

                    // Calculate the distance from our current node to this potential node
                    float dist = Vector3.Distance(nodes[i], currentPos);

                    // If it's a minimum so far, save the distance and the index
                    if (dist < minDist)
                    {
                        minDist = dist;
                        index   = i;
                    }
                }

                // Save the new node
                currentPos = nodes[index];

                // Save this node in our final path
                lineNodes.Add(currentPos);
            }

            Debug.Log($"Iterations: {iterations}");

            return(lineNodes);
        }
예제 #2
0
        public List <Vector3> SolvePath(Vector3 origin, List <Vector3> nodes, Network.Cost cost)
        {
            // Find the average position of all nodes
            Vector3 averagePosition = origin;

            for (int i = 0; i < nodes.Count; i++)
            {
                averagePosition += nodes[i];
            }

            averagePosition /= (nodes.Count + 1);

            // Calculate the angle between the points and the origin about the average center
            List <(float, Vector3)> pointAngles = new List <(float, Vector3)>();
            Vector3 from = origin - averagePosition;

            for (int i = 0; i < nodes.Count; i++)
            {
                Vector3 to = nodes[i] - averagePosition;

                float angle = Vector2.SignedAngle(new Vector2(from.x, from.z), new Vector2(to.x, to.z));
                angle = Mathf.Repeat(angle, 360f);

                pointAngles.Add((angle, nodes[i]));
예제 #3
0
        public List <Vector3> SolvePath(Vector3 origin, List <Vector3> nodes, Network.Cost cost)
        {
            // Check cost is valid
            if (cost != Network.Cost.Distance)
            {
                cost = Network.Cost.Distance;
                Debug.LogError($"Cost type: {cost} is invalid for Brute Force Algorithm.");
            }

            // Init
            List <Path> paths = new List <Path>();

            // Recursively go through all nodes and calculate a path
            for (int i = 0; i < nodes.Count; i++)
            {
                Path path = new Path();
                path.path.Add(nodes[i]);

                TryPath(path);
            }

            void TryPath(Path currentPath)
            {
                int remaining = nodes.Count - currentPath.path.Count;

                // Check if there are no more nodes
                if (remaining == 0)
                {
                    // Save it
                    paths.Add(currentPath);

                    // We are finished
                    return;
                }

                for (int i = 0; i < nodes.Count; i++)
                {
                    // Go through all nodes we haven't been to yet
                    if (!currentPath.path.Contains(nodes[i]))
                    {
                        // Make a new path
                        Path path = new Path();

                        // Add the previous paths positions
                        for (int x = 0; x < currentPath.path.Count; x++)
                        {
                            path.path.Add(currentPath.path[x]);
                        }

                        // Add a new node
                        path.path.Add(nodes[i]);

                        TryPath(path);
                    }
                }
            }

            Debug.Log($"Iterations: {paths.Count}");

            // Find the shortest path out of our many choices
            float minDist = float.MaxValue;
            int   index   = 0;

            for (int i = 0; i < paths.Count; i++)
            {
                float dist = paths[i].GetDistance(origin);
                if (dist < minDist)
                {
                    minDist = dist;
                    index   = i;
                }
            }

            return(paths[index].path);
        }