public Bound(Real x, Real z, Real radius) { Contract.Requires(radius > 0, "Radius must be > 0"); X = x; Z = z; Radius = radius; }
public void Normalize() { Real length = Length(); if (length == 0) { return; } X /= length; Z /= length; }
public static Real Asin(Real F) { bool isNegative = F < 0; F = Abs(F); if (F > Real.OneF) throw new ArithmeticException("Bad Asin Input:" + F.AsFloat); Real f1 = mul(mul(mul(mul(Real.Create(145103 >> Real.SHIFT_AMOUNT, false), F) - Real.Create(599880 >> Real.SHIFT_AMOUNT, false), F) + Real.Create(1420468 >> Real.SHIFT_AMOUNT, false), F) - Real.Create(3592413 >> Real.SHIFT_AMOUNT, false), F) + Real.Create(26353447 >> Real.SHIFT_AMOUNT, false); Real f2 = PI / Real.Create(2, true) - (Sqrt(Real.OneF - F) * f1); return isNegative ? f2.Inverse : f2; }
public static Real Tan(Real i) { return Sin(i) / Cos(i); }
public Vector2r(Real x, Real z) { X = x; Z = z; }
/// <summary> /// Returns true if the given point is contained within this bound. /// </summary> public bool Contains(Real x, Real z) { Real distanceSq = Vector2r.DistanceSq(X, Z, x, z); return (Radius * Radius) > distanceSq; }
public static Real Max(Real one, Real other) { return one.RawValue > other.RawValue ? one : other; }
public static Real Ceiling(Real F) { Real f2; f2.RawValue = ((F.RawValue >> SHIFT_AMOUNT) << SHIFT_AMOUNT) + One; return f2; }
public static Real Abs(Real F) { if (F < 0) return F.Inverse; else return F; }
public static Real PIOver180F = PI / (Real)180; //PI / 180 #endregion #region Sqrt public static Real Sqrt(Real f, int NumberOfIterations) { if (f.RawValue < 0) //NaN in Math.Sqrt throw new ArithmeticException("Input Error"); if (f.RawValue == 0) return (Real)0; Real k = f + Real.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; }
public static Real DistanceSq(Real x0, Real z0, Real x1, Real z1) { var dx = x0 - x1; var dz = z0 - z1; return dx * dx + dz * dz; }
public static Real Distance(Real x0, Real z0, Real x1, Real z1) { var dx = x0 - x1; var dz = z0 - z1; return Real.Sqrt(dx * dx + dz * dz); }
/// <summary> /// Returns true if the given point is contained within this bound. /// </summary> public bool Contains(Real x, Real z) { Real distanceSq = Vector2r.DistanceSq(X, Z, x, z); return((Radius * Radius) > distanceSq); }
public Real Length() { return(Real.Sqrt(X * X + Z * Z)); }
public static Real Atan(Real F) { return Asin(F / Sqrt(Real.OneF + (F * F))); }
public static Real Atan2(Real F1, Real F2) { if (F2.RawValue == 0 && F1.RawValue == 0) return (Real)0; Real result = (Real)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) / Real.Create(2, true); return result; }
public static Real Sqrt(Real f) { byte numberOfIterations = 8; if (f.RawValue > 0x64000) numberOfIterations = 12; if (f.RawValue > 0x3e8000) numberOfIterations = 16; return Sqrt(f, numberOfIterations); }
public static Real Floor(Real F) { Real f2; f2.RawValue = (F.RawValue >> SHIFT_AMOUNT) << SHIFT_AMOUNT; return f2; }
public static Real Sin(Real i) { Real j = (Real)0; for (; i < 0; i += Real.Create(25736, false)) ; if (i > Real.Create(25736, false)) i %= Real.Create(25736, false); Real k = (i * Real.Create(10, false)) / Real.Create(714, false); if (i != 0 && i != Real.Create(6434, false) && i != Real.Create(12868, false) && i != Real.Create(19302, false) && i != Real.Create(25736, false)) j = (i * Real.Create(100, false)) / Real.Create(714, false) - k * Real.Create(10, false); if (k <= Real.Create(90, false)) return sin_lookup(k, j); if (k <= Real.Create(180, false)) return sin_lookup(Real.Create(180, false) - k, j); if (k <= Real.Create(270, false)) return sin_lookup(k - Real.Create(180, false), j).Inverse; else return sin_lookup(Real.Create(360, false) - k, j).Inverse; }
public static Real Min(Real one, Real other) { return one.RawValue < other.RawValue ? one : other; }
private static Real sin_lookup(Real i, Real j) { if (j > 0 && j < Real.Create(10, false) && i < Real.Create(90, false)) return Real.Create(SIN_TABLE[i.RawValue], false) + ((Real.Create(SIN_TABLE[i.RawValue + 1], false) - Real.Create(SIN_TABLE[i.RawValue], false)) / Real.Create(10, false)) * j; else return Real.Create(SIN_TABLE[i.RawValue], false); }
private static Real mul(Real F1, Real F2) { return F1 * F2; }
public static Real Cos(Real i) { return Sin(i + Real.Create(6435, false)); }
public static Real Range(Real min, Real max) { return Real.CreateFromRaw(_random.NextLong(min.RawValue, max.RawValue)); }
protected static Real Interpolate(Real start, Real end, float percentage) { Real delta = end - start; return start + (delta * percentage); //return (start * (1 - percentage)) + (end * percentage); }