protected List<LineSegment> WalkFigure(LineNodeDictionary nodes) { var lines = new List<LineSegment>(); var visitedItems = new List<Vector2>(); var nodesToVisit = new Stack<LineSegment>(); var origin = nodes.Keys.First(); var lineSegment = new LineSegment(origin, origin); nodesToVisit.Push(lineSegment); while (nodesToVisit.Count > 0) { var currentItem = nodesToVisit.Pop(); var connectedNodes = GetConnectedNodes(currentItem.End, nodes); if (connectedNodes.Count == 1) //only the item I come from ForlornStartNodes.Add(currentItem.End); if (connectedNodes.Count > 2 && !SwitchPoints.ContainsKey(currentItem.End)) SwitchPoints.Add(currentItem.End, new SwitchPoint(new List<Vector2>(connectedNodes))); connectedNodes.Remove(currentItem.Start); foreach (var connectedNode in connectedNodes) { if (visitedItems.Contains(connectedNode)) continue; lineSegment = AddLine(currentItem.End, connectedNode, lines); nodesToVisit.Push(lineSegment); } visitedItems.Add(currentItem.End); } if (GetConnectedNodes(origin, nodes).Count <= 2) SwitchPoints.Remove(origin); //remove start point, it's not really a switch point return lines; }
protected void AddToNodes(Vector2 key, LineSegment line) { List<LineSegment> segments; if (Nodes.TryGetValue(key, out segments)) { segments.Add(line); } else { segments = new List<LineSegment> { line }; Nodes.Add(key, segments); } }
private void GetNeighborBlocks(LineSegment line, IEnumerable<Block> blocks, out Block blockX, out Block blockY, out Orientation orientation) { blockX = null; blockY = null; orientation = Orientation.Other; foreach (var block in blocks) { var found = false; var blockLines = Polygon.GetBlockLines(block, Layer).Where(item => item.IsSlope); foreach (var lineObstacle in blockLines) { if (line.Start == lineObstacle.Start && line.End == lineObstacle.End) found = true; else if (line.End == lineObstacle.Start && line.Start == lineObstacle.End) found = true; if (found) break; } if (!found) continue; if (blockX == null) blockX = block; else { blockY = block; //ok, we have both blocks var isVertical = line.Direction == Direction.Down || line.Direction == Direction.Up; var isHorizontal = line.Direction == Direction.Left || line.Direction == Direction.Right; if (isVertical) { orientation = Orientation.Vertical; if (blockX.Position.X < blockY.Position.X) return; } if (isHorizontal) { orientation = Orientation.Horizontal; if (blockX.Position.Y < blockY.Position.Y) return; } var blockTemp = blockX; blockX = blockY; blockY = blockTemp; return; } } }
private static void AddBlockLines(IDictionary<Block, List<LineSegment>> blockLines, Block block, LineSegment intersectionLine) { if (block == null) return; List<LineSegment> blockLineSegments; if (blockLines.TryGetValue(block, out blockLineSegments)) { blockLineSegments.Add(intersectionLine); } else { blockLineSegments = new List<LineSegment> {intersectionLine}; blockLines.Add(block, blockLineSegments); } }
//Line stuff protected virtual LineSegment AddLine(Vector2 start, Vector2 end, ICollection<LineSegment> list) { var line = new LineSegment(start, end); list.Add(line); return line; }
protected static int GetLinePriority(Direction baseDirection, LineSegment line) { var priority = _baseDirectionPriority[line.Direction]; if (baseDirection == Direction.None) baseDirection = Direction.Down; priority += 4 - _baseDirectionPriority[baseDirection]; if (priority < 0) priority = 8 + priority; if (priority > 8) priority = priority - 8; return priority; }