-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rectangle.cs
201 lines (185 loc) · 4.96 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
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
/*
* Vision.NET 2.1 Computer Vision Library
* Copyright (C) 2009 Matthew Johnson
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace VisionNET
{
/// <summary>
/// A rectangle.
/// </summary>
[Serializable]
public struct Rectangle
{
private int _r;
/// <summary>
/// Starting row of the rectangle.
/// </summary>
public int R
{
get { return _r; }
set { _r = value; }
}
private int _c;
/// <summary>
/// Starting column of the rectangle.
/// </summary>
public int C
{
get { return _c; }
set { _c = value; }
}
private int _rows;
/// <summary>
/// Number of rows in the rectangle.
/// </summary>
public int Rows
{
get { return _rows; }
set { _rows = value; }
}
private int _columns;
/// <summary>
/// Number of columns in the rectangle.
/// </summary>
public int Columns
{
get { return _columns; }
set { _columns = value; }
}
/// <summary>
/// Top edge of the rectangle.
/// </summary>
public int Top
{
get
{
return _r;
}
}
/// <summary>
/// Left edge of the rectangle.
/// </summary>
public int Left
{
get
{
return _c;
}
}
/// <summary>
/// Right edge of the rectangle.
/// </summary>
public int Right
{
get
{
return _c + _columns;
}
}
/// <summary>
/// Bottom edge of the rectangle.
/// </summary>
public int Bottom
{
get
{
return _r + _rows;
}
}
/// <summary>
/// Width of the rectangle (equivalent to <see cref="Columns"/>).
/// </summary>
public int Width
{
get
{
return Columns;
}
set
{
Columns = value;
}
}
/// <summary>
/// Height of the rectangle (equivalent to <see cref="Rows"/>).
/// </summary>
public int Height
{
get
{
return Rows;
}
set
{
Rows = value;
}
}
/// <summary>
/// Outputs a string version of the Rectangle object as [topmost row],[leftmost column],[height in rows],[width in columns].
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("{0},{1},{2},{3}", R, C, Rows, Columns);
}
/// <summary>
/// X-offset of the rectangle (equivalent to <see cref="C"/>).
/// </summary>
public int X
{
get
{
return C;
}
set
{
C = value;
}
}
/// <summary>
/// Y-offset of the rectangle (equivalent to <see cref="R"/>).
/// </summary>
public int Y
{
get
{
return R;
}
set
{
R = value;
}
}
/// <summary>
/// Returns a random rectangle within the rectangle provided.
/// </summary>
/// <param name="minRow">Minimum row</param>
/// <param name="minColumn">Minimum column</param>
/// <param name="maxRow">Maximum bound for a rectangle</param>
/// <param name="maxColumn">Maximum bound for a column</param>
/// <returns></returns>
public static Rectangle Random(int minRow, int minColumn, int maxRow, int maxColumn)
{
Rectangle rect = new Rectangle();
rect.R = ThreadsafeRandom.Next(minRow, maxRow);
rect.C = ThreadsafeRandom.Next(minColumn, maxColumn);
rect.Rows = ThreadsafeRandom.Next(maxRow - rect.R) + 1;
rect.Columns = ThreadsafeRandom.Next(maxColumn - rect.C) + 1;
return rect;
}
}
}