Code Bucket …

area

You can visit http://www.the-barn.org/codebucket.php?id=73 to view this snippet directly.

  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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using System;
using System.ComponentModel;
using Microsoft.Xna.Framework;

namespace Incah.Core.Geo
{
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public struct Area
	{
		public static readonly Area Zero = new Area();

		public Area(Position position, Size size)
			: this()
		{
			Position = position;
			Size = size;
		}

		public Area(Position top_left, Position bottom_right)
			: this()
		{
			Position = top_left;
			Size = new Size(
				bottom_right.Horizontal - top_left.Horizontal,
				bottom_right.Vertical - top_left.Vertical
			);
		}

		public Area(int top, int left, int bottom, int right)
			: this()
		{
			Position = new Position(left, top);
			Size = new Size(
				right - left,
				bottom - top
			);
		}

		[Category("Dimensions")]
		public Position Position
		{
			get;
			set;
		}

		[Category("Dimensions")]
		public Size Size
		{
			get;
			set;
		}

		public int Top
		{
			get
			{
				return Position.Vertical;
			}
		}

		public int Bottom
		{
			get
			{
				return Position.Vertical + Size.Height;
			}
		}

		public int Left
		{
			get
			{
				return Position.Horizontal;
			}
		}

		public int Right
		{
			get
			{
				return Position.Horizontal + Size.Width;
			}
		}

		public int HorizontalCenter
		{
			get
			{
				return Position.Horizontal + (Size.Width / 2);
			}
		}

		public int VerticalCenter
		{
			get
			{
				return Position.Vertical + (Size.Height / 2);
			}
		}

		public Position TopLeftCorner
		{
			get
			{
				return new Position(Left, Top);
			}
		}

		public Position TopRightCorner
		{
			get
			{
				return new Position(Right, Top);
			}
		}

		public Position BottomLeftCorner
		{
			get
			{
				return new Position(Left, Bottom);
			}
		}

		public Position BottomRightCorner
		{
			get
			{
				return new Position(Right, Bottom);
			}
		}

		public Position TopCenter
		{
			get
			{
				return new Position(HorizontalCenter, Top);
			}
		}

		public Position BottomCenter
		{
			get
			{
				return new Position(HorizontalCenter, Bottom);
			}
		}

		public Position LeftCenter
		{
			get
			{
				return new Position(Left, VerticalCenter);
			}
		}

		public Position RightCenter
		{
			get
			{
				return new Position(Right, VerticalCenter);
			}
		}

		public Position Center
		{
			get
			{
				return new Position(HorizontalCenter, VerticalCenter);
			}
		}

		public Position AlignmentPoint(Alignment alignment)
		{
			return Position + alignment.Align(Size);
		}

		public int AlignmentPoint(HorizontalAlignment alignment)
		{
			return Position.Horizontal + alignment.Align(Size.Width);
		}

		public int AlignmentPoint(VerticalAlignment alignment)
		{
			return Position.Vertical + alignment.Align(Size.Height);
		}

		[Browsable(false)]
		public bool IsInverted
		{
			get
			{
				return Size.Width < 0
					|| Size.Height < 0;
			}
		}

		public bool Intersects(Area area)
		{
			return Top <= area.Bottom
				&& Bottom >= area.Top
				&& Left <= area.Right
				&& Right >= area.Left;
		}

		public bool Contains(Area area)
		{
			return Top <= area.Top
				&& Bottom >= area.Bottom
				&& Left <= area.Left
				&& Right >= area.Right;
		}

		public bool Contains(Position position)
		{
			return Top <= position.Vertical
				&& Bottom >= position.Vertical
				&& Left <= position.Horizontal
				&& Right >= position.Horizontal;
		}

		public static Area Intersection(Area a, Area b)
		{
			Area area = new Area(
				Math.Max(a.Top, b.Top),
				Math.Max(a.Left, b.Left),
				Math.Min(a.Bottom, b.Bottom),
				Math.Min(a.Right, b.Right)
			);

			return area.IsInverted
				? Zero
				: area;
		}

		public static Area Intersection(params Area[] areas)
		{
			if (areas.Length == 0)
				return Zero;

			Area area = areas[0];

			for (int index = 1; index < areas.Length; ++index)
			{
				area = Intersection(area, areas[index]);
				if (area.IsInverted)
					return Zero;
			}

			return area;
		}

		public static Area Union(Area a, Area b)
		{
			return new Area(
				Math.Min(a.Top, b.Top),
				Math.Min(a.Left, b.Left),
				Math.Max(a.Bottom, b.Bottom),
				Math.Max(a.Right, b.Right)
			);
		}

		public static Area Union(params Area[] areas)
		{
			if (areas.Length == 0)
				return Zero;

			Area area = areas[0];

			for (int index = 1; index < areas.Length; ++index)
				area = Union(area, areas[index]);

			return area;
		}

		[Browsable(false)]
		public bool IsZero
		{
			get
			{
				return Position.IsZero
					&& Size.IsZero;
			}
		}

		public static Area FromRectangle(Rectangle rect)
		{
			return new Area(
				rect.Top,
				rect.Left,
				rect.Bottom,
				rect.Right
			);
		}

		public Rectangle ToRectangle()
		{
			return new Rectangle(
				Position.Horizontal,
				Position.Vertical,
				Size.Width,
				Size.Height
			);
		}

		public override string ToString()
		{
			return string.Format("{0} @ {1}", Size, Position);
		}
	}
}
  • Posted on 27.02.2010 at 3:51 AM by stoffle
  • Language: C#

Snippets

 
36 Results
Page 1 of 3
TitleLanguagePosted ByPostedExpiresActions
Python first-class classes for KodenPythonstoffle04.07.2010 8:19 AM (64 days 3h 56m ago)-view
mLAN Activation ScriptPlain Textstoffle23.06.2010 6:42 AM (75 days 5h 32m ago)-view
areaC#stoffle27.02.2010 3:51 AM (191 days 9h 23m ago)-view
list extensionsC#stoffle09.02.2010 11:12 PM (208 days 14h 2m ago)-view
possible IComponent intererface version 95C#stoffle09.02.2010 11:04 PM (208 days 14h 11m ago)-view
Render SystemC++spam05.06.2009 12:14 PM (458 days ago)-view
Effect SampleC++spam03.06.2009 2:40 PM (459 days 21h 34m ago)-view
Rho LambdasC++spam03.06.2009 2:38 PM (459 days 21h 37m ago)-view
Example VSM implC++spam02.06.2009 1:21 PM (460 days 22h 53m ago)-view
Vector3 Interface (public)C++kalin11.02.2008 9:53 PM (937 days 15h 21m ago)-view
TimesTablesC++kalin09.02.2008 6:04 PM (939 days 19h 10m ago)-view
Skruje Account Search PagePHPstoffle03.02.2008 5:12 PM (945 days 20h 2m ago)-view
C++ is still bork...C++spam26.01.2008 5:58 PM (953 days 19h 16m ago)-view
C++ is bork #3C++spam26.01.2008 5:47 PM (953 days 19h 28m ago)-view
C++ is bork #2C++spam26.01.2008 5:46 PM (953 days 19h 28m ago)-view
36 Results
Page 1 of 3