AbstractedSheep.ShuttleTrackerWorld.Shuttle.SnapToRoute C# (CSharp) Method

SnapToRoute() private method

private SnapToRoute ( ) : void
return void
        internal void SnapToRoute()
        {
            if (this.CurrentRoute != null && this.Location != null)
            {
                Coordinate c1, c2;
                Coordinate closestPoint = null, tempClosestPoint;
                int nextPointId = -1;
                double shortestDistance = 10000, tempShortestDistance = 10000;

                for (int i = 0; i < this.CurrentRoute.coordinates.Count; i++)
                {
                    if (i == 0)
                        c1 = this.CurrentRoute.coordinates.Last();
                    else
                        c1 = this.CurrentRoute.coordinates[i - 1];

                    c2 = this.CurrentRoute.coordinates[i];

                    tempClosestPoint = this.Location.ClosestPoint(c1, c2);
                    tempShortestDistance = tempClosestPoint.DistanceTo(this.Location);

                    if (tempShortestDistance < shortestDistance)
                    {
                        shortestDistance = tempShortestDistance;
                        closestPoint = tempClosestPoint;
                        nextPointId = i + 1 == this.CurrentRoute.coordinates.Count ? 0 : i + 1;
                    }
                }

                this.SnappedCoordinate = closestPoint;
                this.NextRouteCoordinate = nextPointId;
            }
        }

Usage Example

Example #1
0
        /// <summary>
        /// Adds a shuttle to the world or updates the position of an existing shuttle.
        /// </summary>
        /// <param name="shuttleId">The ID number of the shuttle.</param>
        /// <param name="location">The shuttle's current location.</param>
        /// <param name="name">The name of the shuttle.</param>
        /// <param name="bearing">The heading of the shuttle in degrees from north.</param>
        /// <param name="cardinalPoint">The heading of the shuttle as a cardinal direcation (e.g. Northwest).</param>
        /// <param name="speed">The speed of the shuttle in miles per hour.</param>
        /// <param name="route">The id of the shuttle route. -1 indicates that the shuttle is not on a route.</param>
        public void AddOrUpdateShuttle(int shuttleId, Coordinate location, string name, int bearing, string cardinalPoint, int speed, int route = -1)
        {
            Shuttle s = this.shuttles[shuttleId];
            Route   r = this.routes[route];

            if (s == null)
            {
                s                = new Shuttle();
                s.Id             = shuttleId;
                s.Location       = location;
                s.Name           = name;
                s.Bearing        = bearing;
                s.CardinalPoint  = cardinalPoint;
                s.Speed          = speed;
                s.LastUpdateTime = CurrentTimeMillis();

                this.shuttles.Add(s.Id, s);

                if (r != null)
                {
                    s.CurrentRoute = r;
                    r.shuttles.Add(s.Id, s);
                }

                s.SnapToRoute();
            }
            else
            {
                s.LastUpdateTime = CurrentTimeMillis();
                s.Location       = location;
                s.Speed          = speed;
                s.Bearing        = bearing;
                s.CardinalPoint  = cardinalPoint;
                s.Name           = name;

                if (r == null && s.CurrentRoute != null)
                {
                    s.CurrentRoute.shuttles.Remove(s.Id);
                    s.CurrentRoute = null;
                }
                else if (r != null && s.CurrentRoute != null && s.CurrentRoute != r)
                {
                    s.CurrentRoute.shuttles.Remove(s.Id);
                    s.CurrentRoute = r;
                    r.shuttles.Add(s.Id, s);
                }
                else if (r != null && s.CurrentRoute == null)
                {
                    s.CurrentRoute = r;
                    r.shuttles.Add(s.Id, s);
                }

                s.SnapToRoute();
            }
        }
All Usage Examples Of AbstractedSheep.ShuttleTrackerWorld.Shuttle::SnapToRoute