Exemplo n.º 1
0
        /// <summary>
        /// Finds the closest available position to <see cref="pos"/> in <see cref="spaceUsage"/> on the x axis <see cref="searchAxis"/>
        /// </summary>
        /// <param name="spaceUsage">layout object</param>
        /// <param name="pos">position to look for</param>
        /// <param name="searchAxis">x axis to look for</param>
        /// <returns>closest position of pos in the searchAxis, null if cannot be found</returns>
        protected XyPosition?FindClosest(NodeViewModel[,] spaceUsage, XyPosition pos, int searchAxis)
        {
            int height = spaceUsage.GetLength(1);

            int?closestY = null;
            int distance = int.MaxValue;

            for (int i = 0; i < height; i++)
            {
                NodeViewModel model = spaceUsage[searchAxis, i];
                if (model != null)
                {
                    continue;
                }

                int testDistance = Math.Abs(pos.Y - i);
                if (testDistance < distance)
                {
                    distance = testDistance;
                    closestY = i;
                }
            }

            if (closestY != null)
            {
                return(new XyPosition(searchAxis, closestY.Value));
            }

            return(null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Finds the closest available position to <see cref="origin"/> in <see cref="spaceUsage"/> for <see cref="destination"/>
        /// </summary>
        /// <param name="spaceUsage">layout object</param>
        /// <param name="origin">node to start from</param>
        /// <param name="destination">target node placement</param>
        /// <returns>closest position of <see cref="destination"/> to <see cref="origin"/>, null if cannot be found</returns>
        protected XyPosition?FindClosest(NodeViewModel[,] spaceUsage, NodeViewModel origin, NodeViewModel destination)
        {
            XyPosition pos              = SpaceUsagePosition(spaceUsage, origin);
            int        originLevel      = m_nodeColumnLookup[origin];
            int        destinationLevel = m_nodeColumnLookup[destination];

            if (originLevel > destinationLevel)
            {
                return(FindClosest(spaceUsage, pos, pos.X - 1));
            }
            else if (originLevel < destinationLevel)
            {
                return(FindClosest(spaceUsage, pos, pos.X + 1));
            }

            return(null);
        }