-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRectangle.cs
154 lines (137 loc) · 4.58 KB
/
Rectangle.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
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
using System;
using System.Collections.Generic;
using System.Drawing;
using OpenTK;
namespace OpenTKGUI
{
/// <summary>
/// An orthagonal rectangle defined by a point and a size (described as a point offset).
/// </summary>
public struct Rectangle
{
public Rectangle(double X, double Y, double Width, double Height)
{
this.Location = new Point(X, Y);
this.Size = new Point(Width, Height);
}
public Rectangle(Point Location, Point Size)
{
this.Location = Location;
this.Size = Size;
}
public Rectangle(Point Size)
{
this.Location = new Point();
this.Size = Size;
}
/// <summary>
/// Creates a rectangle from this rectangle with a margin applied.
/// </summary>
public Rectangle Margin(double Amount)
{
return new Rectangle(
this.Location + new Point(Amount, Amount),
this.Size - new Point(Amount, Amount) * 2.0);
}
/// <summary>
/// Gets if the specified point is inside the rectangle.
/// </summary>
public bool In(Point Point)
{
return Point.X >= this.Location.X && Point.Y >= this.Location.Y &&
Point.X < this.Location.X + this.Size.X && Point.Y < this.Location.Y + this.Size.Y;
}
/// <summary>
/// Gets if this rectangle intersects another.
/// </summary>
public bool Intersects(Rectangle Rect)
{
return this.Location.X < Rect.Location.X + Rect.Size.X
&& this.Location.Y < Rect.Location.Y + Rect.Size.Y
&& Rect.Location.X < this.Location.X + this.Size.X
&& Rect.Location.Y < this.Location.Y + this.Size.Y;
}
/// <summary>
/// Gets the intersecting area between this rectangle and another.
/// </summary>
public Rectangle Intersection(Rectangle Rect)
{
Point atl = this.Location; Point btl = Rect.Location;
Point abr = this.BottomRight; Point bbr = Rect.BottomRight;
double x = Math.Max(atl.X, btl.X);
double y = Math.Max(atl.Y, btl.Y);
double xx = Math.Min(abr.X, bbr.X);
double yy = Math.Min(abr.Y, bbr.Y);
return new Rectangle(x, y, xx - x, yy - y);
}
public static implicit operator Rectangle(RectangleF A)
{
return new Rectangle(A.X, A.Y, A.Width, A.Height);
}
public static Rectangle operator +(Rectangle A, Point B)
{
return new Rectangle(A.Location + B, A.Size);
}
/// <summary>
/// Gets the relative offset of the given absolute point in this rectangle.
/// </summary>
public Point ToRelative(Point Absolute)
{
return new Point(
(Absolute.X - this.Location.X) / this.Size.X,
(Absolute.Y - this.Location.Y) / this.Size.Y);
}
/// <summary>
/// Gets the relative offset of the given rectangle in this rectangle.
/// </summary>
public Rectangle ToRelative(Rectangle Absolute)
{
return new Rectangle(
this.ToRelative(Absolute.Location),
new Point(Absolute.Size.X / this.Size.X, Absolute.Size.Y / this.Size.Y));
}
/// <summary>
/// Scales this rectangle by a point multiplier.
/// </summary>
public Rectangle Scale(Point Scale)
{
return new Rectangle(this.Location.Scale(Scale), this.Size.Scale(Scale));
}
/// <summary>
/// Gets or sets the position of the top-left corner of the rectangle.
/// </summary>
public Point TopLeft
{
get
{
return this.Location;
}
set
{
this.Location = value;
}
}
/// <summary>
/// Gets or sets the position of the bottom-right corner of the rectangle.
/// </summary>
public Point BottomRight
{
get
{
return this.Location + this.Size;
}
set
{
this.Size = value - this.Location;
}
}
/// <summary>
/// The location of the top-left corner of the rectangle.
/// </summary>
public Point Location;
/// <summary>
/// The size of the rectangle.
/// </summary>
public Point Size;
}
}