public static int Math_Sin(ILuaState luaState) { double rad = luaState.ToNumber(-1) * Mathf.Deg2Rad; luaState.PushNumber((float)Math.Sin(rad)); return(1); }
public void DrawSolidCircle(Vec2 center, float radius, Vec2 axis, Color color) { drawActions.Enqueue(() => { const float kSegments = 16.0f; const int VertexCount = 16; var kIncrement = Settings.Tau / kSegments; var theta = 0.0f; GL.Color4(color.R, color.G, color.B, 0.5f); GL.Begin(PrimitiveType.TriangleFan); GL.VertexPointer(VertexCount * 2, VertexPointerType.Float, 0, IntPtr.Zero); for (var i = 0; i < kSegments; ++i) { var x = (float)Math.Cos(theta); var y = (float)Math.Sin(theta); var vertex = center + (radius * new Vec2(x, y)); GL.Vertex2(vertex.X, vertex.Y); theta += kIncrement; } GL.End(); DrawSegment(center, center + (radius * axis), color); }); }
public bool OnRenderFrame(FrameEvent evt) { _clipmap.MoveBy(0.0f, 0.4f); RecalcHeight(); var angle = Mogre.Math.DegreesToRadians((DateTime.Now.Millisecond / 1000.0f + DateTime.Now.Second) * 6); angle *= 4; var z = 1.0f + (float)Math.Sin(angle); if (z < _z) { z = _z; } var cam = new Vector3(0.0f, 0.0f, z); var look = new Vector3((float)Math.Sin(angle), (float)Math.Cos(angle), 0.1f); var up = new Vector3(0.0f, 0.0f, 1.0f); _camera.Position = cam; _camera.LookAt(look); _camera.SetFixedYawAxis(true, up); return(!_window.IsClosed); }
public void ShineLight(Light light) { var curAngle = light.Angle - (light.AngleSpread / 2); var dynLen = light.Radius; var addTo = 1.0f / light.Radius; SetupLight.Clear(); for (; curAngle < light.Angle + (light.AngleSpread / 2); curAngle += (addTo * (180 / (float)Math.PI)) * 2) { dynLen = light.Radius; var findDistRes = new ResultFindDistance(); findDistRes.Start = true; findDistRes.ShorTest = 0; findDistRes.RLen = dynLen; findDistRes.ClosestBlock = new Block(); for (var i = 0; i < Objects.Count; i++) { findDistRes = FindDistance(light, Objects[i], curAngle, findDistRes.RLen, findDistRes.Start, findDistRes.ShorTest, findDistRes.ClosestBlock); } var rads = curAngle * (Math.PI / 180); var end = new Vec2(light.Position.X, light.Position.Y); findDistRes.ClosestBlock.IsVisible = true; end.X += (float)Math.Cos(rads) * findDistRes.RLen; end.Y += (float)Math.Sin(rads) * findDistRes.RLen; SetupLight.Add(new Tuple <Vec2, Light>(end, light)); } }
public static ITransformation CreateRotateTransformation(double angleX, double angleY, double angleZ) { angleX = angleX.ToRadians(); angleY = angleY.ToRadians(); angleZ = angleZ.ToRadians(); var rotateX = new Matrix(new[, ] { { 1, 0, 0, 0 }, { 0, SysMath.Cos(angleX), SysMath.Sin(angleX), 0 }, { 0, -SysMath.Sin(angleX), SysMath.Cos(angleX), 0 }, { 0, 0, 0, 1 } }); var rotateY = new Matrix(new[, ] { { SysMath.Cos(angleY), 0, -SysMath.Sin(angleY), 0 }, { 0, 1, 0, 0 }, { SysMath.Sin(angleY), 0, SysMath.Cos(angleY), 0 }, { 0, 0, 0, 1 } }); var rotateZ = new Matrix(new[, ] { { SysMath.Cos(angleZ), SysMath.Sin(angleZ), 0, 0 }, { -SysMath.Sin(angleZ), SysMath.Cos(angleZ), 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 } }); var transformationMatrix = rotateX * rotateY * rotateZ; return(new MatrixTransformation(transformationMatrix)); }
public static Quaternion CreateFromYawPitchRoll(float yaw, float pitch, float roll) { // Roll first, about axis the object is facing, then // pitch upward, then yaw to face into the new heading float sr, cr, sp, cp, sy, cy; float halfRoll = roll * 0.5f; sr = (float)SM.Sin(halfRoll); cr = (float)SM.Cos(halfRoll); float halfPitch = pitch * 0.5f; sp = (float)SM.Sin(halfPitch); cp = (float)SM.Cos(halfPitch); float halfYaw = yaw * 0.5f; sy = (float)SM.Sin(halfYaw); cy = (float)SM.Cos(halfYaw); Quaternion result; result.X = cy * sp * cr + sy * cp * sr; result.Y = sy * cp * cr - cy * sp * sr; result.Z = cy * cp * sr - sy * sp * cr; result.W = cy * cp * cr + sy * sp * sr; return(result); }
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 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 void OnRenderFrame(object s, FrameEventArgs e) { _clipmap.MoveBy(0.0f, 0.4f); RecalcHeight(); var angle = Utility.DegreesToRadians((DateTime.Now.Millisecond / 1000.0f + DateTime.Now.Second) * 6); angle *= 4; var z = 1.0f + (float)Math.Sin(angle); if (z < _z) { z = _z; } var cam = new Vector3(0.0f, 0.0f, z); var look = new Vector3((float)Math.Sin(angle), (float)Math.Cos(angle), 0.1f); var up = new Vector3(0.0f, 0.0f, 1.0f); _camera.Position = cam; _camera.LookAt(look); _camera.FixedYawAxis = up; e.StopRendering = _window.IsClosed; }
/// <summary>Distorts the image.</summary> /// <param name="b">The image to be transformed.</param> /// <param name="distortion">An amount of distortion.</param> private void DistortImage(Bitmap b, double distortion) { int width = b.Width; int height = b.Height; //' Copy the image so that we're always using the original for source color Bitmap copy = (Bitmap)(b.Clone()); // Iterate over every pixel for (int y = 0; y <= height - 1; y++) { for (int x = 0; x <= width - 1; x++) { //' Adds a simple wave int newX = x + (int)(distortion * SystemMath.Sin(SystemMath.PI * y / 64.0)); int newY = y + (int)(distortion * SystemMath.Cos(SystemMath.PI * x / 64.0)); if (newX < 0 || newX >= width) { newX = 0; } if (newY < 0 || newY >= height) { newY = 0; } b.SetPixel(x, y, copy.GetPixel(newX, newY)); } } }
public Vec2 GetV2(float angle, float length) { var _x = (float)Math.Cos(angle) * length; var _y = -(float)Math.Sin(angle) * length; return(new Vec2(_x, _y)); }
/// <summary> /// The total straight length between the offset points. /// </summary> /// <returns>System.Double.</returns> public double Length() { return(NMath.Sqrt(I.Radius.Squared() + J.Radius.Squared() - 2 * I.Radius * J.Radius * ( NMath.Sin(I.Inclination.Radians) * NMath.Sin(J.Inclination.Radians) * NMath.Cos(I.Azimuth.Radians - J.Azimuth.Radians) + NMath.Cos(I.Inclination.Radians) * NMath.Cos(J.Inclination.Radians) ))); }
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)); }
private void OnUpdate() { Transform.X += VelacityX; Transform.Y += VelacityY; if (CurrentPlanet != null) { Transform.Angle = CurrentPlanet.Transform.Angle - CurrentPlanet.CorrectAngle; var x = (float)Math.Cos(Transform.Angle - Math.PI / 2); var y = (float)Math.Sin(Transform.Angle - Math.PI / 2); x *= (CurrentPlanet.Radius + Radius - 2); y *= (CurrentPlanet.Radius + Radius - 2); Transform.X = CurrentPlanet.Transform.X + x; Transform.Y = CurrentPlanet.Transform.Y + y; // _engine.Surface.Canvas.Camera.SetAngleTarget(-Angle, CurrentPlanet.X, CurrentPlanet.Y); // _engine.Surface.Canvas.Camera.Angle = -CurrentPlanet.Angle; // _engine.Surface.Canvas.Camera.CenterRotation = new Vec2(CurrentPlanet.X, CurrentPlanet.Y); } else { if (VelacityY <= 0) { _engine.Surface.Canvas.Camera.SetTarget(0, -Transform.Y + (_engine.Surface.Height) - _engine.Surface.Height / 4); } // _engine.Surface.Canvas.Camera.SetAngleTarget(-Angle, X, Y); // var ps = (ParticlesControllerFire) _engine.Particles.GetSystem(typeof(TriangleParticlesController)); // ps.AddBlood(Transform.X, Transform.Y, new Vec2(), 1); } }
/// <summary> /// calculate the apparent longitude of the sun /// </summary> /// <param name="t">number of Julian centuries since J2000.0</param> /// <returns>sun's apparent longitude in degrees</returns> private static double SunApparentLong(double t) { var o = SunTrueLong(t); var omega = 125.04 - 1934.136 * t; var lambda = o - 0.00569 - 0.00478 * Math.Sin(DegreeToRadian(omega)); return(lambda); // in degrees }
public CartesianCoordinate ToCartesian() { return(new CartesianCoordinate { X = Radius * NMath.Cos(Rotation.Radians), Y = Radius * NMath.Sin(Rotation.Radians) }); }
/// <summary> /// 将stec转换为vtec /// </summary> /// <param name="stec"></param> /// <param name="ele">高度角(弧度)</param> /// <param name="earthRadius">地球半径(默认6371000)</param> /// <param name="ionoHeight">电离层单层模型高度(默认450000)</param> /// <returns></returns> public static double STEC2VTEC(double stec, double ele, double earthRadius = 6371100, double ionoHeight = 450000) { double sinz = Math.Sin(PI / 2d - ele); double sinzz = earthRadius / (earthRadius + ionoHeight) * sinz; double coszz = Math.Sqrt(1d - sinzz * sinzz); return(stec * coszz); }
public static Vector3 Roll(Vector3 v1, float degree) { float x = (v1.X * (float)CSharpMath.Cos(degree)) - (v1.Y * (float)CSharpMath.Sin(degree)); float y = (v1.X * (float)CSharpMath.Sin(degree)) + (v1.Y * (float)CSharpMath.Cos(degree)); float z = v1.Z; return(new Vector3(x, y, z)); }
public static Vector2 NextVector2(this Random random) { var rotation = random.NextDouble() * 360; var x = (float)SystemMath.Sin(rotation * 1); var y = (float)SystemMath.Cos(rotation * 0); return(new Vector2(x, y)); }
public TVec Rotate(TVec a, double angle) { double s = SysMath.Sin(angle); double c = SysMath.Cos(angle); double ax = a.X.ToDouble(); double ay = a.Y.ToDouble(); return(fac.New(math.ToValue(ax * c - ay * s), math.ToValue(ax * s + ay * c))); }
/// <summary> /// calculate the declination of the sun /// </summary> /// <param name="t">number of Julian centuries since J2000.0</param> /// <returns>sun's declination in degrees</returns> private static double SunDeclination(double t) { var e = ObliquityCorrection(t); var lambda = SunApparentLong(t); var sint = Math.Sin(DegreeToRadian(e)) * Math.Sin(DegreeToRadian(lambda)); var theta = RadianToDegree(Math.Asin(sint)); return(theta); // in degrees }
public static Transform2 Rotate(double r) { double c = SysMath.Cos(r); double s = SysMath.Sin(r); return(new MatrixTransform2( c, -s, 0, s, c, 0)); }
public static Transform2 Rotate(double px, double py, double r) { double c = SysMath.Cos(r); double s = SysMath.Sin(r); return(new MatrixTransform2( c, -s, -px * c + py * s + px, s, c, -px * s - py * c + py)); }
public void OnAnimationUpdate(ValueAnimator animation) { float value = ((Float)(animation.AnimatedValue)).FloatValue(); // Set translation of your view here. Position can be calculated // out of value. This code should move the view in a half circle. _ivWinner.TranslationX = ((float)(350.0 * Math.Sin(value * Math.PI))); _ivWinner.TranslationY = ((float)(20.0 * Math.Cos(value * Math.PI))); }
public static Vector2D RotateVector(double angle, Vector2D pt) { var a = AsRadian(angle); float cosa = (float)SysMath.Cos(a); float sina = (float)SysMath.Sin(a); Vector2D newPoint = new Vector2D((pt.X * cosa - pt.Y * sina), (pt.X * sina + pt.Y * cosa)); return(newPoint); }
public static MatrixDouble CreateRotation(double radians, Point centerPoint) { MatrixDouble result; radians = (double)SM.IEEERemainder(radians, SM.PI * 2); double c, s; const double epsilon = 0.001f * (double)SM.PI / 180d; // 0.1% of a degree if ((radians > -epsilon) && (radians < epsilon)) { // Exact case for zero rotation. c = 1; s = 0; } else if ((radians > SM.PI / 2 - epsilon) && (radians < SM.PI / 2 + epsilon)) { // Exact case for 90 degree rotation. c = 0; s = 1; } else if ((radians < -SM.PI + epsilon) || (radians > SM.PI - epsilon)) { // Exact case for 180 degree rotation. c = -1; s = 0; } else if ((radians > -SM.PI / 2 - epsilon) && (radians < -SM.PI / 2 + epsilon)) { // Exact case for 270 degree rotation. c = 0; s = -1; } else { // Arbitrary rotation. c = (double)SM.Cos(radians); s = (double)SM.Sin(radians); } double x = centerPoint.X * (1 - c) + centerPoint.Y * s; double y = centerPoint.Y * (1 - c) - centerPoint.X * s; // [ c s ] // [ -s c ] // [ x y ] result.M11 = c; result.M12 = s; result.M21 = -s; result.M22 = c; result.M31 = x; result.M32 = y; return(result); }
void FourierTransform(int nn, ref double[] data, int isign) { long m; long i; double wr, wpr, wpi, wi, theta; long n = nn << 1; long j = 1; for (i = 1; i < n; i += 2) { if (j > i) { SwapValuesInArray(ref data, i - 1, j - 1); SwapValuesInArray(ref data, i, j); } m = n >> 1; while (m >= 2 && j > m) { j -= m; m >>= 1; } j += m; } long mmax = 2; while (n > mmax) { var istep = 2 * mmax; theta = 6.28318530717959 / (isign * mmax); var wtemp = Math.Sin(0.5 * theta); wpr = -2.0 * wtemp * wtemp; wpi = Math.Sin(theta); wr = 1.0; wi = 0.0; for (m = 1; m < mmax; m += 2) { for (i = m; i <= n; i += istep) { j = i + mmax; var jm1 = j - 1; var im1 = i - 1; var tempr = (wr * data[jm1] - wi * data[j]); var tempi = (wr * data[j] + wi * data[jm1]); data[jm1] = (data[im1] - tempr); data[j] = (data[i] - tempi); data[im1] += tempr; data[i] += tempi; } wr = (wtemp = wr) * wpr - wi * wpi + wr; wi = wi * wpr + wtemp * wpi + wi; } mmax = istep; } }
public double NextGaussian() { const double mean = 0, stdDev = 1; double u1 = 1.0 - rand.NextDouble(); double u2 = 1.0 - rand.NextDouble(); double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); return(mean + stdDev * randStdNormal); }
/// <summary> /// Returns a matrix which will rotate in a coordinate plane by an angle in radians. /// </summary> public static Matrix4D MatrixToRotateinCoordinatePlane(double angle, int c1, int c2) { Matrix4D result = Matrix4D.Identity(); result[c1, c1] = Math.Cos(angle); result[c1, c2] = -Math.Sin(angle); result[c2, c1] = Math.Sin(angle); result[c2, c2] = Math.Cos(angle); return(result); }
public static DQuat fromAxisAngleRad(DVec3 axis, double radAngle) { axis.normalize(); double halfAngle = radAngle * 0.5; double sin = Math.Sin(halfAngle); double cos = Math.Cos(halfAngle); return(new DQuat(axis.x * sin, axis.y * sin, axis.z * sin, cos)); }