Exemplo n.º 1
0
        /// <summary>
        /// Create a fixed-int number from parts.  For example, to create 1.5 pass in 1 and 500.
        /// </summary>
        /// <param name="PreDecimal">The number above the decimal.  For 1.5, this would be 1.</param>
        /// <param name="PostDecimal">The number below the decimal, to three digits.  
        /// For 1.5, this would be 500. For 1.005, this would be 5.</param>
        /// <returns>A fixed-int representation of the number parts</returns>
        public static FInt FromParts(int PreDecimal, int PostDecimal) {
            FInt f = new FInt(PreDecimal);
            if (PostDecimal != 0)
                f.RawValue += (new FInt(PostDecimal) / 1000).RawValue;

            return f;
        }
Exemplo n.º 2
0
 public static void Limit(ref PVector2 val, FInt maxLength)
 {
     var l = val.Length();
     if (l > maxLength) {
         var ratio = maxLength / l;
         val.X *= ratio;
         val.Y *= ratio;
     }
 }
Exemplo n.º 3
0
        public Bug(PVector2 p, PVector2 v, List<Boid> bs, Random r)
            : base(p, v, bs, new FInt(3), r)
        {
            AlignmentWeight = new FInt(0.1);
            CohesionWeight = new FInt(1.0);
            SeperationWeight = new FInt(2.0);
            Momentum = new FInt(5.0);
            RandomWeight = new FInt(0.1);

            MaxVelocity = new FInt(5);
            VisionRangeSquared = new FInt(25.0 * 25.0);
        }
Exemplo n.º 4
0
 public static FInt Atan(FInt F) {
     return Asin(F / Sqrt(FInt.OneF + (F * F)));
 }
Exemplo n.º 5
0
        public static FInt Asin(FInt F) {
            bool isNegative = F < 0;
            F = Abs(F);

            if (F > FInt.OneF)
                throw new ArithmeticException("Bad Asin Input:" + F.ToDouble());

            FInt f1 = mul(mul(mul(mul(new FInt(145103 >> FInt.SHIFT_AMOUNT, false), F) -
                new FInt(599880 >> FInt.SHIFT_AMOUNT, false), F) +
                new FInt(1420468 >> FInt.SHIFT_AMOUNT, false), F) -
                new FInt(3592413 >> FInt.SHIFT_AMOUNT, false), F) +
                new FInt(26353447 >> FInt.SHIFT_AMOUNT, false);
            FInt f2 = PI / new FInt(2, true) - (Sqrt(FInt.OneF - F) * f1);

            return isNegative ? f2.Inverse : f2;
        }
Exemplo n.º 6
0
 public static FInt Tan(FInt i) {
     return Sin(i) / Cos(i);
 }
Exemplo n.º 7
0
 public static FInt Cos(FInt i) {
     return Sin(i + new FInt(6435, false));
 }
Exemplo n.º 8
0
 public PVector2(FInt x, FInt y)
 {
     X = x;
     Y = y;
 }
Exemplo n.º 9
0
 public static void Divide(ref FInt one, ref FInt other, out FInt dest) {
     dest.RawValue = (one.RawValue << SHIFT_AMOUNT) / (other.RawValue);
 }
Exemplo n.º 10
0
 public static FInt Sqrt(FInt f) {
     byte numberOfIterations = 8;
     if (f.RawValue > 0x64000)
         numberOfIterations = 12;
     if (f.RawValue > 0x3e8000)
         numberOfIterations = 16;
     return Sqrt(f, numberOfIterations);
 }
Exemplo n.º 11
0
        public static FInt PIOver180F = PI / (FInt)180; //PI / 180
        #endregion

        #region Sqrt
        public static FInt Sqrt(FInt f, int NumberOfIterations) {
            if (f.RawValue < 0) //NaN in Math.Sqrt
                throw new ArithmeticException("Input Error");
            if (f.RawValue == 0)
                return (FInt)0;
            FInt k = f + FInt.OneF >> 1;
            for (int i = 0; i < NumberOfIterations; i++)
                k = (k + (f / k)) >> 1;

            if (k.RawValue < 0)
                throw new ArithmeticException("Overflow");
            else
                return k;
        }
Exemplo n.º 12
0
 public static FInt Clamp(FInt val, FInt min, FInt max) {
     if (val < min) {
         return min;
     } else if (val > max) {
         return max;
     } else {
         return val;
     }
 }
Exemplo n.º 13
0
 public static void Sub(ref FInt one, ref FInt other, out FInt dest) {
     dest.RawValue = (one.RawValue - other.RawValue);
 }
Exemplo n.º 14
0
 public void DistanceSquaredTo(ref PVector2 other, out FInt i)
 {
     PVector2.Sub(ref this, ref other, out scratchV);
     scratchV.LengthSquared(out i);
 }
Exemplo n.º 15
0
 public static void Multiply(ref PVector2 v, ref FInt i, out PVector2 o)
 {
     FInt.Multiply(ref v.X, ref i, out o.X);
     FInt.Multiply(ref v.Y, ref i, out o.Y);
 }
Exemplo n.º 16
0
        public static FInt Atan2(FInt F1, FInt F2) {
            if (F2.RawValue == 0 && F1.RawValue == 0)
                return (FInt)0;

            FInt result = (FInt)0;
            if (F2 > 0)
                result = Atan(F1 / F2);
            else if (F2 < 0) {
                if (F1 >= 0)
                    result = (PI - Atan(Abs(F1 / F2)));
                else
                    result = (PI - Atan(Abs(F1 / F2))).Inverse;
            } else
                result = (F1 >= 0 ? PI : PI.Inverse) / new FInt(2, true);

            return result;
        }
Exemplo n.º 17
0
 public static FInt Abs(FInt F) {
     if (F < 0)
         return F.Inverse;
     else
         return F;
 }
Exemplo n.º 18
0
 public static FInt Sin(FInt i) {
     FInt j = (FInt)0;
     for (; i < 0; i += new FInt(25736, false))
         ;
     if (i > new FInt(25736, false))
         i %= new FInt(25736, false);
     FInt k = (i * new FInt(10, false)) / new FInt(714, false);
     if (i != 0 && i != new FInt(6434, false) && i != new FInt(12868, false) &&
         i != new FInt(19302, false) && i != new FInt(25736, false))
         j = (i * new FInt(100, false)) / new FInt(714, false) - k * new FInt(10, false);
     if (k <= new FInt(90, false))
         return sin_lookup(k, j);
     if (k <= new FInt(180, false))
         return sin_lookup(new FInt(180, false) - k, j);
     if (k <= new FInt(270, false))
         return sin_lookup(k - new FInt(180, false), j).Inverse;
     else
         return sin_lookup(new FInt(360, false) - k, j).Inverse;
 }
Exemplo n.º 19
0
 public static void Multiply(ref FInt one, ref FInt other, out FInt dest) {
     dest.RawValue = (one.RawValue * other.RawValue) >> SHIFT_AMOUNT;
 }
Exemplo n.º 20
0
 private static FInt sin_lookup(FInt i, FInt j) {
     if (j > 0 && j < new FInt(10, false) && i < new FInt(90, false))
         return new FInt(SIN_TABLE[i.RawValue], false) +
             ((new FInt(SIN_TABLE[i.RawValue + 1], false) - new FInt(SIN_TABLE[i.RawValue], false)) /
             new FInt(10, false)) * j;
     else
         return new FInt(SIN_TABLE[i.RawValue], false);
 }
Exemplo n.º 21
0
 private static FInt mul(FInt F1, FInt F2) {
     return F1 * F2;
 }
Exemplo n.º 22
0
 public void LengthSquared(out FInt i)
 {
     FInt.Multiply(ref X, ref X, out scratch1);
     FInt.Multiply(ref Y, ref Y, out scratch2);
     FInt.Add(ref scratch1, ref scratch2, out i);
 }