Ejemplo n.º 1
0
        private static DECIMAL GradCoord2D(int seed, int x, int y, DECIMAL xd, DECIMAL yd)
        {
            int hash = seed;

            hash ^= PrimeX * x;
            hash ^= PrimeY * y;

            hash = hash * hash * hash * 60493;
            hash = (hash >> 13) ^ hash;

            Decimal2 g = Grad2D[hash & 7];

            return(xd * g.x + yd * g.y);
        }
        private void SingleGradientPerturb(int seed, DECIMAL perturbAmp, DECIMAL frequency, ref DECIMAL x, ref DECIMAL y)
        {
            DECIMAL xf = x * frequency;
            DECIMAL yf = y * frequency;

            int x0 = FastFloor(xf);
            int y0 = FastFloor(yf);
            int x1 = x0 + 1;
            int y1 = y0 + 1;

            DECIMAL xs, ys;

            switch (InterpolationMethod)
            {
            default:
            case Interp.Linear:
                xs = xf - x0;
                ys = yf - y0;
                break;

            case Interp.Hermite:
                xs = InterpHermiteFunc(xf - x0);
                ys = InterpHermiteFunc(yf - y0);
                break;

            case Interp.Quintic:
                xs = InterpQuinticFunc(xf - x0);
                ys = InterpQuinticFunc(yf - y0);
                break;
            }

            Decimal2 vec0 = Cell2D[Hash2D(seed, x0, y0) & 255];
            Decimal2 vec1 = Cell2D[Hash2D(seed, x1, y0) & 255];

            DECIMAL lx0x = Lerp(vec0.x, vec1.x, xs);
            DECIMAL ly0x = Lerp(vec0.y, vec1.y, xs);

            vec0 = Cell2D[Hash2D(seed, x0, y1) & 255];
            vec1 = Cell2D[Hash2D(seed, x1, y1) & 255];

            DECIMAL lx1x = Lerp(vec0.x, vec1.x, xs);
            DECIMAL ly1x = Lerp(vec0.y, vec1.y, xs);

            x += Lerp(lx0x, lx1x, ys) * perturbAmp;
            y += Lerp(ly0x, ly1x, ys) * perturbAmp;
        }
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            var p1 = GetInteger(bindingContext, "P1");
            var p2 = GetInteger(bindingContext, "P2");
            var p3 = GetString(bindingContext, "P2");

            if (p1 != null && p2 != null)
            {
                var r = new Decimal2(Convert.ToDecimal(p1 + (p2 / (Math.Pow(10, p3.ToString().Length)))));
                bindingContext.Result = ModelBindingResult.Success(r);
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
 public static DecimalPart BreakPart(this Decimal2 decimalValue)
 {
     if (decimalValue != null)
     {
         var result = decimalValue.Value.ToString(CultureInfo.InvariantCulture).Split('.');
         if (result.Count() == 2)
         {
             var d             = int.Parse(result[0]);
             var f             = int.Parse(result[1]);
             var fractionSplit = new DecimalPart
             {
                 Integral = d.ToString("N0"),
                 Fraction = f < 10 ? "0" + f.ToString("N0") : f.ToString("N0")
             };
             return(fractionSplit);
         }
     }
     return(new DecimalPart("0", "0"));
 }
    /// <summary>
    /// Wyciąga z podanej listy (tablicy atrybutów) atrybut o maksymalnej wartości
    /// i zwraca Tuple nazwa atrybutu na jego wartość.
    /// Gdy brak wartości atrybutów, to zwróci Decimal2.Min i pusty string.
    /// </summary>
    public Tuple <string, Decimal2> SelectMin(IEnumerable attributeNames)
    {
        Decimal2 resultVal  = Decimal2.MaxValue;
        string   resultName = "";

        foreach (string str in attributeNames)
        {
            Decimal2 value;
            if (_attributes.TryGetValue(str, out value))
            {
                if (value <= resultVal)
                {
                    resultVal  = value;
                    resultName = str;
                }
            }
        }

        return(Tuple.Create(resultName, resultVal));
    }
Ejemplo n.º 6
0
        public static void Parse_Invalid(string value, NumberStyles style, IFormatProvider provider, Type exceptionType)
        {
            bool isDefaultProvider = provider == null || provider == NumberFormatInfo.InvariantInfo;

            if (provider == null)
            {
                provider = NumberFormatInfo.InvariantInfo;
            }

            decimal result;

            if ((style & ~NumberStyles.Integer) == 0 && style != NumberStyles.None && (style & NumberStyles.AllowLeadingWhite) == (style & NumberStyles.AllowTrailingWhite))
            {
                // Use Parse(string) or Parse(string, IFormatProvider)
                if (isDefaultProvider)
                {
                    //Assert.IsFalse(Decimal2.TryParse(value, out result));
                    //Assert.AreEqual(default(decimal), result);

                    //AssertExtensions.Throws(exceptionType, () => Decimal2.Parse(value));
                }

                AssertExtensions.Throws(exceptionType, () => Decimal2.Parse(value, provider));
            }

            // Use Parse(string, NumberStyles, IFormatProvider)
            Assert.IsFalse(Decimal2.TryParse(value, style, provider, out result));
            Assert.AreEqual(default(decimal), result);

            AssertExtensions.Throws(exceptionType, () => Decimal2.Parse(value, style, provider));

            if (isDefaultProvider)
            {
                // Use Parse(string, NumberStyles) or Parse(string, NumberStyles, IFormatProvider)
                Assert.IsFalse(Decimal2.TryParse(value, style, NumberFormatInfo.InvariantInfo, out result));
                Assert.AreEqual(default(decimal), result);

                //AssertExtensions.Throws(exceptionType, () => Decimal2.Parse(value, style));
                AssertExtensions.Throws(exceptionType, () => Decimal2.Parse(value, style, NumberFormatInfo.InvariantInfo));
            }
        }
Ejemplo n.º 7
0
        public static void Parse(string value, NumberStyles style, IFormatProvider provider, decimal expected)
        {
            bool isDefaultProvider = provider == null || provider == NumberFormatInfo.InvariantInfo;

            if (provider == null)
            {
                provider = NumberFormatInfo.InvariantInfo;
            }

            decimal result;

            if ((style & ~NumberStyles.Integer) == 0 && style != NumberStyles.None)
            {
                // Use Parse(string) or Parse(string, IFormatProvider)
                if (isDefaultProvider)
                {
                    Assert.IsTrue(Decimal2.TryParse(value, out result));
                    Assert.AreEqual(expected, result);

                    Assert.AreEqual(expected, Decimal2.Parse(value));
                }

                Assert.AreEqual(expected, Decimal2.Parse(value, provider));
            }

            // Use Parse(string, NumberStyles, IFormatProvider)
            Assert.IsTrue(Decimal2.TryParse(value, style, provider, out result), "Error parsing: {0}", value);
            Assert.AreEqual(expected, result);

            Assert.AreEqual(expected, Decimal2.Parse(value, style, provider));

            if (isDefaultProvider)
            {
                // Use Parse(string, NumberStyles) or Parse(string, NumberStyles, IFormatProvider)
                Assert.IsTrue(Decimal2.TryParse(value, style, NumberFormatInfo.InvariantInfo, out result));
                Assert.AreEqual(expected, result);

                //Assert.AreEqual(expected, Decimal2.Parse(value, style));
                Assert.AreEqual(expected, Decimal2.Parse(value, style, NumberFormatInfo.InvariantInfo));
            }
        }
Ejemplo n.º 8
0
 public static Decimal Decimal(Decimal2 dec)
 {
     return(dec);
 }
        private DECIMAL SingleCellular2Edge(DECIMAL x, DECIMAL y)
        {
            int xr = FastRound(x);
            int yr = FastRound(y);

            DECIMAL[] distance = { 999999, 999999, 999999, 999999 };

            switch (CellularDistanceFunction)
            {
            default:
            case CellularDistanceFunctions.Euclidean:
                for (int xi = xr - 1; xi <= xr + 1; xi++)
                {
                    for (int yi = yr - 1; yi <= yr + 1; yi++)
                    {
                        Decimal2 vec = Cell2D[Hash2D(Seed, xi, yi) & 255];

                        DECIMAL vecX = xi - x + vec.x * CellularJitter;
                        DECIMAL vecY = yi - y + vec.y * CellularJitter;

                        DECIMAL newDistance = vecX * vecX + vecY * vecY;

                        for (int i = cellularDistanceIndex1; i > 0; i--)
                        {
                            distance[i] = Math.Max(Math.Min(distance[i], newDistance), distance[i - 1]);
                        }
                        distance[0] = Math.Min(distance[0], newDistance);
                    }
                }
                break;

            case CellularDistanceFunctions.Manhattan:
                for (int xi = xr - 1; xi <= xr + 1; xi++)
                {
                    for (int yi = yr - 1; yi <= yr + 1; yi++)
                    {
                        Decimal2 vec = Cell2D[Hash2D(Seed, xi, yi) & 255];

                        DECIMAL vecX = xi - x + vec.x * CellularJitter;
                        DECIMAL vecY = yi - y + vec.y * CellularJitter;

                        DECIMAL newDistance = Math.Abs(vecX) + Math.Abs(vecY);

                        for (int i = cellularDistanceIndex1; i > 0; i--)
                        {
                            distance[i] = Math.Max(Math.Min(distance[i], newDistance), distance[i - 1]);
                        }
                        distance[0] = Math.Min(distance[0], newDistance);
                    }
                }
                break;

            case CellularDistanceFunctions.Natural:
                for (int xi = xr - 1; xi <= xr + 1; xi++)
                {
                    for (int yi = yr - 1; yi <= yr + 1; yi++)
                    {
                        Decimal2 vec = Cell2D[Hash2D(Seed, xi, yi) & 255];

                        DECIMAL vecX = xi - x + vec.x * CellularJitter;
                        DECIMAL vecY = yi - y + vec.y * CellularJitter;

                        DECIMAL newDistance = (Math.Abs(vecX) + Math.Abs(vecY)) + (vecX * vecX + vecY * vecY);

                        for (int i = cellularDistanceIndex1; i > 0; i--)
                        {
                            distance[i] = Math.Max(Math.Min(distance[i], newDistance), distance[i - 1]);
                        }
                        distance[0] = Math.Min(distance[0], newDistance);
                    }
                }
                break;
            }

            switch (CellularReturnType)
            {
            case CellularReturnTypes.Distance2:
                return(distance[cellularDistanceIndex1]);

            case CellularReturnTypes.Distance2Add:
                return(distance[cellularDistanceIndex1] + distance[cellularDistanceIndex0]);

            case CellularReturnTypes.Distance2Sub:
                return(distance[cellularDistanceIndex1] - distance[cellularDistanceIndex0]);

            case CellularReturnTypes.Distance2Mul:
                return(distance[cellularDistanceIndex1] * distance[cellularDistanceIndex0]);

            case CellularReturnTypes.Distance2Div:
                return(distance[cellularDistanceIndex0] / distance[cellularDistanceIndex1]);

            default:
                return(0);
            }
            ;
        }
        private DECIMAL SingleCellular(DECIMAL x, DECIMAL y)
        {
            int xr = FastRound(x);
            int yr = FastRound(y);

            DECIMAL distance = 999999;
            int     xc = 0, yc = 0;

            switch (CellularDistanceFunction)
            {
            default:
            case CellularDistanceFunctions.Euclidean:
                for (int xi = xr - 1; xi <= xr + 1; xi++)
                {
                    for (int yi = yr - 1; yi <= yr + 1; yi++)
                    {
                        Decimal2 vec = Cell2D[Hash2D(Seed, xi, yi) & 255];

                        DECIMAL vecX = xi - x + vec.x * CellularJitter;
                        DECIMAL vecY = yi - y + vec.y * CellularJitter;

                        DECIMAL newDistance = vecX * vecX + vecY * vecY;

                        if (newDistance < distance)
                        {
                            distance = newDistance;
                            xc       = xi;
                            yc       = yi;
                        }
                    }
                }
                break;

            case CellularDistanceFunctions.Manhattan:
                for (int xi = xr - 1; xi <= xr + 1; xi++)
                {
                    for (int yi = yr - 1; yi <= yr + 1; yi++)
                    {
                        Decimal2 vec = Cell2D[Hash2D(Seed, xi, yi) & 255];

                        DECIMAL vecX = xi - x + vec.x * CellularJitter;
                        DECIMAL vecY = yi - y + vec.y * CellularJitter;

                        DECIMAL newDistance = (Math.Abs(vecX) + Math.Abs(vecY));

                        if (newDistance < distance)
                        {
                            distance = newDistance;
                            xc       = xi;
                            yc       = yi;
                        }
                    }
                }
                break;

            case CellularDistanceFunctions.Natural:
                for (int xi = xr - 1; xi <= xr + 1; xi++)
                {
                    for (int yi = yr - 1; yi <= yr + 1; yi++)
                    {
                        Decimal2 vec = Cell2D[Hash2D(Seed, xi, yi) & 255];

                        DECIMAL vecX = xi - x + vec.x * CellularJitter;
                        DECIMAL vecY = yi - y + vec.y * CellularJitter;

                        DECIMAL newDistance = (Math.Abs(vecX) + Math.Abs(vecY)) + (vecX * vecX + vecY * vecY);

                        if (newDistance < distance)
                        {
                            distance = newDistance;
                            xc       = xi;
                            yc       = yi;
                        }
                    }
                }
                break;
            }

            switch (CellularReturnType)
            {
            case CellularReturnTypes.CellValue:
                return(ValCoord2D(Seed, xc, yc));

            case CellularReturnTypes.NoiseLookup:
                Decimal2 vec = Cell2D[Hash2D(Seed, xc, yc) & 255];
                return(CellularNoiseLookup.GetNoise(xc + vec.x * CellularJitter, yc + vec.y * CellularJitter));

            case CellularReturnTypes.Distance:
                return(distance);

            default:
                return(0);
            }
        }