Example #1
0
        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);
            }
        }
Example #2
0
        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);
            });
        }
Example #3
0
        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);
        }
Example #4
0
        public void DrawSolidCircle(Vec2 center, float radius, Vec2 axis, Color color)
        {
            drawActions.Enqueue(() =>
            {
                const float K_SEGMENTS = 16.0f;
                const int VERTEX_COUNT = 16;
                var kIncrement         = 2.0f * Settings.Pi / K_SEGMENTS;
                var theta = 0.0f;

                GL.Color4(color.R, color.G, color.B, 0.5f);
                GL.Begin(PrimitiveType.TriangleFan);

                GL.VertexPointer(VERTEX_COUNT * 2, VertexPointerType.Float, 0, IntPtr.Zero);

                for (var i = 0; i < K_SEGMENTS; ++i)
                {
                    var v = center + radius * new Vec2((float)Math.Cos(theta), (float)Math.Sin(theta));
                    GL.Vertex2(v.X, v.Y);
                    theta += kIncrement;
                }

                GL.End();

                DrawSegment(center, center + radius * axis, color);
            });
        }
Example #5
0
        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);
        }
        /// <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 });
        }
Example #7
0
    public static int Math_Cos(ILuaState luaState)
    {
        double rad = luaState.ToNumber(-1) * Mathf.Deg2Rad;

        luaState.PushNumber((float)Math.Cos(rad));
        return(1);
    }
Example #8
0
        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));
            }
        }
Example #9
0
        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));
        }
Example #10
0
        /// <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));
                }
            }
        }
Example #11
0
        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;
        }
        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));
        }
Example #13
0
 /// <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)
                           )));
 }
Example #14
0
        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));
        }
Example #15
0
        /// <summary>
        /// calculate the corrected obliquity of the ecliptic
        /// </summary>
        /// <param name="t">number of Julian centuries since J2000.0</param>
        /// <returns>corrected obliquity in degrees</returns>
        private static double ObliquityCorrection(double t)
        {
            var e0    = MeanObliquityOfEcliptic(t);
            var omega = 125.04 - 1934.136 * t;
            var e     = e0 + 0.00256 * Math.Cos(DegreeToRadian(omega));

            return(e); // in degrees
        }
Example #16
0
        /// <summary>
        /// calculate the distance to the sun in AU
        /// </summary>
        /// <param name="t">number of Julian centuries since J2000.0</param>
        /// <returns> sun radius vector in AUs</returns>
        private static double SunRadVector(double t)
        {
            var v = SunTrueAnomaly(t);
            var e = EccentricityEarthOrbit(t);
            var R = (1.000001018 * (1 - e * e)) / (1 + e * Math.Cos(DegreeToRadian(v)));

            return(R); // in AUs
        }
Example #17
0
 public CartesianCoordinate ToCartesian()
 {
     return(new CartesianCoordinate
     {
         X = Radius * NMath.Cos(Rotation.Radians),
         Y = Radius * NMath.Sin(Rotation.Radians)
     });
 }
Example #18
0
        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)));
        }
Example #19
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));
        }
Example #20
0
        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));
        }
Example #21
0
        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));
        }
Example #22
0
        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)));
        }
Example #23
0
        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);
        }
Example #24
0
        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);
        }
Example #25
0
        /// <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);
        }
Example #26
0
    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));
    }
Example #27
0
        internal static List <IHitTarget> GenerateTwoSphereScene()
        {
            var returnList = new List <IHitTarget>();

            var radius = Math.Cos(Math.PI / 4);

            returnList.Add(new Sphere(new Vec3(-radius, 0, -1), radius, new Lambertian(new Color(0, 0, 1))));
            returnList.Add(new Sphere(new Vec3(radius, 0, -1), radius, new Lambertian(new Color(1, 0, 0))));

            return(returnList);
        }
Example #28
0
        /// <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);
        }
Example #29
0
        /// <summary>
        /// Derivada iesima de la integral del Fresnel (i = 1..5).
        /// </summary>
        public static void DFresnel(double z, out double sz, out double cz, int i = 1)
        {
            double zz = (SysMath.PI / 2) * z * z;
            double s  = SysMath.Sin(zz);
            double c  = SysMath.Cos(zz);

            switch (i)
            {
            case 1:
                sz = s;
                cz = c;
                return;

            case 2:
            {
                double PI_z = SysMath.PI * z;

                sz = PI_z * c;
                cz = -PI_z * s;
                return;
            }

            case 3:
            {
                double PI_e2_z2 = PI_e2 * z * z;

                sz = SysMath.PI * c - PI_e2_z2 * s;
                cz = -SysMath.PI * s - PI_e2_z2 * c;
                return;
            }

            case 4:
            {
                double PI_e2_3_z = 3 * PI_e2 * z;
                double PI_e3_z3  = PI_e3 * z * z * z;

                sz = -PI_e2_3_z * s - PI_e3_z3 * c;
                cz = -PI_e2_3_z * c + PI_e3_z3 * s;
                return;
            }

            case 5:
            {
                double PI_e2_3    = 3 * PI_e2;
                double PI_e3_6_z2 = 6 * PI_e3 * z * z;
                double PI_e4_z4   = PI_e4 * z * z * z * z;

                sz = -PI_e2_3 * s - PI_e3_6_z2 * c + PI_e4_z4 * s;
                cz = -PI_e2_3 * c + PI_e3_6_z2 * s + PI_e4_z4 * c;
                return;
            }
            }
            throw new NotImplementedException();
        }
Example #30
0
        public Vec2 GetAngleVector(float angle, float lenth)
        {
            var a = angle / 180.0f * (float)Math.PI;
            var v = new Vec2
            {
                X = (float)Math.Sin(a) * lenth,
                Y = -(float)Math.Cos(a) * lenth
            };

            return(v);
        }