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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Xunit;
using Vector2 = Microsoft.Xna.Framework.Vector2;
namespace MonoGame.Extended.Tests.Primitives;
public class OrientedRectangleTests
{
[Fact]
public void Initializes_oriented_rectangle()
{
var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(3, 4), new Matrix2(5, 6, 7, 8, 9, 10));
Assert.Equal(new Point2(1, 2), rectangle.Center);
Assert.Equal(new Vector2(3, 4), rectangle.Radii);
Assert.Equal(new Matrix2(5, 6, 7, 8, 9, 10), rectangle.Orientation);
CollectionAssert.Equal(
new List<Vector2>
{
new(-3, -2),
new(-33, -38),
new(23, 26),
new(53, 62)
},
rectangle.Points);
}
public static readonly IEnumerable<object[]> _equalsComparisons = new[]
{
new object[]
{
"empty compared with empty is true",
new OrientedRectangle(Point2.Zero, Size2.Empty, Matrix2.Identity),
new OrientedRectangle(Point2.Zero, Size2.Empty, Matrix2.Identity)
},
new object[]
{
"initialized compared with initialized true",
new OrientedRectangle(new Point2(1, 2), new Size2(3, 4), new Matrix2(5, 6, 7, 8, 9, 10)),
new OrientedRectangle(new Point2(1, 2), new Size2(3, 4), new Matrix2(5, 6, 7, 8, 9, 10))
}
};
[Theory]
[MemberData(nameof(_equalsComparisons))]
#pragma warning disable xUnit1026
public void Equals_comparison(string name, OrientedRectangle first, OrientedRectangle second)
#pragma warning restore xUnit1026
{
Assert.True(first == second);
Assert.False(first != second);
}
public class Transform
{
[Fact]
public void Center_point_is_not_translated()
{
var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(), Matrix2.Identity);
var transform = Matrix2.Identity;
var result = OrientedRectangle.Transform(rectangle, ref transform);
Assert.Equal(new Point2(1, 2), result.Center);
}
[Fact]
public void Center_point_is_translated()
{
var rectangle = new OrientedRectangle(new Point2(0, 0), new Size2(), new Matrix2());
var transform = Matrix2.CreateTranslation(1, 2);
var result = OrientedRectangle.Transform(rectangle, ref transform);
Assert.Equal(new Point2(1, 2), result.Center);
}
[Fact]
public void Radii_is_not_changed_by_identity_transform()
{
var rectangle = new OrientedRectangle(new Point2(), new Size2(10, 20), new Matrix2());
var transform = Matrix2.Identity;
var result = OrientedRectangle.Transform(rectangle, ref transform);
Assert.Equal(new Vector2(10, 20), result.Radii);
}
[Fact]
public void Radii_is_not_changed_by_translation()
{
var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(10, 20), new Matrix2());
var transform = Matrix2.CreateTranslation(1, 2);
var result = OrientedRectangle.Transform(rectangle, ref transform);
Assert.Equal(new Vector2(10, 20), result.Radii);
}
[Fact]
public void Orientation_is_rotated_45_degrees_left()
{
var rectangle = new OrientedRectangle(new Point2(), new Size2(), Matrix2.Identity);
var transform = Matrix2.CreateRotationZ(MathHelper.PiOver4);
var result = OrientedRectangle.Transform(rectangle, ref transform);
Assert.Equal(new Point2(), result.Center);
Assert.Equal(new Vector2(), result.Radii);
Assert.Equal(Matrix2.CreateRotationZ(MathHelper.PiOver4), result.Orientation);
}
[Fact]
public void Orientation_is_rotated_to_45_degrees_from_180()
{
var rectangle = new OrientedRectangle(new Point2(), new Size2(), Matrix2.CreateRotationZ(MathHelper.Pi));
var transform = Matrix2.CreateRotationZ(-3 * MathHelper.PiOver4);
var expectedOrientation = Matrix2.CreateRotationZ(MathHelper.PiOver4);
var result = OrientedRectangle.Transform(rectangle, ref transform);
Assert.Equal(new Point2(), result.Center);
Assert.Equal(new Vector2(), result.Radii);
Assert.Equal(expectedOrientation.M11, result.Orientation.M11, 6);
Assert.Equal(expectedOrientation.M12, result.Orientation.M12, 6);
Assert.Equal(expectedOrientation.M21, result.Orientation.M21, 6);
Assert.Equal(expectedOrientation.M22, result.Orientation.M22, 6);
Assert.Equal(expectedOrientation.M31, result.Orientation.M31, 6);
Assert.Equal(expectedOrientation.M32, result.Orientation.M32, 6);
}
[Fact]
public void Points_are_same_as_center()
{
var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(), Matrix2.Identity);
var transform = Matrix2.Identity;
var result = OrientedRectangle.Transform(rectangle, ref transform);
CollectionAssert.Equal(
new List<Vector2>
{
new(1, 2),
new(1, 2),
new(1, 2),
new(1, 2)
},
result.Points);
}
[Fact]
public void Points_are_translated()
{
var rectangle = new OrientedRectangle(new Point2(0, 0), new Size2(2, 4), Matrix2.Identity);
var transform = Matrix2.CreateTranslation(10, 20);
var result = OrientedRectangle.Transform(rectangle, ref transform);
CollectionAssert.Equal(
new List<Vector2>
{
new(8, 16),
new(8, 24),
new(12, 24),
new(12, 16)
},
result.Points);
}
[Fact]
public void Applies_rotation_and_translation()
{
/* Rectangle with center point p, aligned in coordinate system with origin 0.
*
* :
* :
* +-+
* | |
* |p|
* | |
* ...............0-+............
* :
* :
* :
*
* Rotate around center point p, 90 degrees around origin 0.
*
* :
* :
* +---+
* | p |
* ...........+---0..............
* :
* :
* :
*
* Then translate rectangle by x=10 and y=20.
* :
* : +---+
* : | p |
* y=21 - - - - - - - -> +---+
* .
* :
* ...............0..............
* :
* :
* :
*/
var rectangle = new OrientedRectangle(new Point2(1, 2), new Size2(2, 4), Matrix2.Identity);
var transform =
Matrix2.CreateRotationZ(MathHelper.PiOver2)
*
Matrix2.CreateTranslation(10, 20);
var result = OrientedRectangle.Transform(rectangle, ref transform);
Assert.Equal(8, result.Center.X, 6);
Assert.Equal(21, result.Center.Y, 6);
Assert.Equal(2, result.Radii.X, 6);
Assert.Equal(4, result.Radii.Y, 6);
Assert.Equal(Matrix2.CreateRotationZ(MathHelper.PiOver2), result.Orientation);
CollectionAssert.Equal(
new List<Vector2>
{
new(4, 23),
new(4, 19),
new(12, 19),
new(12, 23)
},
result.Points);
}
}
}
|