public static Quaternion Slerp(Quaternion p, Quaternion q, float time, bool useShortCut = false) { var cos = p.Dot(q); var angle = MathDotNet.Acos(cos); if (MathDotNet.Abs(angle) < Quaternion.EPSILONE) { return(p); } var sin = MathDotNet.Sin(angle); var inverseSin = 1f / sin; var coeff0 = MathDotNet.Sin((1f - time) * angle) * inverseSin; var coeff1 = MathDotNet.Sin(time * angle) * inverseSin; if (useShortCut == false || cos >= 0d) { return(p * coeff0 + q * coeff1); } coeff0 = -coeff0; Quaternion temp = p * coeff0 + q * coeff1; var factor = 1d / MathDotNet.Sqrt(temp.Normalized); return(temp * factor); }
public static DQuat slerp(DQuat a, DQuat b, double t) { double dotVal = dot(a, b); double angle = Math.Acos(dotVal); return((a * Math.Sin(angle * (1.0 - t)) + b * Math.Sin(angle * t)) / Math.Sin(angle)); }
public static int Math_ACos(ILuaState luaState) { double rad = luaState.ToNumber(-1) * Mathf.Deg2Rad; luaState.PushNumber((float)Math.Acos(rad)); return(1); }
public static aQuat Slerp(aQuat start, aQuat end, float percent) { var cos = aQuat.DotProduct(start, end); if (cos < 0.0f) { cos = -cos; end = -end; } if ((1.0f - cos) > Single.Epsilon) { var angle = (float)SystemMath.Acos(cos); var vector = new aVec3( (float)SystemMath.Sin(angle * (1.0f - percent)), (float)SystemMath.Sin(angle * percent), (float)SystemMath.Sin(angle) ); return(new aQuat( (start.x * vector.x + end.x * vector.y) / vector.z, (start.y * vector.x + end.y * vector.y) / vector.z, (start.z * vector.x + end.z * vector.y) / vector.z, (start.w * vector.x + end.w * vector.y) / vector.z )); } return(aQuat.Lerp(start, end, percent)); }
/// <summary> /// Returns the 3 'x' solutions to the equation x^3 + a2*x^2 + a1*x + a0 = 0. /// </summary> /// <param name="a0">Constant.</param> /// <param name="a1">Multiplier to x.</param> /// <param name="a2">Multiplier to x^2.</param> /// <param name="returnFirstRoot">if set to <c>true</c> [return first root].</param> /// <returns>System.Double[].</returns> private static double[] cubicCurveRootsNormalized(double a0, double a1, double a2, bool returnFirstRoot = false) { double Q = (3 * a1 - a2.Squared()) / 9d; double R = (9 * a2 * a1 - 27 * a0 - 2 * a2.Cubed()) / 54d; double D = Q.Cubed() + R.Squared(); double aCosRatio = R / Numbers.Sqrt(NMath.Abs(-Q.Cubed())); double x1; if ((returnFirstRoot && D.IsGreaterThanOrEqualTo(0)) || (aCosRatio.IsLessThan(-1) || aCosRatio.IsGreaterThan(1))) { double S = Numbers.CubeRoot(R + Numbers.Sqrt(D)); double T = Numbers.CubeRoot(R - Numbers.Sqrt(D)); x1 = (S + T) - (1 / 3d) * a2; return(new double[] { x1 }); } double theta = NMath.Acos(aCosRatio); x1 = 2 * Numbers.Sqrt((NMath.Abs(-Q))) * NMath.Cos(theta / 3d) - a2 / 3d; double x2 = 2 * Numbers.Sqrt(NMath.Abs(-Q)) * NMath.Cos((theta + 2 * Numbers.Pi) / 3d) - a2 / 3d; double x3 = 2 * Numbers.Sqrt(NMath.Abs(-Q)) * NMath.Cos((theta + 4 * Numbers.Pi) / 3d) - a2 / 3d; return(new double[] { x1, x2, x3 }); }
/// <summary> /// 計算兩個經緯度座標的距離, 單位公里 /// </summary> /// <returns></returns> public static double GetDistance(double lng1, double lat1, double lng2, double lat2) { double r = 6371; // 地球平均半徑, 單位公里 double d = M.Acos( M.Sin(lat1) * M.Sin(lat2) + M.Cos(lat1) * M.Cos(lat2) * M.Cos(lng2 - lng1) ) * r; return(d); }
public static Quaternion Slerp(Quaternion quaternion1, Quaternion quaternion2, float amount) { const float epsilon = 1e-6f; float t = amount; float cosOmega = quaternion1.X * quaternion2.X + quaternion1.Y * quaternion2.Y + quaternion1.Z * quaternion2.Z + quaternion1.W * quaternion2.W; bool flip = false; if (cosOmega < 0.0f) { flip = true; cosOmega = -cosOmega; } float s1, s2; if (cosOmega > (1.0f - epsilon)) { // Too close, do straight linear interpolation. s1 = 1.0f - t; s2 = (flip) ? -t : t; } else { float omega = (float)SM.Acos(cosOmega); float invSinOmega = (float)(1 / SM.Sin(omega)); s1 = (float)SM.Sin((1.0f - t) * omega) * invSinOmega; s2 = (flip) ? (float)-SM.Sin(t * omega) * invSinOmega : (float)SM.Sin(t * omega) * invSinOmega; } Quaternion ans; ans.X = s1 * quaternion1.X + s2 * quaternion2.X; ans.Y = s1 * quaternion1.Y + s2 * quaternion2.Y; ans.Z = s1 * quaternion1.Z + s2 * quaternion2.Z; ans.W = s1 * quaternion1.W + s2 * quaternion2.W; return(ans); }
public static aQuat FromLookAt(aVec3 src, aVec3 dest) { var forward = (dest - src).Normalized; var dot = aVec3.DotProduct(aVec3.UnitZ, forward); if (SystemMath.Abs(dot + 1.0f) < Single.Epsilon) { return(new aQuat(aVec3.UnitY.x, aVec3.UnitY.y, aVec3.UnitY.z, (float)SystemMath.PI)); } else if (SystemMath.Abs(dot - 1.0f) < Single.Epsilon) { return(aQuat.Identity); } var axis = aVec3.CrossProduct(aVec3.UnitZ, forward).Normalized; var angle = (float)SystemMath.Acos(dot); return(FromAxisAngle(axis, angle)); }
/// <summary> /// /// </summary> /// <param name="destination">The position of the destination relative to the turning point</param> /// <param name="radius">The radius of the turning circle</param> /// <param name="turnDirection">If the turn is clockwise or anti clockwise</param> /// <returns></returns> public Angle DetermineTurnEnd(Vector destination, double radius, TurnDirection turnDirection) { Angle angleTowardsDestination = new Angle(destination); Angle tangentAngles = new Angle(Math.Acos(radius / destination.Length)); Angle desiredEndPoint; if (turnDirection == TurnDirection.Clockwise) { desiredEndPoint = angleTowardsDestination + tangentAngles; } else { desiredEndPoint = angleTowardsDestination - tangentAngles; } desiredEndPoint.ReduceAngle(); return(desiredEndPoint); }
private void SetTransformByPoints(PointF controlPoint1, PointF actualPoint1, PointF controlPoint2) { var controlCenter = ControlCenter; var actualPoint2 = GetActualPoint(controlPoint2); var newCenter = new PointF((actualPoint2.X + actualPoint1.X) / 2, (actualPoint2.Y + actualPoint1.Y) / 2); var translateX = newCenter.X - controlCenter.X; var translateY = newCenter.Y - controlCenter.Y; var p1 = new PointF(controlPoint1).Translate(-controlCenter.X, -controlCenter.Y); var p2 = new PointF(actualPoint1).Translate(-translateX, -translateY). Translate(-controlCenter.X, -controlCenter.Y); var cosAngle = (p1.X * p2.X + p1.Y * p2.Y) / SMath.Sqrt((p1.X * p1.X + p1.Y * p1.Y) * (p2.X * p2.X + p2.Y * p2.Y)); cosAngle = SMath.Max(-1, SMath.Min(1, cosAngle)); var angle = Utils.ConvertRadianToDegree(SMath.Acos(cosAngle)); p2.Rotate(-angle); Transform = new Transform(p2.X / p1.X, p2.Y / p1.Y, translateX, translateY, angle); }
public static double Acos(double d) => Math.Acos(d);
static double IFloatingPoint <double> .Acos(double x) => Math.Acos(x);
public static Half Acos(Half x) => (Half)M.Acos(x);
public static double Acos(double value) { return(CSMath.Acos(value) * kRadiansToDegrees); }
public static Angle Acos(Real radians) { return(FromRadians(Math.Acos(radians))); }
/// <summary> /// Finds the inverse cosine, the angle whose cosine is the given ratio. /// </summary> /// <param name="ratio">The cosine of the angle, a number in the range [-1, 1].</param> /// <returns name="angle">The angle whose cosine is the input ratio.</returns> /// <search>acosine,arccosine</search> public static double Acos(double ratio) { return(CSMath.Acos(ratio) * kRadiansToDegrees); }
/// <summary> /// Returns the angle [radians] between the two vectors, which is a value between 0 and +π. /// </summary> /// <param name="vector1">The vector1.</param> /// <param name="vector2">The vector2.</param> /// <param name="tolerance">Tolerance by which a double is considered to be zero or equal.</param> /// <returns>System.Double.</returns> public static double Angle(Vector vector1, Vector vector2, double tolerance = Numbers.ZeroTolerance) { tolerance = Generics.GetTolerance(vector1, vector2, tolerance); return(NMath.Acos(ConcavityCollinearity(vector1, vector2))); }
/// <summary> /// calculate solar position for the entered date, time and /// location. Results are reported in azimuth and elevation /// (in degrees) and cosine of solar zenith angle. /// </summary> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <param name="dateTime"></param> /// <param name="zone">Time offset from UTC</param> private static Sun CalcSun(double latitude, double longitude, DateTime dateTime, int zone) { var result = new Sun(); if ((latitude >= -90) && (latitude < -89.8)) { //"All latitudes between 89.8 and 90 S\n will be set to -89.8." latitude = -89.8; } if ((latitude <= 90) && (latitude > 89.8)) { //"All latitudes between 89.8 and 90 N\n will be set to 89.8." latitude = 89.8; } var jd = JD(dateTime.AddHours(zone)); var jc = JulianCent(jd); var theta = SunDeclination(jc); var etime = EquationOfTime(jc); var eqTime = etime; var solarDec = theta; // in degrees result.eqTime = (Math.Floor(100 * eqTime)) / 100; result.solarDec = (Math.Floor(100 * (solarDec))) / 100; var solarTimeFix = eqTime - 4.0 * longitude + 60.0 * zone; var trueSolarTime = dateTime.TimeOfDay.Hours * 60 + dateTime.TimeOfDay.Minutes + dateTime.TimeOfDay.Seconds / 60 + solarTimeFix; while (trueSolarTime > 1440) { trueSolarTime -= 1440; } var hourAngle = trueSolarTime / 4.0 - 180.0; if (hourAngle < -180) { hourAngle += 360.0; } var haRad = DegreeToRadian(hourAngle); var csz = Math.Sin(DegreeToRadian(latitude)) * Math.Sin(DegreeToRadian(solarDec)) + Math.Cos(DegreeToRadian(latitude)) * Math.Cos(DegreeToRadian(solarDec)) * Math.Cos(haRad); if (csz > 1.0) { csz = 1.0; } else if (csz < -1.0) { csz = -1.0; } var zenith = RadianToDegree(Math.Acos(csz)); var azDenom = (Math.Cos(DegreeToRadian(latitude)) * Math.Sin(DegreeToRadian(zenith))); double azimuth = 0; if (Math.Abs(azDenom) > 0.001) { var azRad = ((Math.Sin(DegreeToRadian(latitude)) * Math.Cos(DegreeToRadian(zenith))) - Math.Sin(DegreeToRadian(solarDec))) / azDenom; if (Math.Abs(azRad) > 1.0) { if (azRad < 0) { azRad = -1.0; } else { azRad = 1.0; } } azimuth = 180.0 - RadianToDegree(Math.Acos(azRad)); if (hourAngle > 0.0) { azimuth = -azimuth; } } else { if (latitude > 0.0) { azimuth = 180.0; } else { azimuth = 0.0; } } if (azimuth < 0.0) { azimuth += 360.0; } double refractionCorrection; var exoatmElevation = 90.0 - zenith; if (exoatmElevation > 85.0) { refractionCorrection = 0.0; } else { var te = Math.Tan(DegreeToRadian(exoatmElevation)); if (exoatmElevation > 5.0) { refractionCorrection = 58.1 / te - 0.07 / (te * te * te) + 0.000086 / (te * te * te * te * te); } else if (exoatmElevation > -0.575) { refractionCorrection = 1735.0 + exoatmElevation * (-518.2 + exoatmElevation * (103.4 + exoatmElevation * (-12.79 + exoatmElevation * 0.711))); } else { refractionCorrection = -20.774 / te; } refractionCorrection = refractionCorrection / 3600.0; } var solarZen = zenith - refractionCorrection; if (solarZen < 108.0) { // astronomical twilight result.azimuth = (Math.Floor(100 * azimuth)) / 100; result.elevation = (Math.Floor(100 * (90.0 - solarZen))) / 100; if (solarZen < 90.0) { result.coszen = (Math.Floor(10000.0 * (Math.Cos(DegreeToRadian(solarZen))))) / 10000.0; } else { result.coszen = 0.0; } } else { result.dark = true; } return(result); }
/// <summary> /// Returns the angle in degrees between from and to. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <returns></returns> public static float Angle(Vector3 from, Vector3 to) { float dot = Dot(from.normalized, to.normalized); return((float)Maths.Acos((dot) * (180.0 / 3.141592653))); }
public static double Acos(double d) { return(Math.Acos(d)); }