summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Tests/Primitives/ShapeTests.cs
blob: 37b2fc4c2e9739985d2ffe46abe4996caf90f733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using Microsoft.Xna.Framework;
using Xunit;

namespace MonoGame.Extended.Tests.Primitives;

public class ShapeTests
{
    public class CircleFTests
    {
        [Fact]
        public void CircCircIntersectionSameCircleTest()
        {
            IShapeF shape1 = new CircleF(Point2.Zero, 2.0f);
            IShapeF shape2 = new CircleF(Point2.Zero, 2.0f);

            Assert.True(shape1.Intersects(shape2));
        }

        [Fact]
        public void CircCircIntersectionOverlappingTest()
        {
            IShapeF shape1 = new CircleF(new Point2(1, 2), 2.0f);
            IShapeF shape2 = new CircleF(Point2.Zero, 2.0f);

            Assert.True(shape1.Intersects(shape2));
        }

        [Fact]
        public void CircleCircleNotIntersectingTest()
        {
            IShapeF shape1 = new CircleF(new Point2(5, 5), 2.0f);
            IShapeF shape2 = new CircleF(Point2.Zero, 2.0f);

            Assert.False(shape1.Intersects(shape2));
        }
    }

    public class RectangleFTests
    {
        [Fact]
        public void RectRectSameRectTest()
        {
            IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5));
            IShapeF shape2 = new RectangleF(Point2.Zero, new Size2(5, 5));

            Assert.True(shape1.Intersects(shape2));
        }

        [Fact]
        public void RectRectIntersectingTest()
        {
            IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5));
            IShapeF shape2 = new RectangleF(new Point2(3, 3), new Size2(5, 5));

            Assert.True(shape1.Intersects(shape2));
        }

        [Fact]
        public void RectRectNotIntersectingTest()
        {
            IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5));
            IShapeF shape2 = new RectangleF(new Point2(10, 10), new Size2(5, 5));

            Assert.False(shape1.Intersects(shape2));
        }

        [Fact]
        public void RectCircContainedTest()
        {
            IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5));
            IShapeF shape2 = new CircleF(Point2.Zero, 4);

            Assert.True(shape1.Intersects(shape2));
            Assert.True(shape2.Intersects(shape1));
        }

        [Fact]
        public void RectCircOnEdgeTest()
        {
            IShapeF shape1 = new RectangleF(Point2.Zero, new Size2(5, 5));
            IShapeF shape2 = new CircleF(new Point2(5, 0), 4);

            Assert.True(shape1.Intersects(shape2));
            Assert.True(shape2.Intersects(shape1));
        }
    }

    public class OrientedRectangleTests
    {
        [Fact]
        public void Axis_aligned_rectangle_intersects_circle()
        {
            /*
             *                    :
             *                    :
             *                   +*+
             *        ...........* *.........
             *                   +*+
             *                    :
             *                    :
             */
            IShapeF rectangle = new OrientedRectangle(Point2.Zero, new Size2(1, 1), Matrix2.Identity);
            var circle = new CircleF(Point2.Zero, 1);

            Assert.True(rectangle.Intersects(circle));
        }

        [Fact]
        public void Axis_aligned_rectangle_does_not_intersect_circle_in_top_left()
        {
            /*
             *                  * :
             *                 * *:
             *                  *+-+
             *        ...........| |.........
             *                   +-+
             *                    :
             *                    :
             */
            IShapeF rectangle = new OrientedRectangle(Point2.Zero, new Size2(1, 1), Matrix2.Identity);
            var circle = new CircleF(new Point2(-2, 1), .99f);

            Assert.False(rectangle.Intersects(circle));
        }

        [Fact]
        public void Rectangle_rotated_45_degrees_does_not_intersect_circle_in_bottom_right()
        {
            /*
             *                    :
             *                    :
             *                    +-.
             *        .........../ / ........
             *                  +./* *
             *                    *   *
             *                    :* *
             */
            IShapeF rectangle = new OrientedRectangle(new Point2(-1, 1), new Size2(1.42f, 1.42f), Matrix2.CreateRotationZ(-MathHelper.PiOver4));
            var circle = new CircleF(new Point2(1, -1), 1.4f);

            Assert.False(rectangle.Intersects(circle));
        }

        [Fact]
        public void Axis_aligned_rectangle_does_not_intersect_rectangle()
        {
            /*
             *                    :
             *                    :
             *                  +-+
             *        ..........| |**.......
             *                  +-+ *
             *                    :**
             *                    :
             */
            IShapeF rectangle = new OrientedRectangle(new Point2(-1, 0), new Size2(1, 1), Matrix2.Identity);
            var rect = new RectangleF(new Point2(0.001f, 0), new Size2(2, 2));

            Assert.False(rectangle.Intersects(rect));
        }

        [Fact]
        public void Axis_aligned_rectangle_intersects_rectangle()
        {
            /*
             *                    :
             *                    :
             *                  +-+
             *        ..........| |**.......
             *                  +-+ *
             *                    :**
             *                    :
             */
            IShapeF rectangle = new OrientedRectangle(new Point2(-1, 0), new Size2(1, 1), Matrix2.Identity);
            var rect = new RectangleF(new Point2(0, 0), new Size2(2, 2));

            Assert.True(rectangle.Intersects(rect));
        }
    }
}