Exemple #1
0
        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);
        }
Exemple #2
0
        /// <summary>
        /// Tries to to merge neighbor lines.
        /// </summary>
        public void Optimize()
        {
            var optimizedLines        = new List <LineSegment>();
            var optimizedSwitchPoints = new SerializableDictionary <Vector2, SwitchPoint>();

            Nodes.Clear();
            var     currentDirection = Direction.None;
            Vector2?start            = null;
            Vector2?end = null;

            for (var i = 0; i < Lines.Count; i++)
            {
                var directedLine = GetLine(Lines[i].Start, Lines[i].End, true);
                if (end != null && !HasConnection(end.Value, directedLine.Start))
                {
                    currentDirection = Direction.None;
                }

                var isLast                = i == Lines.Count - 1;
                var isSwitchPoint         = start != null && SwitchPoints.ContainsKey(start.Value);
                var isSwitchPointInvented = end != null && SwitchPoints.ContainsKey(end.Value);

                if (directedLine.Direction != currentDirection || isSwitchPointInvented)
                {
                    if (start != null)
                    {
                        AddLine(start.Value, end.Value, optimizedLines);
                        if (isSwitchPoint)
                        {
                            AddSwitchPoint(start.Value, end.Value, optimizedSwitchPoints);
                        }
                        if (isSwitchPointInvented)
                        {
                            AddSwitchPoint(end.Value, start.Value, optimizedSwitchPoints);
                        }
                    }
                    currentDirection = directedLine.Direction;
                    start            = directedLine.Start;
                }

                if (isLast && start != null && end != null) //ToDo: not sure if these SwitchPoints here are correct. Create a simple figure to test it.
                {
                    AddLine(start.Value, directedLine.End, optimizedLines);
                    if (isSwitchPoint)
                    {
                        AddSwitchPoint(start.Value, directedLine.End, optimizedSwitchPoints);
                    }
                    if (isSwitchPointInvented)
                    {
                        AddSwitchPoint(directedLine.End, start.Value, optimizedSwitchPoints);
                    }
                    if (SwitchPoints.ContainsKey(directedLine.Start))
                    {
                        AddSwitchPoint(directedLine.Start, end.Value, optimizedSwitchPoints);
                    }
                    if (SwitchPoints.ContainsKey(directedLine.End))
                    {
                        AddSwitchPoint(directedLine.End, start.Value, optimizedSwitchPoints);
                    }
                    //if (SwitchPoints.ContainsKey(directedLine.Start))
                    //    AddSwitchPoint(directedLine.Start, directedLine.End, optimizedSwitchPoints);
                    //if (SwitchPoints.ContainsKey(directedLine.End))
                    //    AddSwitchPoint(directedLine.End, directedLine.Start, optimizedSwitchPoints);
                }
                end = directedLine.End;
            }
            Lines        = optimizedLines;
            SwitchPoints = optimizedSwitchPoints;
        }