Esempio n. 1
0
        //public void draw(MVector3 pos, MRectangle srcRect, MVector2 scale,
        //    MVector2 translate, MVector3 drawCenter, float angle, MVector4 color)
        public static void draw(int textureID, int pos, int srcRect, int scale,
                                int translate, int drawCenter, float angle, int color)
        {
            //m_graphicHandlerRef.getSpriteBatch().Draw(m_texture, m_graphicHandlerRef.getGameInstance().Window.ClientBounds, Color.White);

            Rectangle? rect;
            MRectangle trueRect = MRectangle.getFromStorage(srcRect);

            if (trueRect != null)
            {
                rect = trueRect.getRawData();
            }
            else
            {
                rect = null;
            }

            Vector2       scaleParam     = MVector2.getFromStorage(scale).getRawData();
            SpriteEffects sEffect        = generateSpriteEffect(ref scaleParam);
            Vector2       translateParam = MVector3.getFromStorage(pos).getRawAxistedVec2()
                                           + MVector2.getFromStorage(translate).getRawData();

            m_graphicHandlerRef.getSpriteBatch()
            .Draw(
                MTexture.getFromStorage(textureID).m_texture,
                translateParam,
                rect,
                MVector4.getFromStorage(color).getColor(),
                angle,
                MVector3.getFromStorage(drawCenter).getRawVec2(),
                scaleParam,
                sEffect,
                0.0f);
        }
Esempio n. 2
0
        /// <summary>
        /// Checks if the the <see cref="MCircle"/> intersects the specified <see cref="MRectangle"/>.
        /// </summary>
        /// <param name="rectangle">The rectangle.</param>
        /// <returns>True if it intersects; otherwise false.</returns>
        public bool Intersects(MRectangle rectangle)
        {
            MVector2 circleDistance = (this.Origin - rectangle.Center).AbsoluteComponents;

            if (circleDistance.X > (rectangle.Width / 2 + this.Radius)) { return false; }
            if (circleDistance.Y > (rectangle.Height / 2 + this.Radius)) { return false; }

            if (circleDistance.X <= (rectangle.Width / 2)) { return true; }
            if (circleDistance.Y <= (rectangle.Height / 2)) { return true; }

            float cornerDistanceSquared = circleDistance.Translate(rectangle.Width * -0.5f, rectangle.Height * -0.5f).LengthSquared();

            return cornerDistanceSquared <= this.Radius * this.Radius;
        }
Esempio n. 3
0
        public void TestConstructors()
        {
            // Test that all constructors work the same
            MRectangle a = new MRectangle(new MRectangleInt(-5, -5, 5, 5));
            MRectangle b = new MRectangle(-5, -5);
            MRectangle c = new MRectangle(new Vector2(0, 0), new Vector2(-5, -5));
            MRectangle d = new MRectangle(new MPoint2(0, 0), new MPoint2(-5, -5));
            MRectangle e = new MRectangle(0, 0, -5f, -5f);
            MRectangle f = new MRectangle(new Vector2(-5, -5));
            MRectangle g = new MRectangle(new MCircle(new MVector2(-2.5f, -2.5f), 2.5f));

            Assert.AreEqual(a, b);
            Assert.AreEqual(b, c);
            Assert.AreEqual(c, d);
            Assert.AreEqual(d, e);
            Assert.AreEqual(e, f);
            Assert.AreEqual(f, g);

            // Test negative size
            Assert.AreEqual(new MRectangle(0, 0, 10, 10), new MRectangle(10, 10, -10, -10));
            Assert.AreEqual(new MRectangle(-10, -10, 10, 10), new MRectangle(0, 0, -10, -10));
            Assert.AreEqual(new MRectangle(-30, -30, 20, 20), new MRectangle(-10, -10, -20, -20));
        }
Esempio n. 4
0
 public void SeparationVector_Rectangle_Right_Correct()
 {
     MCircle c = new MCircle(new MVector2(3, 0), 2f);
     MRectangle r = new MRectangle(1, -32, 1, 64);
     MVector2 sepVector = c.SeparationVector(r);
     MVector2 expected = new MVector2(1, 0);
     Assert.AreEqual(expected, sepVector);
 }
Esempio n. 5
0
 public void SeparationVector_Rectangle_Outside_ZeroVector()
 {
     MCircle c = new MCircle(new MVector2(100, -100), 2f);
     MRectangle r = new MRectangle(1, -32, 1, 64);
     MVector2 sepVector = c.SeparationVector(r);
     Assert.AreEqual(MVector2.Zero, sepVector);
 }
Esempio n. 6
0
 public void SeparationVector_Rectangle_Outside_Corner_ZeroVector()
 {
     MCircle c = new MCircle(new MVector2(-128, 128), 32f);
     MRectangle r = new MRectangle(-64, -64, 128, 128);
     MVector2 sepVector = c.SeparationVector(r);
     Assert.AreEqual(MVector2.Zero, sepVector);
 }
Esempio n. 7
0
 public void SeparationVector_Rectangle_BottomLeft_CorrectDirection()
 {
     MCircle c = new MCircle(new MVector2(-2, 2), 2f);
     MRectangle r = new MRectangle(-1, -1, 2, 2);
     MVector2 sepVector = c.SeparationVector(r);
     Assert.IsTrue(sepVector.X < 0);
     Assert.IsTrue(sepVector.Y > 0);
 }
Esempio n. 8
0
 public void Separate_Rectangle_Separated()
 {
     MCircle c1 = new MCircle(new MVector2(-64, -64), 30f);
     MRectangle r = new MRectangle(-128, 0, 128, 70);
     MCircle separated = c1.Separate(r);
     Assert.IsTrue(separated.SeparationVector(r) == MVector2.Zero);
 }
Esempio n. 9
0
 public void CellRectangle_CorrectPositive()
 {
     MPoint2 point = new MPoint2(0, 1);
     MRectangle expected = new MRectangle(0, cellSize, cellSize, cellSize);
     Assert.AreEqual(expected, grid.CellRectangle(point));
 }
Esempio n. 10
0
        /// <summary>
        /// Returns the shortest vector to translate the <see cref="MCircle"/> with in order to separate from the provided <see cref="MRectangle"/>.
        /// </summary>
        /// <param name="rectangle">The rectangle to separate from.</param>
        /// <returns></returns>
        public MVector2 SeparationVector(MRectangle rectangle)
        {
            MVector2 left = new MVector2(rectangle.TopLeft.X - (this.Origin.X + this.Radius), 0);
            MVector2 right = new MVector2(rectangle.BottomRight.X - (this.Origin.X - this.Radius), 0);
            MVector2 up = new MVector2(0, rectangle.TopLeft.Y - (this.Origin.Y + this.Radius));
            MVector2 down = new MVector2(0, rectangle.BottomRight.Y - (this.Origin.Y - this.Radius));

            if (left.X < 0 && right.X > 0 || up.Y < 0 && down.Y > 0)
            {
                MVector2 shortest = new MVector2(float.MaxValue, float.MaxValue);

                if (this.Origin.X < rectangle.Left && this.Origin.Y < rectangle.Top)
                {
                    shortest = this.SeparationVector(rectangle.TopLeft);
                }
                else if (this.Origin.X > rectangle.Right && this.Origin.Y < rectangle.Top)
                {
                    shortest = this.SeparationVector(rectangle.TopRight);
                }
                else if (this.Origin.X < rectangle.Left && this.Origin.Y > rectangle.Bottom)
                {
                    shortest = this.SeparationVector(rectangle.BottomLeft);
                }
                else if (this.Origin.X > rectangle.Right && this.Origin.Y > rectangle.Bottom)
                {
                    shortest = this.SeparationVector(rectangle.BottomRight);
                }

                if (left.X < 0 && left.LengthSquared() < shortest.LengthSquared())
                {
                    shortest = left;
                }
                if (right.X > 0 && right.LengthSquared() < shortest.LengthSquared())
                {
                    shortest = right;
                }
                if (up.Y < 0 && up.LengthSquared() < shortest.LengthSquared())
                {
                    shortest = up;
                }
                if (down.Y > 0 && down.LengthSquared() < shortest.LengthSquared())
                {
                    shortest = down;
                }

                return shortest;
            }

            return MVector2.Zero;
        }
Esempio n. 11
0
        public void TestWidthHeight()
        {
            for (int i = 0; i < AreaTest.RANDOM_TEST_AMOUNT; i++)
            {
                float x = random.Next(-100, 100);
                float y = random.Next(-100, 100);
                float x2 = random.Next(-100, 100);
                float y2 = random.Next(-100, 100);

                MRectangle a = new MRectangle(x, y, x2 - x, y2 - y);
                Assert.AreEqual(Math.Abs(y2 - y), a.Height);
                Assert.AreEqual(Math.Abs(x2 - x), a.Width);
            }
        }
Esempio n. 12
0
        public void TestTranslate()
        {
            for (int i = 0; i < AreaTest.RANDOM_TEST_AMOUNT; i++)
            {
                float x = random.Next(-100, 100);
                float y = random.Next(-100, 100);
                float w = random.Next(0, 100);
                float h = random.Next(0, 100);

                float tx = random.Next(-100, 100);
                float ty = random.Next(-100, 100);

                MRectangle a = new MRectangle(x, y, w, h);
                Assert.AreEqual(new MRectangle(x + tx, y + ty, w, h), a.Translate(new Vector2(tx, ty)));

                // Assert X Y methods
                Assert.AreEqual(a.Translate(new Vector2(tx, 0)), a.TranslateX(tx));
                Assert.AreEqual(a.Translate(new Vector2(0, ty)), a.TranslateY(ty));
            }
        }
Esempio n. 13
0
        private void PointerPressedDraw(bool rightButtonPressed, Point startPosition)
        {
            if (CurrentShapeType == ShapeType.Polygon || CurrentShapeType == ShapeType.Bezier)
            {
                DrawClickTool.IsChecked = true;
                SelectedTool            = Tools.DrawClick;
            }

            if (SelectedTool == Tools.DrawClick)
            {
                if (CurrentShapeType == ShapeType.Polygon || CurrentShapeType == ShapeType.Bezier)
                {
                    if (ClickedTimes++ >= 1)
                    {
                        if (rightButtonPressed)
                        {
                            if (ClickedTimes >= 4)
                            {
                                ClickedTimes = 0;

                                var poly = PendingShape as MPolygon;
                                if (CurrentShapeType == ShapeType.Polygon)
                                {
                                    poly.AddSegment(poly.StartLocation);
                                }
                                poly.StartLocation = new Point(0, 0);
                                poly.EndLocation   = new Point(0, 0);
                                poly.Closed        = true;

                                AddShape();

                                return;
                            }
                            else
                            {
                                --ClickedTimes;
                            }
                        }
                        else
                        {
                            if (CurrentShapeType == ShapeType.Polygon || CurrentShapeType == ShapeType.Bezier)
                            {
                                (PendingShape as MPolygon).AddSegment(startPosition);
                            }
                        }
                        this.canvasControl.Invalidate();

                        return;
                    }
                }
                else
                {
                    if (ClickedTimes == 0)
                    {
                        ClickedTimes = 1;
                    }
                    else
                    {
                        ClickedTimes = 0;

                        AddShape();

                        return;
                    }
                }
            }

            if (CurrentShapeType == ShapeType.Rectangle)
            {
                PendingShape = new MRectangle(startPosition, startPosition);
            }
            else if (CurrentShapeType == ShapeType.Circle)
            {
                PendingShape = new MCircle(startPosition, startPosition);
            }
            else if (CurrentShapeType == ShapeType.Line)
            {
                PendingShape = new MLine(startPosition, startPosition);
            }
            else if (CurrentShapeType == ShapeType.Polygon)
            {
                PendingShape = new MPolygon(startPosition, startPosition);
            }
            else if (CurrentShapeType == ShapeType.Bezier)
            {
                PendingShape = new MBezier(startPosition, startPosition);
            }

            PendingShape.Mode = ShapeModes.Drawing;

            this.canvasControl.Invalidate();
        }
Esempio n. 14
0
 public void SeparationVector_Rectangle_TopRight_CorrectDirection()
 {
     MCircle c = new MCircle(new MVector2(2, -2), 2f);
     MRectangle r = new MRectangle(-1, -1, 2, 2);
     MVector2 sepVector = c.SeparationVector(r);
     Assert.IsTrue(sepVector.X > 0);
     Assert.IsTrue(sepVector.Y < 0);
 }
Esempio n. 15
0
 public void CellRectangle_CorrectNegative()
 {
     MPoint2 point = new MPoint2(-1, -2);
     MRectangle expected = new MRectangle(-cellSize, -2 * cellSize, cellSize, cellSize);
     Assert.AreEqual(expected, grid.CellRectangle(point));
 }
Esempio n. 16
0
 public void SeparationVector_Rectangle_Up_Correct()
 {
     MCircle c = new MCircle(new MVector2(0, 0), 2f);
     MRectangle r = new MRectangle(-32, 1, 64, 1);
     MVector2 sepVector = c.SeparationVector(r);
     MVector2 expected = new MVector2(0, -1);
     Assert.AreEqual(expected, sepVector);
 }
Esempio n. 17
0
 /// <summary>
 /// Separates the <see cref="MCircle"/> from the specified <see cref="MRectangle"/>, translating it with the shortest distance that makes them not intersect.
 /// </summary>
 /// <param name="rectangle">The rectangle to separate from.</param>
 /// <returns></returns>
 public MCircle Separate(MRectangle rectangle) => this.Translate(this.SeparationVector(rectangle));