Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Cначала определю классы
- Point
- {
- public double X;
- public double Y;
- }
- Node
- {
- public Point Point;
- public double PathLength;
- public List<Node> connections;
- }
- Теперь алгоритм:
- foreach(var node in allNodes)
- {
- var shape = FindCycleFromNode(node);
- }
- List<Node> FindCycleFromNode(Node targetNode)
- {
- List<Node> openList = new ...
- List<Node> closedList = new ...
- closedList.Add(targetNode);
- Node currentNode = targetNode;
- do
- {
- foreach(var connectedNode in currentNode.Connections)
- {
- if(closedList.Contains(connectedNode) continue;
- if(openList.Contains(connectedNode)
- {
- if(connectedNode.PathLength > currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point))
- {
- connectedNode.PathLength = currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point);
- }
- else if(Distance(connectedNode.Point, targetNode.Point) == connectedNode.PathLength)//Если значение ноды равно длине до целевой точки.
- {
- connectedNode.PathLength = currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point);
- }
- }
- else
- {
- connectedNode.PathLength = currentNode.PathLength + Distance(currentNode.Point, connectedNode.Point);
- openList.Add(connectedNode);
- }
- }
- closedList.Add(currentNode);
- openList.Remove(currentNode);
- var nextNode = FindNodeInOpenListWithMinLength(openList);
- currentNode = nextNode;
- }while(openList.Count > 0);
- Node pathNode = null;
- foreach(var node in targetNode.Connections)
- {
- if(node.PathLength > Disntance(node.Point, targetNode.Point))
- {
- if(pathNode == null) pathNode = node;
- else if(pathNode.Length > node.PathLength) pathNode = node;
- }
- }
- List<Node> path = new ...
- path.Add(pathNode);
- while(pathNode != null)
- {
- var nextPathNode = FindMinNode(pathNode.Connections);
- if(nextPathNode == targetNode)
- {
- path.Add(nextPathNode);
- pathNode = null;
- }
- else
- {
- path.Add(nextPathNode);
- pathNode = nextPathNode;
- }
- }
- return path;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement