-
Notifications
You must be signed in to change notification settings - Fork 5
/
BoundingBox.cs
93 lines (82 loc) · 2.64 KB
/
BoundingBox.cs
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
namespace KeesTalksTech.Utilities.Graphics
{
using System;
/// <summary>
/// Calculates a bounding box.
/// </summary>
public class BoundingBox
{
/// <summary>
/// Gets the width.
/// </summary>
/// <value>
/// The width.
/// </value>
public float Width { get; private set; }
/// <summary>
/// Gets the height.
/// </summary>
/// <value>
/// The height.
/// </value>
public float Height { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="BoundingBox"/> class.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
public BoundingBox(float width, float height)
{
this.Width = width;
this.Height = height;
}
/// <summary>
/// Scales the boundingbox using the specified scale.
/// </summary>
/// <param name="scale">The scale.</param>
/// <returns>A new bounding box.</returns>
public BoundingBox Scale(float scale)
{
return new BoundingBox(Width * scale, Height * scale);
}
/// <summary>
/// Calculates the new bounding box based on the max width and max height.
/// </summary>
/// <param name="maxWidth">The maximum width.</param>
/// <param name="maxHeight">The maximum height.</param>
/// <returns>A new bounding box.</returns>
public BoundingBox Calculate(float maxWidth, float maxHeight)
{
var resolution = Width / Height;
var maxResolution = maxWidth / maxHeight;
if (resolution == maxResolution)
{
return new BoundingBox(maxWidth, maxHeight);
}
if (maxResolution == 1)
{
var diff = maxWidth / Math.Max(Width, Height);
return new BoundingBox(
Width * diff,
Height * diff
);
}
if (resolution == 1)
{
var min = Math.Min(maxWidth, maxHeight);
var diff = min / Width;
return new BoundingBox(
Width * diff,
Height * diff
);
}
var h = (maxWidth / Width) * Height;
if (h <= maxHeight)
{
return new BoundingBox(maxWidth, h);
}
var w = (maxHeight / Height) * Width;
return new BoundingBox(w, maxHeight);
}
}
}