A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using osuTK;
6
7namespace osu.Framework.Graphics.Primitives
8{
9 /// <summary>
10 /// A structure that tells how "far" along an axis
11 /// the projection of vertices onto the axis would be.
12 /// </summary>
13 internal readonly struct ProjectionRange
14 {
15 /// <summary>
16 /// The minimum projected value.
17 /// </summary>
18 public float Min { get; }
19
20 /// <summary>
21 /// The maximum projected value.
22 /// </summary>
23 public float Max { get; }
24
25 public ProjectionRange(Vector2 axis, ReadOnlySpan<Vector2> vertices)
26 {
27 Min = 0;
28 Max = 0;
29
30 if (vertices.Length == 0)
31 return;
32
33 Min = Vector2.Dot(axis, vertices[0]);
34 Max = Min;
35
36 for (int i = 1; i < vertices.Length; i++)
37 {
38 float val = Vector2.Dot(axis, vertices[i]);
39 if (val < Min)
40 Min = val;
41 if (val > Max)
42 Max = val;
43 }
44 }
45
46 /// <summary>
47 /// Checks whether this range overlaps another range.
48 /// </summary>
49 /// <param name="other">The other range to test against.</param>
50 /// <returns>Whether the two ranges overlap.</returns>
51 public bool Overlaps(ProjectionRange other) => Min <= other.Max && Max >= other.Min;
52 }
53}