public static int DetermineManhattanDistance(string firstWire, string secondWire)
        {
            var firstWireCommands  = firstWire.Split(',').ToList();
            var secondWireCommands = secondWire.Split(',').ToList();

            var firstWireVectors  = BuildVectorList(firstWireCommands);
            var secondWireVectors = BuildVectorList(secondWireCommands);

            // Determine where the points are equal (Overlapping wires)
            var intersections = firstWireVectors.Intersect(secondWireVectors).ToList();

            intersections.Remove(new LocationVector {
                X = 0, Y = 0
            });

            var lowestDistance = 0;
            var hub            = new LocationVector {
                X = 0, Y = 0
            };

            foreach (var intersection in intersections)
            {
                var distance = DetermineDistance(hub, intersection);
                if (lowestDistance == 0 || distance < lowestDistance)
                {
                    lowestDistance = distance;
                }
            }

            return(lowestDistance);
        }
        public LuaVararg VectorToSegmentLua(LuaTable zonePoint, LuaTable firstLineZonePoint, LuaTable secondLineZonePoint)
        {
            List <LuaValue> ret = new List <LuaValue>(2);

            if (zonePoint == null || firstLineZonePoint == null || secondLineZonePoint == null)
            {
                ret.Add(LuaNil.Instance);
                ret.Add(LuaNil.Instance);

                return(new LuaVararg(ret, true));
            }

            // Gets the data model entities.
            ZonePoint zonePointEntity           = _dataFactory.GetWherigoObject <ZonePoint>(zonePoint);
            ZonePoint firstLinezonePointEntity  = _dataFactory.GetWherigoObject <ZonePoint>(firstLineZonePoint);
            ZonePoint secondLinezonePointEntity = _dataFactory.GetWherigoObject <ZonePoint>(secondLineZonePoint);

            // Performs the computation.
            LocationVector lv = _mathHelper.VectorToSegment(
                _dataFactory.GetWherigoObject <ZonePoint>(zonePoint),
                _dataFactory.GetWherigoObject <ZonePoint>(firstLineZonePoint),
                _dataFactory.GetWherigoObject <ZonePoint>(secondLineZonePoint)
                );

            // Prepares the lua return.
            ret.Add(_dataFactory.GetNativeContainer(lv.Distance));
            ret.Add(lv.Bearing.GetValueOrDefault());
            return(new LuaVararg(ret, true));
        }
        public ZonePoint TranslatePoint(ZonePoint point, LocationVector vector)
        {
            // Computes the input.
            CalcInput c = new CalcInput();

            c.MainPoint    = new Point(point);
            c.TargetVector = new Vector(vector);

            // Performs the computation.
            Point newPoint = TranslatePointCore(c);

            // Returns the right object.
            return(newPoint.ToZonePoint(_dataFactory));
        }
        private void OnWherigoObjectChanged(WF.Player.Core.Thing wherigoObject)
        {
            // Updates the vector.
            LocationVector vector = null;

            if (wherigoObject != null)
            {
                vector = wherigoObject.VectorFromPlayer;
            }

            bool vectorChanged = Vector != vector;

            Vector = vector;
            if (!vectorChanged)
            {
                RefreshFromVector(vector);
            }
        }
        private static List <LocationVector> FillInLocations(LocationVector startingVector, LocationVector targetVector, Command command)
        {
            // Collection of vectors that are the fillers / wire
            var vectors = new List <LocationVector>();

            if (command == Command.Left)
            {
                for (int i = startingVector.X - 1; i > targetVector.X; i--)
                {
                    vectors.Add(new LocationVector {
                        X = i, Y = startingVector.Y
                    });
                }
            }
            else if (command == Command.Down)
            {
                for (int i = startingVector.Y - 1; i > targetVector.Y; i--)
                {
                    vectors.Add(new LocationVector {
                        X = startingVector.X, Y = i
                    });
                }
            }
            else if (command == Command.Up)
            {
                for (int i = startingVector.Y + 1; i < targetVector.Y; i++)
                {
                    vectors.Add(new LocationVector {
                        X = startingVector.X, Y = i
                    });
                }
            }
            else if (command == Command.Right)
            {
                for (int i = startingVector.X + 1; i < targetVector.X; i++)
                {
                    vectors.Add(new LocationVector {
                        X = i, Y = startingVector.Y
                    });
                }
            }

            return(vectors);
        }
        public LuaVararg VectorToPointLua(LuaTable zonePoint1, LuaTable zonePoint2)
        {
            List <LuaValue> ret = new List <LuaValue>(2);

            // Gets the data model entities.
            ZonePoint zonePoint1Entity = _dataFactory.GetWherigoObject <ZonePoint>(zonePoint1);
            ZonePoint zonePoint2Entity = _dataFactory.GetWherigoObject <ZonePoint>(zonePoint2);

            // Performs the computation.
            LocationVector lv = _mathHelper.VectorToPoint(
                _dataFactory.GetWherigoObject <ZonePoint>(zonePoint1),
                _dataFactory.GetWherigoObject <ZonePoint>(zonePoint2)
                );

            // Prepares the lua return.
            ret.Add(_dataFactory.GetNativeContainer(lv.Distance));
            ret.Add(lv.Bearing.GetValueOrDefault());
            return(new LuaVararg(ret, true));
        }
 private void RefreshFromVector(LocationVector v)
 {
     if (v == null)
     {
         // No distance to show.
         bool distanceChanged = Distance != null;
         Distance = null;
         RefreshBearing(bearingFromNorth: 0);
         if (!distanceChanged)
         {
             RefreshFromDistance(null);
         }
     }
     else
     {
         // A distance to show!
         Distance = v.Distance;
         RefreshBearing(bearingFromNorth: v.Bearing);
     }
 }
        private static LocationVector CalculateLocation(LocationVector location, string commandVector, Command command)
        {
            var action = commandVector.Substring(1, commandVector.Length - 1);

            return(command switch
            {
                Command.Right => new LocationVector {
                    X = location.X + Int32.Parse(action), Y = location.Y
                },
                Command.Left => new LocationVector {
                    X = location.X - Int32.Parse(action), Y = location.Y
                },
                Command.Up => new LocationVector {
                    X = location.X, Y = location.Y + Int32.Parse(action)
                },
                Command.Down => new LocationVector {
                    X = location.X, Y = location.Y - Int32.Parse(action)
                },
                _ => throw new NotSupportedException()
            });
        private static List <LocationVector> BuildVectorList(List <string> commandVectors)
        {
            var vectorList      = new List <LocationVector>();
            var currentLocation = new LocationVector {
                X = 0, Y = 0
            };

            vectorList.Add(currentLocation);

            foreach (var item in commandVectors)
            {
                var command         = DetermineCommand(item[0]);
                var updatedLocation = CalculateLocation(currentLocation, item, command);

                vectorList.AddRange(FillInLocations(currentLocation, updatedLocation, command));
                vectorList.Add(updatedLocation);
                currentLocation = updatedLocation;
            }

            return(vectorList);
        }
        public LuaVararg VectorToZoneLua(LuaTable zonePoint, LuaTable zone)
        {
            List <LuaValue> ret = new List <LuaValue>(2);

            if (zonePoint == null || zone == null)
            {
                throw new ArgumentNullException();
            }

            // Gets the data model entities.
            ZonePoint zonePointEntity = _dataFactory.GetWherigoObject <ZonePoint>(zonePoint);
            Zone      zoneEntity      = _dataFactory.GetWherigoObject <Zone>(zone);

            // Performs the computation.
            LocationVector vector = _mathHelper.VectorToZone(zonePointEntity, zoneEntity);

            // Prepares the lua return.
            ret.Add(vector.Distance != null ? (LuaValue)_dataFactory.GetNativeContainer(vector.Distance) : LuaNil.Instance);
            ret.Add(vector.Bearing);

            return(new LuaVararg(ret, true));
        }
Beispiel #11
0
			public Vector(LocationVector vector)
			{
				Distance = vector.Distance == null ? new Nullable<double>() : vector.Distance.Value;
				Bearing = vector.Bearing;
			}
Beispiel #12
0
		public ZonePoint TranslatePoint(ZonePoint point, LocationVector vector)
		{
			// Computes the input.
			CalcInput c = new CalcInput();
			c.MainPoint = new Point(point);
			c.TargetVector = new Vector(vector);

			// Performs the computation.
			Point newPoint = TranslatePointCore(c);

			// Returns the right object.
			return newPoint.ToZonePoint(_dataFactory);
		}
 public LocationVector Rotate(LocationVector direction)
 {
 }
 private static int DetermineDistance(LocationVector firstVector, LocationVector secondVector)
 {
     return(Math.Abs(secondVector.X - firstVector.X) + Math.Abs(secondVector.Y - firstVector.Y));
 }
 public void MoveForward(LocationVector vectorLocation)
 {
 }
        internal IReadOnlyList <IReadOnlyList <ILocation> > CalculateRingLocations(double frequency, int stepsInCircles)
        {
            // How many degrees is a meter?
            // $lat_meter = 1 / ( CIRCUMFERENCE_OF_EARTH / 360 );
            var latitudeMeterPerDegree = 1.0 / (LocationExtensions.CircumferenceOfEarth / 360.0);

            // $lon_meter = (1 / cos(deg2rad($from['lat']))) * $lat_meter;
            var longitudeMeterPerDegree = (1.0 / Math.Cos(directionVector.From.Latitude.ToRadian())) * latitudeMeterPerDegree;

            // $distance = distance($from, $to);
            // $bearing = bearing($from, $to);
            // $wavelen = SPEED_OF_LIGHT / $freq;  // Speed of light
            var wavelength = LocationExtensions.SpeedOfLight / frequency; // $wavelen

            // $steps_in_path is an array of values between 0 (at $from) and 1 (at $to)
            // These are the distances where new polygons are started to show elipse

            // First we do that at some fixed fractions of path
            // $steps_in_path = array(0,0.25,0.4);
            var stepsInPath = new List <double> {
                0, 0.25, 0.4
            };                                                   // $steps_in_path

            // Then we add some steps set in meters because that looks better at the ends of the beam
            // foreach (array(0.3,1,2,4,7,10,20,40,70,100) as $meters) {
            foreach (var meters in new[] { 0.3, 1.0, 2.0, 4.0, 7.0, 10.0, 20.0, 40.0, 70.0, 100.0 })
            {
                // calculate fraction of path
                // $steps_in_path[] = $meters / $distance;
                stepsInPath.Add(meters / directionVector.Distance);
            }

            // Add the reverse of these steps on other side of beam
            // $temp = $steps_in_path;
            var temp = new List <double>(stepsInPath);

            // foreach ($temp as $step) {
            foreach (var step in temp)
            {
                // $steps_in_path[] = 1 - $step;
                stepsInPath.Add(1 - step);
            }

            // Sort and remove duplicates
            var distinctSteps = stepsInPath.OrderBy(p => p).Distinct();

            List <List <LocationVector> > rings = new List <List <LocationVector> >();

            // Fill array $rings with arrays that each hold a ring of points surrounding the beam
            foreach (var step in distinctSteps)
            {
                // $centerpoint['lat'] = $from['lat'] + ( ($to['lat'] - $from['lat']) * $step );
                // $centerpoint['lon'] = $from['lon'] + ( ($to['lon'] - $from['lon']) * $step );
                // $centerpoint['alt'] = $from['alt'] + ( ($to['alt'] - $from['alt']) * $step );
                ILocation centerpoint = new LocationVector(
                    directionVector.From.Latitude + ((directionVector.To.Latitude - directionVector.From.Latitude) * step),
                    directionVector.From.Longitude + ((directionVector.To.Longitude - directionVector.From.Longitude) * step),
                    directionVector.From.Altitude + ((directionVector.To.Altitude - directionVector.From.Altitude) * step));

                // Fresnel radius calculation
                // $d1 = $distance * $step;
                var d1 = directionVector.Distance * step;

                // $d2 = $distance - $d1;
                var d2 = directionVector.Distance - d1;

                // $radius = sqrt(($wavelen * $d1 * $d2) / $distance );
                var radius = Math.Sqrt((wavelength * d1 * d2) / directionVector.Distance);

                // Bearing of line perpendicular to bearing of line of sight.
                // $ring_bearing = $bearing + 90 % 360;
                var ringBearing    = (directionVector.Bearing + 90.0) % 360.0;
                var cosRingBearing = Math.Cos(ringBearing.ToRadian());
                var sinRingBearing = Math.Sin(ringBearing.ToRadian());

                List <LocationVector> ring = new List <LocationVector>(stepsInCircles);
                //for ($n = 0; $n < $steps_in_circles; $n++) {
                for (int n = 0; n < stepsInCircles; ++n)
                {
                    //$angle = $n * (360 / $steps_in_circles );
                    var angle       = n * (360.0 / stepsInCircles);
                    var radianAngle = angle.ToRadian();

                    // $vertical_factor = cos(deg2rad($angle));
                    var verticalFactor = Math.Cos(radianAngle);

                    // $horizontal_factor = sin(deg2rad($angle));
                    var horizontalFactor = Math.Sin(radianAngle);

                    // $lat_factor = cos(deg2rad($ring_bearing)) * $horizontal_factor;
                    var latitudeFactor = cosRingBearing * horizontalFactor;

                    // $lon_factor = sin(deg2rad($ring_bearing)) * $horizontal_factor;
                    var longitudeFactor = sinRingBearing * horizontalFactor;

                    // $new_point['lat'] = $centerpoint['lat'] + ($lat_factor * $lat_meter * $radius);
                    // $new_point['lon'] = $centerpoint['lon'] + ($lon_factor * $lon_meter * $radius);
                    // $new_point['alt'] = $centerpoint['alt'] + ($vertical_factor * $radius);
                    var newPoint = new LocationVector(
                        centerpoint.Latitude + (latitudeFactor * latitudeMeterPerDegree * radius),
                        centerpoint.Longitude + (longitudeFactor * longitudeMeterPerDegree * radius),
                        centerpoint.Altitude + (verticalFactor * radius));

                    // $ring[] = $new_point;
                    ring.Add(newPoint);
                }

                // $rings[] = $ring;
                rings.Add(ring);
            }

            return(rings);
        }
Beispiel #17
0
 public Vector(LocationVector vector)
 {
     Distance = vector.Distance == null ? new Nullable <double>() : vector.Distance.Value;
     Bearing  = vector.Bearing;
 }
        private void OnVectorChanged(DependencyPropertyChangedEventArgs e)
        {
            LocationVector v = (LocationVector)e.NewValue;

            RefreshFromVector(v);
        }