public static double Gamma(double value, double absmax, double gamma)
    {
        bool flag = false;

        if (value < 0.0)
        {
            flag = true;
        }
        double num1 = MathD.Abs(value);

        if (num1 > absmax)
        {
            if (flag)
            {
                return(-num1);
            }
            else
            {
                return(num1);
            }
        }
        else
        {
            double num2 = MathD.Pow(num1 / absmax, gamma) * absmax;
            if (flag)
            {
                return(-num2);
            }
            else
            {
                return(num2);
            }
        }
    }
Example #2
0
        public void GetOddNumbers_OddNumbers_ReturnOdd(int odd, int[] expect)
        {
            var _math = new MathD();

            var result = _math.GetOddNumbers(odd);

            Assert.That(result, Is.EquivalentTo(expect));
        }
Example #3
0
        public void Max_AisGreaterThanB_ReturnA(int a, int b, int expect)
        {
            var _math = new MathD();

            var result = _math.Max(a, b);

            Assert.That(result, Is.EqualTo(expect));
        }
Example #4
0
        public void Add_AandB_ReturnAddition(int a, int b, int expect)
        {
            var mathD = new MathD();

            var result = mathD.Add(a, b);

            Assert.That(result, Is.EqualTo(expect));
        }
Example #5
0
        private static object Get()
        {
            if (_math != null)
            {
                return(_math);
            }

            Type   t = typeof(T);
            object m;

            if (t == typeof(sbyte))
            {
                m = new MathI8();
            }
            else if (t == typeof(byte))
            {
                m = new MathU8();
            }
            else if (t == typeof(short))
            {
                m = new MathI16();
            }
            else if (t == typeof(ushort))
            {
                m = new MathU16();
            }
            else if (t == typeof(int))
            {
                m = new MathI();
            }
            else if (t == typeof(uint))
            {
                m = new MathU();
            }
            else if (t == typeof(long))
            {
                m = new MathL();
            }
            else if (t == typeof(ulong))
            {
                m = new MathUL();
            }
            else if (t == typeof(float))
            {
                m = new MathF();
            }
            else if (t == typeof(double))
            {
                m = new MathD();
            }
            else
            {
                m = null;              // TODO: search open assemblies for INumTraits<T> via reflection?
            }
            return(_math = m);
        }
    public static double DeltaAngle(double current, double target)
    {
        double num = MathD.Repeat(target - current, 360d);

        if (num > 180.0d)
        {
            num -= 360d;
        }
        return(num);
    }
    public static double LerpAngle(double a, double b, double t)
    {
        double num = MathD.Repeat(b - a, 360d);

        if (num > 180.0d)
        {
            num -= 360d;
        }
        return(a + num * MathD.Clamp01(t));
    }
 public static double MoveTowards(double current, double target, double maxDelta)
 {
     if (MathD.Abs(target - current) <= maxDelta)
     {
         return(target);
     }
     else
     {
         return(current + MathD.Sign(target - current) * maxDelta);
     }
 }
Example #9
0
    protected override void InitEvents()
    {
        onDealDamage = this.NewPlayerActionEvent(() =>
        {
            PlayerStats.Inst.DamageToDeal += MathD.Round(PlayerStats.Inst.DamageToDeal * (effectPercent * 0.01f));
        });

        onDamaged = this.NewPlayerActionEvent(() =>
        {
            PlayerStats.Inst.DamageToReceive += MathD.Round(PlayerStats.Inst.DamageToReceive * (effectPercent * 0.01f));
        });
    }
Example #10
0
    //Maybe...http://answers.google.com/answers/threadview/id/782886.html
    //https://en.wikipedia.org/wiki/Position_of_the_Sun
    //https://en.wikipedia.org/wiki/Solar_azimuth_angle
    //http://www.itacanet.org/the-sun-as-a-source-of-energy/part-3-calculating-solar-angles/

    public double[] getZenithAndAzimuth(DateTime time, double longitude, double latitude, double timezone)
    {
        int daysInYear = 365;

        if (DateTime.IsLeapYear(time.Year))
        {
            daysInYear = 366;
        }


        //radians
        double fractionalYear = (2 * Math.PI / daysInYear) * (time.DayOfYear - 1 + ((time.Hour - 12) / 24));

        //eqptime in Minutes
        double eqtime = 229.18 * (0.000075 + 0.001868 * Math.Cos(fractionalYear) - 0.032077 * Math.Sin(fractionalYear) - 0.014615 * Math.Cos(2 * fractionalYear) - 0.040849 * Math.Sin(2 * fractionalYear));

        //decl in Radians
        double decl = 0.006918 - 0.399912 * Math.Cos(fractionalYear) + 0.070257 * Math.Sin(fractionalYear) - 0.006758 * Math.Cos(2 * fractionalYear) + 0.000907 * Math.Sin(2 * fractionalYear) - 0.002697 * Math.Cos(3 * fractionalYear) + 0.00148 * Math.Sin(3 * fractionalYear);

        double time_offset = eqtime + 4 * longitude - 60 * timezone;
        double tst         = time.Hour * 60 + time.Minute + time.Second / 60 + time_offset; //Tst= true solar time

        double ha = (tst / 4) - 180;                                                        //Solar Hour Angle in degrees

        //Debug.Log(ha);


        //Solar Zenith Angle, and solar azimuth angle in radians

        double sza = Math.Acos(MathD.Sin(latitude) * Math.Sin(decl) + MathD.Cos(latitude) * Math.Cos(decl) * MathD.Cos(ha));                  //Solar Zenith Angle in radians
        double saa = -(Math.Acos(-(MathD.Sin(latitude) * Math.Cos(sza) - Math.Sin(decl)) / (MathD.Cos(latitude) * Math.Sin(sza))) - Math.PI); //Solar Azimuth Angle

        //Wikiperdia solar azimuth angle
        //double saa = Math.Acos(
        //        (Math.Sin(decl) * Math.Cos(latitude) - Math.Cos(ha) * Math.Cos(decl) * Math.Sin(latitude)) / Math.Sin(sza)
        //    );

        double[] answer = new double[2];
        answer[0] = MathD.RadianToDegree(sza);
        answer[1] = MathD.RadianToDegree(saa);


        if (ha < 0 || ha > 180)
        {
            //answer[0] *= -1;
            answer[0] += 180;
        }

        Debug.LogFormat("Time: {0},\tZenith: {1},\tAzimuth: {2},\tHour Angle: {3}", time.ToString(), answer[0], answer[1], ha);

        return(answer);
    }
Example #11
0
    //https://www.ncdc.noaa.gov/gridsat/docs/Angle_Calculations.pdf
    public double[] getZenithAndAzimuth2(DateTime time, double longitude, double latitude, double timezone)
    {
        double hourAngle = -(getLocalHour(time) - 12 / 12);
        //Are the units correct? Degrees or radians?!
        double solarDeclinationAngle = -23.45 * Math.Cos(MathD.DegreeToRadian(2 * Math.PI * time.DayOfYear / 365 + 20 * Math.PI / 365));



        double[] answer = new double[2];


        // answer[0] = MathD.RadianToDegree(sza);
        //answer[1] = MathD.RadianToDegree(saa) % 360;
        return(answer);
    }
Example #12
0
    public void setSun()   //Sets sun to the current postion given latitude, longitude, and time
    {
        if (Sun != null)
        {
            double[] ZenAzi = getZenithAndAzimuth();
            //Debug.LogFormat("'{0}:{1}','{2}:{3}'", ZenAzi[0], MathD.HasValue(ZenAzi[0]), ZenAzi[1], MathD.HasValue(ZenAzi[1]));
            //Debug.Log(datetime);
            //Debug.LogFormat("{0}: {1}, {2}", datetime, ZenAzi[0], ZenAzi[1]);
            if (MathD.HasValue(ZenAzi[0]) && MathD.HasValue(ZenAzi[1]))
            {
                Sun.transform.localEulerAngles = new Vector3((float)ZenAzi[0], (float)ZenAzi[1], 0);
            }

            if (timeText != null)
            {
                timeText.text = datetime.ToString();
            }
        }
    }
    public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime)
    {
        smoothTime = MathD.Max(0.0001d, smoothTime);
        double num1 = 2d / smoothTime;
        double num2 = num1 * deltaTime;
        double num3 = (1.0d / (1.0d + num2 + 0.479999989271164d * num2 * num2 + 0.234999999403954d * num2 * num2 * num2));
        double num4 = current - target;
        double num5 = target;
        double max  = maxSpeed * smoothTime;
        double num6 = MathD.Clamp(num4, -max, max);

        target = current - num6;
        double num7 = (currentVelocity + num1 * num6) * deltaTime;

        currentVelocity = (currentVelocity - num1 * num7) * num3;
        double num8 = target + (num6 + num7) * num3;

        if (num5 - current > 0.0 == num8 > num5)
        {
            num8            = num5;
            currentVelocity = (num8 - num5) / deltaTime;
        }
        return(num8);
    }
    // This method calculates part values such as mass, lift, drag and connection forces, as well as all intermediates.
    public void CalculateAerodynamicValues(bool doInteraction = true)
    {
        // Calculate intemediate values
        //print(part.name + ": Calc Aero values");
        b_2 = (double)tipPosition.z - (double)Root.localPosition.z + 1.0;

        MAC = ((double)tipScale.x + (double)rootScale.x + 2.0) * (double)modelChordLenght / 2.0;

        midChordSweep = (MathD.Rad2Deg * Math.Atan(((double)Root.localPosition.x - (double)tipPosition.x) / b_2));

        taperRatio = ((double)tipScale.x + 1.0) / ((double)rootScale.x + 1.0);

        surfaceArea = MAC * b_2;

        aspectRatio = 2.0 * b_2 / MAC;

        ArSweepScale = Math.Pow(aspectRatio / MathD.Cos(MathD.Deg2Rad * midChordSweep), 2.0) + 4.0;
        ArSweepScale = 2.0 + Math.Sqrt(ArSweepScale);
        ArSweepScale = (2.0 * MathD.PI) / ArSweepScale * aspectRatio;

        wingMass = MathD.Clamp((double)massFudgeNumber * surfaceArea * ((ArSweepScale * 2.0) / (3.0 + ArSweepScale)) * ((1.0 + taperRatio) / 2), 0.01, double.MaxValue);

        Cd = (double)dragBaseValue / ArSweepScale * (double)dragMultiplier;

        Cl = (double)liftFudgeNumber * surfaceArea * ArSweepScale;

        //print("Gather Children");
        GatherChildrenCl();

        connectionForce = MathD.Round(MathD.Clamp(MathD.Sqrt(Cl + ChildrenCl) * (double)connectionFactor, (double)connectionMinimum, double.MaxValue));

        // Values always set
        if (isWing)
        {
            wingCost = (float)wingMass * (1f + (float)ArSweepScale / 4f) * costDensity;
            wingCost = Mathf.Round(wingCost / 5f) * 5f;
        }
        else if (isCtrlSrf)
        {
            wingCost  = (float)wingMass * (1f + (float)ArSweepScale / 4f) * costDensity * (1f - modelControlSurfaceFraction);
            wingCost += (float)wingMass * (1f + (float)ArSweepScale / 4f) * costDensityControl * modelControlSurfaceFraction;
            wingCost  = Mathf.Round(wingCost / 5f) * 5f;
        }

        part.breakingForce  = Mathf.Round((float)connectionForce);
        part.breakingTorque = Mathf.Round((float)connectionForce);

        // Stock-only values
        if (!FARactive)
        {
            // numbers for lift from: http://forum.kerbalspaceprogram.com/threads/118839-Updating-Parts-to-1-0?p=1896409&viewfull=1#post1896409
            float stockLiftCoefficient = (float)(surfaceArea / 3.52);
            // CoL/P matches CoM unless otherwise specified
            part.CoMOffset = new Vector3(Vector3.Dot(Tip.position - Root.position, part.transform.right) / 2, Vector3.Dot(Tip.position - Root.position, part.transform.up) / 2, 0);
            if (isWing && !isCtrlSrf)
            {
                part.Modules.GetModules <ModuleLiftingSurface>().FirstOrDefault().deflectionLiftCoeff = stockLiftCoefficient;
                part.mass = stockLiftCoefficient * 0.1f;
            }
            else
            {
                ModuleControlSurface mCtrlSrf = part.Modules.OfType <ModuleControlSurface>().FirstOrDefault();
                if (mCtrlSrf != null)
                {
                    mCtrlSrf.deflectionLiftCoeff = stockLiftCoefficient;
                    mCtrlSrf.ctrlSurfaceArea     = modelControlSurfaceFraction;
                    part.mass = stockLiftCoefficient * (1 + modelControlSurfaceFraction) * 0.1f;
                }
            }
        }

        // FAR values
        // With reflection stuff from r4m0n
        if (FARactive)
        {
            if (part.Modules.Contains("FARControllableSurface"))
            {
                PartModule FARmodule = part.Modules["FARControllableSurface"];
                Type       FARtype   = FARmodule.GetType();
                FARtype.GetField("b_2").SetValue(FARmodule, b_2);
                FARtype.GetField("b_2_actual").SetValue(FARmodule, b_2);
                FARtype.GetField("MAC").SetValue(FARmodule, MAC);
                FARtype.GetField("MAC_actual").SetValue(FARmodule, MAC);
                FARtype.GetField("S").SetValue(FARmodule, surfaceArea);
                FARtype.GetField("MidChordSweep").SetValue(FARmodule, midChordSweep);
                FARtype.GetField("TaperRatio").SetValue(FARmodule, taperRatio);
                FARtype.GetField("ctrlSurfFrac").SetValue(FARmodule, modelControlSurfaceFraction);
                //print("Set fields");
            }
            else if (part.Modules.Contains("FARWingAerodynamicModel"))
            {
                PartModule FARmodule = part.Modules["FARWingAerodynamicModel"];
                Type       FARtype   = FARmodule.GetType();
                FARtype.GetField("b_2").SetValue(FARmodule, b_2);
                FARtype.GetField("b_2_actual").SetValue(FARmodule, b_2);
                FARtype.GetField("MAC").SetValue(FARmodule, MAC);
                FARtype.GetField("MAC_actual").SetValue(FARmodule, MAC);
                FARtype.GetField("S").SetValue(FARmodule, surfaceArea);
                FARtype.GetField("MidChordSweep").SetValue(FARmodule, midChordSweep);
                FARtype.GetField("TaperRatio").SetValue(FARmodule, taperRatio);
            }
            if (!triggerUpdate && doInteraction)
            {
                TriggerUpdateAllWings();
            }
            if (doInteraction)
            {
                triggerUpdate = false;
            }
        }
        //print("FAR Done");
        // Update GUI values
        if (!FARactive)
        {
            guiCd       = Mathf.Round((float)Cd * 100f) / 100f;
            guiCl       = Mathf.Round((float)Cl * 100f) / 100f;
            guiWingMass = part.mass;
        }

        guiMAC           = (float)MAC;
        guiB_2           = (float)b_2;
        guiMidChordSweep = (float)midChordSweep;
        guiTaperRatio    = (float)taperRatio;
        guiSurfaceArea   = (float)surfaceArea;
        guiAspectRatio   = (float)aspectRatio;

        StartCoroutine(updateAeroDelayed());
    }
 public static double Lerp(double from, double to, double t)
 {
     return(from + (to - from) * MathD.Clamp01(t));
 }
 public static double MoveTowardsAngle(double current, double target, double maxDelta)
 {
     target = current + MathD.DeltaAngle(current, target);
     return(MathD.MoveTowards(current, target, maxDelta));
 }
 public static double PingPong(double t, double length)
 {
     t = MathD.Repeat(t, length * 2d);
     return(length - MathD.Abs(t - length));
 }
 public static double Repeat(double t, double length)
 {
     return(t - MathD.Floor(t / length) * length);
 }
 public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime)
 {
     return(MathD.SmoothDamp(current, current + MathD.DeltaAngle(current, target), ref currentVelocity, smoothTime, maxSpeed, deltaTime));
 }
 public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime)
 {
     return(MathD.SmoothDampAngle(current, target, ref currentVelocity, smoothTime, double.PositiveInfinity, Time.deltaTime));
 }
 public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed)
 {
     return(MathD.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime));
 }
 public static bool Approximately(double a, double b)
 {
     return(MathD.Abs(b - a) < MathD.Max(1E-06d * MathD.Max(MathD.Abs(a), MathD.Abs(b)), Epsilon));
 }
 public static double SmoothStep(double from, double to, double t)
 {
     t = MathD.Clamp01(t);
     t = (-2.0 * t * t * t + 3.0 * t * t);
     return(to * t + from * (1.0 - t));
 }