Ejemplo n.º 1
0
        protected virtual void TestRectangle(double minX, double width, double minY, double height)
        {
            double maxX = minX + width;
            double maxY = minY + height;

            minX = NormX(minX);
            maxX = NormX(maxX);

            IRectangle r = ctx.MakeRectangle(minX, maxX, minY, maxY);

            //test equals & hashcode of duplicate
            IRectangle r2 = ctx.MakeRectangle(minX, maxX, minY, maxY);

            Assert.Equal(r, r2);
            Assert.Equal(r.GetHashCode(), r2.GetHashCode());

            string msg = r.ToString();

            Assert.Equal(/*msg,*/ width != 0 && height != 0, r.HasArea);
            Assert.Equal(/*msg,*/ width != 0 && height != 0, r.GetArea(ctx) > 0);
            if (ctx.IsGeo && r.Width == 360 && r.Height == 180)
            {
                //whole globe
                double earthRadius = DistanceUtils.ToDegrees(1);
                CustomAssert.EqualWithDelta(4 * Math.PI * earthRadius * earthRadius, r.GetArea(ctx), 1.0);//1km err
            }

            AssertEqualsRatio(msg, height, r.Height);
            AssertEqualsRatio(msg, width, r.Width);
            IPoint center = r.Center;

            msg += " ctr:" + center;
            //System.out.println(msg);
            AssertRelation(msg, SpatialRelation.CONTAINS, r, center);

            IDistanceCalculator dc = ctx.DistCalc;
            double dUR             = dc.Distance(center, r.MaxX, r.MaxY);
            double dLR             = dc.Distance(center, r.MaxX, r.MinY);
            double dUL             = dc.Distance(center, r.MinX, r.MaxY);
            double dLL             = dc.Distance(center, r.MinX, r.MinY);

            Assert.Equal(/*msg,*/ width != 0 || height != 0, dUR != 0);
            if (dUR != 0)
            {
                Assert.True(dUR > 0 && dLL > 0);
            }
            AssertEqualsRatio(msg, dUR, dUL);
            AssertEqualsRatio(msg, dLR, dLL);
            if (!ctx.IsGeo || center.Y == 0)
            {
                AssertEqualsRatio(msg, dUR, dLL);
            }
        }
            public override double DoubleVal(int doc)
            {
                var vals = cache.GetShapes(doc);

                if (vals != null)
                {
                    double v = calculator.Distance(from, vals[0]);
                    for (int i = 1; i < vals.Count; i++)
                    {
                        v = Math.Min(v, calculator.Distance(from, vals[i]));
                    }
                    return(v * enclosingInstance.multiplier);
                }
                return(nullValue);
            }
        /// <summary>
        /// Returns index of most similar color in Palette.
        /// </summary>
        /// <param name="color">Target color.</param>
        /// <returns>Index of most </returns>
        public int GetPaletteIndex(Color color)
        {
            if (Palette.Count == 0)
            {
                throw new ArgumentNullException("Palette is empty, please use GetPalette first.", "Palette");
            }

            int bestIndex    = 0;
            int bestDistance = int.MaxValue;

            for (int i = 0; i < Palette.Count; i++)
            {
                int distance = distanceCalculator.Distance(color, Palette[i]);
                if (distance < bestDistance)
                {
                    if (distance <= 12) //if color is in cell. 9 = 3 * 3
                    {
                        return(i);
                    }
                    bestDistance = distance;
                    bestIndex    = i;
                }
            }
            return(bestIndex);
        }
Ejemplo n.º 4
0
 public override double DoubleVal(int doc)
 {
     // make sure it has minX and area
     if (validX.Get(doc))
     {
         Debug.Assert(validY.Get(doc));
         return(calculator.Distance(outerInstance.from, ptX.Get(doc), ptY.Get(doc)) * outerInstance.multiplier);
     }
     return(nullValue);
 }
        /// <summary>
        /// Creates the dynamic table.
        /// Cell[i, j] defines the best match in which array1 contains all elements up to i (including), and array2 contains all elements up to j (including)
        /// </summary>
        private long[,] CreateDynamicTable(ICollectionWrapper <T> collection1, ICollectionWrapper <T> collection2, CancellationToken cancellationToken)
        {
            var dynamicTable = new long[collection1.Length + 1, collection2.Length + 1];

            dynamicTable[0, 0] = 0;
            for (int i = 1; i < collection1.Length + 1; i++)
            {
                dynamicTable[i, 0] = dynamicTable[i - 1, 0] + removalCalculator.RemovalCost(collection1[i - 1]);
            }
            for (int i = 1; i < collection2.Length + 1; i++)
            {
                dynamicTable[0, i] = dynamicTable[0, i - 1] + insertionCalculator.InsertionCost(collection2[i - 1]);
            }

            OnProgressUpdate?.Invoke(1, collection1.Length + 1);
            for (int i = 1; i < collection1.Length + 1; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                for (int j = 1; j < collection2.Length + 1; j++)
                {
                    if (collection1[i - 1].Equals(collection2[j - 1]))
                    {
                        dynamicTable[i, j] = dynamicTable[i - 1, j - 1];
                        continue;
                    }

                    var insertion = dynamicTable[i, j - 1] + insertionCalculator.InsertionCost(collection1[i - 1]);
                    var removal   = dynamicTable[i - 1, j] + removalCalculator.RemovalCost(collection2[j - 1]);
                    var min       = Min(insertion, removal);

                    if (distanceCalculator != null)
                    {
                        var substitution = dynamicTable[i - 1, j - 1] + distanceCalculator.Distance(collection1[i - 1], collection2[j - 1]);
                        min = Math.Min(min, substitution);
                    }
                    if (CanDoTransposition(collection1, collection2, i, j))
                    {
                        var transposition = transpositionCalculator.TranspositionCost(collection1[i - 1], collection1[i - 2]) + dynamicTable[i - 2, j - 2];
                        min = Math.Min(min, transposition);
                    }
                    dynamicTable[i, j] = min;
                }
                OnProgressUpdate?.Invoke(i + 1, collection1.Length + 1);
            }
            return(dynamicTable);
        }
Ejemplo n.º 6
0
        public void CheckPerimeter()
        {
            if (this.coreApplicationContext.CurrentLocation != null)
            {
                if (this.coreApplicationContext.CurrentTrackLocation != null)
                {
                    var distance = distanceCalculator.Distance(this.coreApplicationContext.CurrentLocation, this.coreApplicationContext.CurrentTrackLocation.ToCoordinate(), UnitsOfLength.Meter);
                    if (distance < Constants.PerimeterDistance)
                    {
                        // All OK still in perimeter of current TrackLocation
                        return;
                    }

                    this.coreApplicationContext.SetTrackLocation(null);
                }

                var location = this.FindTrackLocationForCoordinate(this.coreApplicationContext.CurrentLocation);
                this.coreApplicationContext.SetTrackLocation(location);
            }
        }
Ejemplo n.º 7
0
 public override double Distance(IPoint @from, double toX, double toY)
 {
     return(Round(_delegate.Distance(from, toX, toY)));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Convenience that uses <see cref="DistCalc"/>
 /// </summary>
 public virtual double CalcDistance(IPoint p, double x2, double y2)
 {
     return(DistCalc.Distance(p, x2, y2));
 }