comparison SilverlightGlimpse/SilverFlow.Controls/Extensions/GeometryExtensions.cs @ 63:536498832a79

Latest version before changing bindings to Listbox
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sun, 22 Apr 2012 13:33:42 +0100
parents
children
comparison
equal deleted inserted replaced
62:810116cd6b8e 63:536498832a79
1 using System;
2 using System.Windows;
3 using SilverFlow.Geometry;
4
5 namespace SilverFlow.Controls.Extensions
6 {
7 /// <summary>
8 /// Geometry extensions
9 /// </summary>
10 public static class GeometryExtensions
11 {
12 /// <summary>
13 /// Rounds the specified point to the nearest integer coordinates.
14 /// </summary>
15 /// <param name="point">The point to round coordinates.</param>
16 /// <returns>New point with rounded coordinates.</returns>
17 public static Point Round(this Point point)
18 {
19 return new Point(Math.Round(point.X), Math.Round(point.Y));
20 }
21
22 /// <summary>
23 /// Ensures that coordinates are positive.
24 /// </summary>
25 /// <param name="point">The point.</param>
26 /// <returns>Point with positive coordinates.</returns>
27 public static Point EnsurePositive(this Point point)
28 {
29 return new Point()
30 {
31 X = (point.X < 0) ? 0 : point.X,
32 Y = (point.Y < 0) ? 0 : point.Y
33 };
34 }
35
36 /// <summary>
37 /// Gets position of the specified rectangle.
38 /// </summary>
39 /// <param name="rect">Rectangle.</param>
40 /// <returns>Upper left corner of the rectangle.</returns>
41 public static Point Position(this Rect rect)
42 {
43 return new Point(rect.X, rect.Y);
44 }
45
46 /// <summary>
47 /// Gets position the lower right corner of the rectangle.
48 /// </summary>
49 /// <param name="rect">Rectangle.</param>
50 /// <returns>Lower right corner of the rectangle.</returns>
51 public static Point BottomRight(this Rect rect)
52 {
53 return new Point(rect.Right, rect.Bottom);
54 }
55
56 /// <summary>
57 /// Gets position the lower left corner of the rectangle.
58 /// </summary>
59 /// <param name="rect">Rectangle.</param>
60 /// <returns>Lower left corner of the rectangle.</returns>
61 public static Point BottomLeft(this Rect rect)
62 {
63 return new Point(rect.X, rect.Bottom);
64 }
65
66 /// <summary>
67 /// Gets position the upper right corner of the rectangle.
68 /// </summary>
69 /// <param name="rect">Rectangle.</param>
70 /// <returns>Upper right corner of the rectangle.</returns>
71 public static Point TopRight(this Rect rect)
72 {
73 return new Point(rect.Right, rect.Y);
74 }
75
76 /// <summary>
77 /// Ensures that coordinates of the point are in the specified bounds.
78 /// </summary>
79 /// <param name="point">The point.</param>
80 /// <param name="bounds">The bounds.</param>
81 /// <returns>Point within the boinds.</returns>
82 public static Point EnsureInBounds(this Point point, Rect bounds)
83 {
84 return new Point()
85 {
86 X = point.X.EnsureInRange(bounds.X, bounds.Right),
87 Y = point.Y.EnsureInRange(bounds.Y, bounds.Bottom)
88 };
89 }
90
91 /// <summary>
92 /// Ensures that the X-coordinate of the point is in the specified horizontal bounds.
93 /// </summary>
94 /// <param name="point">The point.</param>
95 /// <param name="bounds">The bounds.</param>
96 /// <returns>Point with the X-coordinate within the boinds.</returns>
97 public static Point EnsureInHorizontalBounds(this Point point, Rect bounds)
98 {
99 return new Point()
100 {
101 X = point.X.EnsureInRange(bounds.X, bounds.Right),
102 Y = point.Y
103 };
104 }
105
106 /// <summary>
107 /// Ensures that the Y-coordinate of the point is in the specified vertical bounds.
108 /// </summary>
109 /// <param name="point">The point.</param>
110 /// <param name="bounds">The bounds.</param>
111 /// <returns>Point with the Y-coordinate within the boinds.</returns>
112 public static Point EnsureInVerticalBounds(this Point point, Rect bounds)
113 {
114 return new Point()
115 {
116 X = point.X,
117 Y = point.Y.EnsureInRange(bounds.Y, bounds.Bottom)
118 };
119 }
120
121 /// <summary>
122 /// Determines whether coordinates of the point are Not a Number (NaN).
123 /// </summary>
124 /// <param name="point">The point.</param>
125 /// <returns>
126 /// <c>true</c> if coordinates are not specified; otherwise, <c>false</c>.
127 /// </returns>
128 public static bool IsNotSet(this Point point)
129 {
130 return double.IsNaN(point.X) || double.IsNaN(point.Y);
131 }
132
133 /// <summary>
134 /// Adds an offset to the specified point.
135 /// </summary>
136 /// <param name="point">The point.</param>
137 /// <param name="x">Distance along X-coordinate.</param>
138 /// <param name="y">Distance along Y-coordinate.</param>
139 /// <returns>New point.</returns>
140 public static Point Add(this Point point, double x, double y)
141 {
142 return new Point(point.X + x, point.Y + y);
143 }
144
145 /// <summary>
146 /// Adds an offset specified by the Size to the specified point.
147 /// </summary>
148 /// <param name="point">The point.</param>
149 /// <param name="size">Distance to add.</param>
150 /// <returns>New point</returns>
151 public static Point Add(this Point point, Size size)
152 {
153 return new Point(point.X + size.Width, point.Y + size.Height);
154 }
155
156 /// <summary>
157 /// Determines whether dimensions are Not a Number (NaN).
158 /// </summary>
159 /// <param name="size">The size.</param>
160 /// <returns>
161 /// <c>true</c> if dimensions are not specified; otherwise, <c>false</c>.
162 /// </returns>
163 public static bool IsNotSet(this Size size)
164 {
165 return double.IsNaN(size.Width) || double.IsNaN(size.Height);
166 }
167
168 /// <summary>
169 /// Increments size to the specified values.
170 /// </summary>
171 /// <param name="size">The size.</param>
172 /// <param name="x">Increment by X-coordinate.</param>
173 /// <param name="y">Increment by Y-coordinate.</param>
174 /// <returns>New size.</returns>
175 public static Size Add(this Size size, double x, double y)
176 {
177 double width = size.Width + x;
178 double height = size.Height + y;
179
180 return new Size()
181 {
182 Width = width < 0 ? 0 : width,
183 Height = height < 0 ? 0 : height
184 };
185 }
186
187 /// <summary>
188 /// Increases size if the rectangle to the specified width and height.
189 /// </summary>
190 /// <param name="rect">The rectangle.</param>
191 /// <param name="width">Value to add to the width of the rectangle.</param>
192 /// <param name="height">Value to add to the height of the rectangle.</param>
193 /// <returns>New rectangle.</returns>
194 public static Rect Add(this Rect rect, double width, double height)
195 {
196 return new Rect
197 {
198 X = rect.X,
199 Y = rect.Y,
200 Width = Math.Max(0, rect.Width + width),
201 Height = Math.Max(0, rect.Height + height)
202 };
203 }
204
205 /// <summary>
206 /// Shifts the point to the specified distance.
207 /// </summary>
208 /// <param name="point">The point.</param>
209 /// <param name="distance">The distance.</param>
210 /// <returns>Point shifted to the specified distance.</returns>
211 public static Point Add(this Point point, Distance distance)
212 {
213 return new Point()
214 {
215 X = distance.X.IsNotSet() ? point.X : point.X + distance.X,
216 Y = distance.Y.IsNotSet() ? point.Y : point.Y + distance.Y
217 };
218 }
219
220 /// <summary>
221 /// Tests whether the rectangle overlaps with another one vertically.
222 /// </summary>
223 /// <param name="rect">The rectangle.</param>
224 /// <param name="testRect">Test rectangle.</param>
225 /// <param name="accuracy">Accuracy.</param>
226 /// <returns><c>true</c> if overlaps vertically; otherwise, <c>false</c>.</returns>
227 public static bool OverlapsVertically(this Rect rect, Rect testRect, double accuracy = 0)
228 {
229 double y1 = rect.Y + rect.Height - 1;
230 double y2 = testRect.Y + testRect.Height - 1;
231
232 if (rect.Y <= testRect.Y)
233 {
234 return y1 >= (testRect.Y - accuracy);
235 }
236 else if (rect.Y <= (y2 + accuracy))
237 {
238 return true;
239 }
240
241 return false;
242 }
243
244 /// <summary>
245 /// Tests whether the rectangle overlaps with another one horizontally.
246 /// </summary>
247 /// <param name="rect">The rectangle.</param>
248 /// <param name="testRect">Test rectangle.</param>
249 /// <param name="accuracy">Accuracy.</param>
250 /// <returns><c>true</c> if overlaps horizontally; otherwise, <c>false</c>.</returns>
251 public static bool OverlapsHorizontally(this Rect rect, Rect testRect, double accuracy = 0)
252 {
253 double x1 = rect.X + rect.Width - 1;
254 double x2 = testRect.X + testRect.Width - 1;
255
256 if (rect.X <= testRect.X)
257 {
258 return x1 >= (testRect.X - accuracy);
259 }
260 else if (rect.X <= (x2 + accuracy))
261 {
262 return true;
263 }
264
265 return false;
266 }
267
268 /// <summary>
269 /// Tests whether the point overlaps with the rectangle vertically.
270 /// </summary>
271 /// <param name="point">Test point.</param>
272 /// <param name="rect">Test rectangle.</param>
273 /// <param name="accuracy">Accuracy.</param>
274 /// <returns><c>true</c> if overlaps vertically; otherwise, <c>false</c>.</returns>
275 public static bool OverlapsVertically(this Point point, Rect rect, double accuracy = 0)
276 {
277 return (point.Y >= (rect.Y - accuracy) && point.Y <= (rect.Y + rect.Height - 1 + accuracy));
278 }
279
280 /// <summary>
281 /// Tests whether the point overlaps with the rectangle horizontally.
282 /// </summary>
283 /// <param name="point">Test point.</param>
284 /// <param name="rect">Test rectangle.</param>
285 /// <param name="accuracy">Accuracy.</param>
286 /// <returns><c>true</c> if overlaps horizontally; otherwise, <c>false</c>.</returns>
287 public static bool OverlapsHorizontally(this Point point, Rect rect, double accuracy = 0)
288 {
289 return (point.X >= (rect.X - accuracy) && point.X <= (rect.X + rect.Width - 1 + accuracy));
290 }
291 }
292 }