Advertisement
TizzyT

Angle -TizzyT

Jul 5th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1.     public struct Angle
  2.     {
  3.         internal const decimal SCALAR = 100000000000000000000000000m;
  4.         internal const decimal BASE = 36000000000000000000000000000m;
  5.         internal readonly decimal _numerator;
  6.         private Angle(decimal numerator)
  7.         {
  8.             if (numerator % 1 > 0) throw new Exception("Precision exceeded the capabilities of Angle."); ;
  9.             _numerator = numerator < 0 ? ((numerator + 1) % BASE + BASE - 1) : numerator % BASE;
  10.         }
  11.         public static implicit operator Angle(decimal degrees) =>
  12.             new Angle(degrees * SCALAR);
  13.         public static Angle operator +(Angle angle1, Angle angle2) =>
  14.             new Angle(angle1._numerator + angle2._numerator);
  15.         public static Angle operator -(Angle angle1, Angle angle2) =>
  16.             new Angle(angle1._numerator - angle2._numerator);
  17.         public override string ToString() =>
  18.             (_numerator / SCALAR).ToString();
  19.     }
  20.     public static class AngleEx
  21.     {
  22.         private const decimal PI = 3.1415926535897932384626433832m;
  23.         public static decimal Radians(this Angle @this) =>
  24.             @this._numerator / Angle.SCALAR * PI / 180m;
  25.         public static decimal Degrees(this Angle @this) =>
  26.             @this._numerator / Angle.SCALAR;
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement