示例#1
0
        private static int Math_Random(ILuaState lua)
        {
            double r = RandObj.NextDouble();

            switch (lua.GetTop())
            {
            case 0:                     // no argument
                lua.PushNumber(r);
                break;

            case 1:
            {
                double u = lua.L_CheckNumber(1);
                lua.L_ArgCheck(1.0 <= u, 1, "interval is empty");
                lua.PushNumber(Math.Floor(r * u) + 1.0);                         // int in [1, u]
                break;
            }

            case 2:
            {
                double l = lua.L_CheckNumber(1);
                double u = lua.L_CheckNumber(2);
                lua.L_ArgCheck(l <= u, 2, "interval is empty");
                lua.PushNumber(Math.Floor(r * (u - l + 1)) + l);                     // int in [l, u]
                break;
            }

            default: return(lua.L_Error("wrong number of arguments"));
            }
            return(1);
        }
示例#2
0
        public float LinearInterpolate(Vector2 position, OverworldField fieldType)
        {
            float x  = position.X;
            float y  = position.Y;
            float x1 = (int)MathFunctions.Clamp((float)Math.Ceiling(x), 0, Map.GetLength(0) - 2);
            float y1 = (int)MathFunctions.Clamp((float)Math.Ceiling(y), 0, Map.GetLength(1) - 2);
            float x2 = (int)MathFunctions.Clamp((float)Math.Floor(x), 0, Map.GetLength(0) - 2);
            float y2 = (int)MathFunctions.Clamp((float)Math.Floor(y), 0, Map.GetLength(1) - 2);

            if (Math.Abs(x1 - x2) < 0.5f)
            {
                x1 = x1 + 1;
            }

            if (Math.Abs(y1 - y2) < 0.5f)
            {
                y1 = y1 + 1;
            }

            float q11 = Map[(int)x1, (int)y1].GetValue(fieldType);
            float q12 = Map[(int)x1, (int)y2].GetValue(fieldType);
            float q21 = Map[(int)x2, (int)y1].GetValue(fieldType);
            float q22 = Map[(int)x2, (int)y2].GetValue(fieldType);

            return(MathFunctions.LinearCombination(x, y, x1, y1, x2, y2, q11, q12, q21, q22));
        }
示例#3
0
        public static Vector2 WorldToOverworldRemainder(Vector2 World)
        {
            var x = 16.0f * Math.Floor(World.X / 16.0f);
            var y = 16.0f * Math.Floor(World.Y / 16.0f);

            return(new Vector2((float)(World.X - x), (float)(World.Y - y)));
        }
示例#4
0
        /// <summary>
        /// <para>Reduce {@code |a - offset|} to the primary interval
        /// {@code [0, |period|)}.</para>
        ///
        /// <para>Specifically, the value returned is <br/>
        /// {@code a - |period| * floor((a - offset) / |period|) - offset}.</para>
        ///
        /// <para>If any of the parameters are {@code NaN} or infinite, the result is
        /// {@code NaN}.</para>
        /// </summary>
        /// <param name="a"> Value to reduce. </param>
        /// <param name="period"> Period. </param>
        /// <param name="offset"> Value that will be mapped to {@code 0}. </param>
        /// <returns> the value, within the interval {@code [0 |period|)},
        /// that corresponds to {@code a}. </returns>
        public static double Reduce(double a, double period, double offset)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double p = FastMath.abs(period);
            double p = FastMath.Abs(period);

            return(a - p * FastMath.Floor((a - offset) / p) - offset);
        }
示例#5
0
        internal static Color FromHSV(float hue, float saturation, float value)
        {
            byte r = 0, g = 0, b = 0;

            if (saturation == 0)
            {
                r = g = b = (byte)(value * 255.0f + 0.5f);
            }
            else
            {
                float h = (hue - (byte)Math.Floor(hue)) * 6.0f;
                float f = h - (byte)Math.Floor(h);
                float p = value * (1.0f - saturation);
                float q = value * (1.0f - saturation * f);
                float t = value * (1.0f - (saturation * (1.0f - f)));
                switch ((int)h)
                {
                case 0:
                    r = (byte)(value * 255.0f + 0.5f);
                    g = (byte)(t * 255.0f + 0.5f);
                    b = (byte)(p * 255.0f + 0.5f);
                    break;

                case 1:
                    r = (byte)(q * 255.0f + 0.5f);
                    g = (byte)(value * 255.0f + 0.5f);
                    b = (byte)(p * 255.0f + 0.5f);
                    break;

                case 2:
                    r = (byte)(p * 255.0f + 0.5f);
                    g = (byte)(value * 255.0f + 0.5f);
                    b = (byte)(t * 255.0f + 0.5f);
                    break;

                case 3:
                    r = (byte)(p * 255.0f + 0.5f);
                    g = (byte)(q * 255.0f + 0.5f);
                    b = (byte)(value * 255.0f + 0.5f);
                    break;

                case 4:
                    r = (byte)(t * 255.0f + 0.5f);
                    g = (byte)(p * 255.0f + 0.5f);
                    b = (byte)(value * 255.0f + 0.5f);
                    break;

                case 5:
                    r = (byte)(value * 255.0f + 0.5f);
                    g = (byte)(p * 255.0f + 0.5f);
                    b = (byte)(q * 255.0f + 0.5f);
                    break;
                }
            }

            return(new Color(r, g, b));
        }
示例#6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShapeRegion"/> class.
        /// </summary>
        /// <param name="shape">The shape.</param>
        public ShapeRegion(IPath shape)
        {
            this.Shape = shape.AsClosedPath();
            int left = (int)MathF.Floor(shape.Bounds.Left);
            int top  = (int)MathF.Floor(shape.Bounds.Top);

            int right  = (int)MathF.Ceiling(shape.Bounds.Right);
            int bottom = (int)MathF.Ceiling(shape.Bounds.Bottom);

            this.Bounds = Rectangle.FromLTRB(left, top, right, bottom);
        }
示例#7
0
        public override void animate()
        {
            if (!enabled)
            {
                return;
            }
            frameInit(); // percent, percentAfter, delta and enabled are now set
            uint frame = (uint)Math.Floor(percentAfter * (state.NumFrames - 1));

            // Console.WriteLine(System.Math.Ceiling(percentAfter * state.NumFrames));
            state.CurrentFrame = frame;
            startNewLoop       = false;
        }
示例#8
0
        private static int Math_Modf(ILuaState lua)
        {
            double d = lua.L_CheckNumber(1);
            double c;

            if (d < 0)
            {
                c = Math.Ceiling(d);
            }
            else
            {
                c = Math.Floor(d);
            }
            lua.PushNumber(c);
            lua.PushNumber(d - c);
            return(2);
        }
示例#9
0
        private void add(double d) {
            long value = 0;

            if (d > 0)
                value = (long)SM.Floor(d);
            else
                value = (long)SM.Ceiling(d);
            accumulated += (d - value);

            if (accumulated >= 1) {
                ++value;
                --accumulated;
            } else if (accumulated <= -1) {
                --value;
                ++accumulated;
            }
            Total += value;
        }
示例#10
0
        public LevelTileTypes TickCollisions()
        {
            var levelGrid = LevelConstants.LEVEL_GRID;
            var blockSize = levelGrid.Size;

            var(centerLevelC, centerLevelR) =
                ((int)CMath.Floor(this.PlayerRigidbody.CenterX / blockSize),
                 (int)CMath.Floor(this.PlayerRigidbody.CenterY / blockSize));

            var windowWidth =
                (int)(1 +
                      CMath.Ceiling(PlayerConstants.HSIZE / blockSize) +
                      1);
            var windowHeight =
                (int)(1 +
                      CMath.Ceiling(PlayerConstants.VSIZE / blockSize) +
                      1);

            var relativeWindowHStart = (int)-CMath.Floor(windowWidth / 2d);
            var relativeWindowVStart = (int)-CMath.Floor(windowHeight / 2d);

            var collidedTypes = LevelTileTypes.EMPTY;

            for (var r = 0; r < windowHeight; ++r)
            {
                for (var c = 0; c < windowWidth; ++c)
                {
                    var(relativeCacheC, relativeCacheR) =
                        (relativeWindowHStart + c, relativeWindowVStart + r);
                    var(absoluteLevelC, absoluteLevelR) = (centerLevelC + relativeCacheC,
                                                           centerLevelR +
                                                           relativeCacheR);
                    var tile = levelGrid.GetAtIndex(absoluteLevelC, absoluteLevelR);
                    if (tile != LevelTileTypes.EMPTY)
                    {
                        var blockPosition = (absoluteLevelC * blockSize,
                                             absoluteLevelR *blockSize);
                        collidedTypes |= this.CheckCollisions_(tile, blockPosition);
                    }
                }
            }

            return(collidedTypes);
        }
示例#11
0
            public Bound(Finger finger)
            {
                int value = (int)finger;

                if (System.Math.Abs(value) < 10)
                {
                    left  = value;
                    right = value;
                }
                else
                {
                    int minus = value < 0 ? -1 : 1;
                    value = System.Math.Abs(value);

                    int first  = (int)Math.Floor(value / 10f) * minus;
                    int second = value % 10 * minus;

                    left  = Math.Min(first, second);
                    right = Math.Max(first, second);
                }
            }
示例#12
0
        /// <summary>
        ///     Ajusta al intervalo closed [cut, cut + 2*PI] o open [cut, cut + 2*PI).
        /// </summary>
        /// <param name="rad">Angulo en radianes.</param>
        /// <param name="cut">Angulo inicial de la rama.</param>
        /// <param name="open">Indica si queremos [corte, corte+2*PI).</param>
        /// <returns>Angulo en radianes entre [corte, corte+2*PI] o [corte, corte+2*PI).</returns>
        public static double EnsureBranch(double rad, double cut, bool open = false)
        {
            double rightEnd = cut + twoPi;

            if (rad < cut || rad > rightEnd)
            {
                rad -= SysMath.Floor((rad - cut) / twoPi) * twoPi;
            }

            // Si es abierto, se trata el extremo derecho.
            if (open)
            {
                if (rad == rightEnd)
                {
                    rad = cut;
                }
                Contract.Assert(rad >= cut && rad < rightEnd);
                return(rad);
            }

            Contract.Assert(rad >= cut && rad <= rightEnd);
            return(rad);
        }
示例#13
0
 public static double Ldexp(object self, double x, double y)
 {
     return(x * SM.Pow(2, SM.Floor(y)));
 }
示例#14
0
 /// <summary>
 /// Normalize an angle in a 2&pi; wide interval around a center value.
 /// <para>This method has three main uses:</para>
 /// <ul>
 ///   <li>normalize an angle between 0 and 2&pi;:<br/>
 ///       {@code a = MathUtils.normalizeAngle(a, FastMath.PI);}</li>
 ///   <li>normalize an angle between -&pi; and +&pi;<br/>
 ///       {@code a = MathUtils.normalizeAngle(a, 0.0);}</li>
 ///   <li>compute the angle between two defining angular positions:<br>
 ///       {@code angle = MathUtils.normalizeAngle(end, start) - start;}</li>
 /// </ul>
 /// <para>Note that due to numerical accuracy and since &pi; cannot be represented
 /// exactly, the result interval is <em>closed</em>, it cannot be half-closed
 /// as would be more satisfactory in a purely mathematical view.</para> </summary>
 /// <param name="a"> angle to normalize </param>
 /// <param name="center"> center of the desired 2&pi; interval for the result </param>
 /// <returns> a-2k&pi; with integer k and center-&pi; &lt;= a-2k&pi; &lt;= center+&pi;
 /// @since 1.2 </returns>
 public static double NormalizeAngle(double a, double center)
 {
     return(a - TWO_PI * FastMath.Floor((a + FastMath.PI - center) / TWO_PI));
 }
示例#15
0
 private static int Math_Floor(ILuaState lua)
 {
     lua.PushNumber(Math.Floor(lua.L_CheckNumber(1)));
     return(1);
 }
示例#16
0
 /// <returns>
 /// Time of the last fractional beat in MS
 /// </returns>
 public double getLastBeatTime(double frac = 1, double offset = 0)
 {
     return(this.beatEpoch + Math.Floor(this.getFractionalBeat(frac)) * frac * this.mspb);
 }
示例#17
0
 //True modulo, not remainder
 float TrueModf(float a, float b)
 {
     return(a - b * (float)Math.Floor(a / b));
 }
示例#18
0
        protected override void Draw(GameTime gameTime)
        {
            //Clear our background
            GraphicsDevice.Clear(new Color(75, 75, 75));

            //Begin sprite batch : We need to clamp as point and blend with alpha
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp);

            //Draw chunks based on current camera's position clamped to chunk grid
            int xGrid = (int)Math.Ceiling((PlayerPositionWorld().X / (float)WorldMetrics.ChunkSizeX) / (float)WorldMetrics.SpriteSize);
            int yGrid = (int)Math.Ceiling((PlayerPositionWorld().Y / (float)WorldMetrics.ChunkSizeY) / (float)WorldMetrics.SpriteSize);


            int drawingCount   = 0;
            int chunkCount     = 0;
            int fullBlockCount = 0;

            //Loop through all nearest chunks
            for (int xChunk = xGrid - toDrawChunksThreshold; xChunk <= xGrid + toDrawChunksThreshold; xChunk++)
            {
                for (int yChunk = yGrid - toDrawChunksThreshold; yChunk <= yGrid + toDrawChunksThreshold; yChunk++)
                {
                    //Check for chunk availability
                    if (xChunk < 0 || yChunk < 0 || xChunk >= WorldMetrics.ChunkCountX || yChunk >= WorldMetrics.ChunkCountY)
                    {
                        continue;
                    }
                    chunkCount++;
                    //Loop through chunk's blocks
                    for (int x = 0; x < WorldMetrics.ChunkSizeX; x++)
                    {
                        for (int y = 0; y < WorldMetrics.ChunkSizeY; y++)
                        {
                            //Calculate block's position
                            int     blockX    = xChunk * WorldMetrics.ChunkSizeX + x;
                            int     blockY    = yChunk * WorldMetrics.ChunkSizeY + y;
                            Vector2 screenPos = new Vector2(blockX, blockY) * WorldMetrics.SpriteSize;
                            fullBlockCount++;
                            if (Vector2.DistanceSquared(screenPos, PlayerPositionWorld()) <= ScreenWidthSquared)
                            {
                                ////Calculate block's color
                                //float PlayerToBlockDistance = Vector2.DistanceSquared(PlayerPositionGrid(), new Vector2(blockX, blockY));
                                //float ColorValue = (300f - PlayerToBlockDistance) / 300f;
                                Color color = Color.White;
                                //if (ColorValue > 0f)
                                //{
                                //    color = new Color(ColorValue, ColorValue, ColorValue);
                                //}
                                //else
                                //{
                                //    color = Color.Black;
                                //}

                                //Draw background
                                //spriteBatch.Draw(BackgroundTexture, new Vector2(blockX * WorldMetrics.SpriteSize, blockY * WorldMetrics.SpriteSize) - CameraPosition, null, color, 0f, Vector2.Zero, WorldMetrics.SpriteSize / 16f, SpriteEffects.None, 1f);

                                //If block is solid (Not air)
                                if (GetBlock(blockX, blockY) != 0)
                                {
                                    //Draw it
                                    int       State           = Chunks[xChunk, yChunk].GetBlockType(x, y);
                                    Rectangle SourceRectangle = new Rectangle((State * 16) % 64, ((State * 16) / 64) * 16, 16, 16);

                                    Color colorToAttach = Data[Chunks[xChunk, yChunk].GetBlock(x, y) - 1].GetColor();
                                    //spriteBatch.Draw(GroundTexture, new Vector2(blockX * WorldMetrics.SpriteSize, blockY * WorldMetrics.SpriteSize) - CameraPosition, null, new Color(0.5f, 0.5f, 0.5f), 0f, Vector2.Zero, WorldMetrics.SpriteSize / 16f, SpriteEffects.None, 0.5f);
                                    spriteBatch.Draw(GroundTexture, new Vector2(blockX * WorldMetrics.SpriteSize, blockY * WorldMetrics.SpriteSize) - CameraPosition, SourceRectangle, colorToAttach, 0f, Vector2.Zero, WorldMetrics.SpriteSize / 16f, SpriteEffects.None, 0.5f);
                                    //Get health
                                    int health = Chunks[xChunk, yChunk].GetBlockHealth(x, y);

                                    //If block is damaged : display damage
                                    if (health < 5)
                                    {
                                        spriteBatch.Draw(BreakTextures[health], new Vector2(blockX * WorldMetrics.SpriteSize, blockY * WorldMetrics.SpriteSize) - CameraPosition, null, color, 0f, Vector2.Zero, WorldMetrics.SpriteSize / 16f, SpriteEffects.None, 0f);
                                    }
                                    drawingCount++;
                                }
                            }
                        }
                    }
                }
            }

            int MouseGridX = (int)Math.Floor(mouseCameraPosition.X / WorldMetrics.SpriteSize);
            int MouseGridY = (int)Math.Floor(mouseCameraPosition.Y / WorldMetrics.SpriteSize);

            int BlockID = 0;

            if (MouseGridX > 0 && MouseGridY > 0 && MouseGridX <= WorldMetrics.ChunkSizeX * WorldMetrics.ChunkCountX &&
                MouseGridY <= WorldMetrics.ChunkSizeY * WorldMetrics.ChunkCountY)
            {
                BlockID = Chunks[MouseGridX / WorldMetrics.ChunkSizeX, MouseGridY / WorldMetrics.ChunkSizeY].
                          GetBlock(MouseGridX % WorldMetrics.ChunkSizeX, MouseGridY % WorldMetrics.ChunkSizeY);
            }

            if (BlockID != 0)
            {
                var name = Data[BlockID - 1].Name;
                spriteBatch.DrawString(defaultFont, name, mousePosition, Color.Black);
            }
            //Draw cursor
            spriteBatch.Draw(Cursor, new Vector2(mousePosition.X - 16, mousePosition.Y - 16), Color.White);

            //Draw player
            spriteBatch.Draw(Player, player.Position - new Vector2(16, 16), Color.Pink);

            //Format our fps and draw it
            var fps = string.Format("FPS : {0}, Full tiles drawn : {3},Tiles drawn : {1}, Chunk drawn : {2}", FPSCounter.AverageFramesPerSecond, drawingCount, chunkCount, fullBlockCount);

            spriteBatch.DrawString(defaultFont, fps, new Vector2(0, 0), Color.White);

            var pos = string.Format("Player : {0}", currentPlayerState);

            spriteBatch.DrawString(defaultFont, pos, new Vector2(0, 40), Color.White);
            //Close sprite batch
            spriteBatch.End();
            base.Draw(gameTime);
        }
        // Methods
        protected string GetTaskColorCode(bool isVip, DateTime created)
        {
            var dateDiff = (int)Math.Floor((DateTime.Now - created).TotalHours);

            return(ColorCodes.FirstOrDefault(c => c.Value == (isVip ? 3 : (dateDiff <= 2 ? dateDiff : 2))).Key);
        }
示例#20
0
 public static int Floor(double a)
 {
     return((int)MathCore.Floor(a));
 }
示例#21
0
文件: Math.cs 项目: pacojq/Mononoke
 public static Vector2 Floor(Vector2 val)
 {
     return(new Vector2((int)SMath.Floor(val.X), (int)SMath.Floor(val.Y)));
 }
示例#22
0
        private static Rectangle GetBoundingRectangle(Vector2 tl, Vector2 tr, Vector2 bl, Vector2 br)
        {
            // Find the minimum and maximum "corners" based on the given vectors
            float minX  = MathF.Min(tl.X, MathF.Min(tr.X, MathF.Min(bl.X, br.X)));
            float maxX  = MathF.Max(tl.X, MathF.Max(tr.X, MathF.Max(bl.X, br.X)));
            float minY  = MathF.Min(tl.Y, MathF.Min(tr.Y, MathF.Min(bl.Y, br.Y)));
            float maxY  = MathF.Max(tl.Y, MathF.Max(tr.Y, MathF.Max(bl.Y, br.Y)));
            float sizeX = maxX - minX + .5F;
            float sizeY = maxY - minY + .5F;

            return(new Rectangle((int)(MathF.Ceiling(minX) - .5F), (int)(MathF.Ceiling(minY) - .5F), (int)MathF.Floor(sizeX), (int)MathF.Floor(sizeY)));
        }