Example #1
0
        /// <summary>
        /// Find the node in the path that is closest to the mouse,
        /// </summary>
        /// <param name="drawArea">Area that is being drawn upon and where we have a mouse location</param>
        /// <param name="drawnPathData">The data structure with the information on the drawn path</param>
        void FindActiveNode(DrawArea drawArea, DrawnPathData drawnPathData)
        {
            // Initial simplest implementation: find simply the closest and first.
            float         closestMouseDistanceSquared = float.MaxValue;
            TrainpathNode closestNode = null;

            foreach (TrainpathNode node in drawnPathData.DrawnNodes)
            {
                float distanceSquared = CloseToMouse.GetGroundDistanceSquared(node.Location, drawArea.MouseLocation);
                // by using '<=' instead of '<' we should get the latest one, which overrides earlier ones
                if (distanceSquared <= closestMouseDistanceSquared)
                {
                    closestMouseDistanceSquared = distanceSquared;
                    closestNode = node;
                }
            }

            activeNode = closestNode;
        }
Example #2
0
        /// <summary>
        /// Find the node in the path that is closest to the mouse,
        /// </summary>
        /// <param name="drawArea">Area that is being drawn upon and where we have a mouse location</param>
        /// <param name="drawnPathData">The data structure with the information on the drawn path</param>
        void FindActiveNode(DrawArea drawArea, DrawnPathData drawnPathData)
        {
            // Initial simplest implementation: find simply the closest and first.
            float         closestMouseDistanceSquared = float.MaxValue;
            TrainpathNode closestNode = null;

            foreach (TrainpathNode node in drawnPathData.DrawnNodes)
            {
                float distanceSquared = CloseToMouse.GetGroundDistanceSquared(node.Location, drawArea.MouseLocation);
                // by using '<=' instead of '<' we should get the latest one, which overrides earlier ones
                // To prevent numerical issues, we add a small number (smaller than two junctions would normally be together
                if (distanceSquared <= closestMouseDistanceSquared + 0.1f)
                {
                    closestMouseDistanceSquared = distanceSquared;
                    closestNode = node;
                }
            }

            activeNode = closestNode;
        }