1 | #region MIT License |
---|
2 | /* |
---|
3 | * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/ |
---|
4 | * |
---|
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
6 | * of this software and associated documentation files (the "Software"), to deal |
---|
7 | * in the Software without restriction, including without limitation the rights to |
---|
8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
---|
9 | * the Software, and to permit persons to whom the Software is furnished to do so, |
---|
10 | * subject to the following conditions: |
---|
11 | * |
---|
12 | * The above copyright notice and this permission notice shall be |
---|
13 | * included in all copies or substantial portions of the Software. |
---|
14 | * |
---|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
17 | * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
---|
18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
---|
19 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
---|
20 | * OTHER DEALINGS IN THE SOFTWARE. |
---|
21 | */ |
---|
22 | #endregion |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | #if UseDouble |
---|
27 | using Scalar = System.Double; |
---|
28 | #else |
---|
29 | using Scalar = System.Single; |
---|
30 | #endif |
---|
31 | using System; |
---|
32 | using System.Runtime.InteropServices; |
---|
33 | using System.Xml.Serialization; |
---|
34 | |
---|
35 | using AdvanceMath.Design ; |
---|
36 | namespace AdvanceMath |
---|
37 | { |
---|
38 | |
---|
39 | /// <summary> |
---|
40 | /// This is the Vector Class. |
---|
41 | /// </summary> |
---|
42 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29"/></remarks> |
---|
43 | [StructLayout(LayoutKind.Sequential, Size = Point2D.Size)] |
---|
44 | [AdvBrowsableOrder("X,Y")] |
---|
45 | #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE |
---|
46 | [Serializable] |
---|
47 | [System.ComponentModel.TypeConverter(typeof(AdvTypeConverter<Point2D>))] |
---|
48 | #endif |
---|
49 | public struct Point2D : IEquatable<Point2D> |
---|
50 | { |
---|
51 | #region const fields |
---|
52 | /// <summary> |
---|
53 | /// The number of int values in the class. |
---|
54 | /// </summary> |
---|
55 | public const int Count = 2; |
---|
56 | /// <summary> |
---|
57 | /// The Size of the class in bytes; |
---|
58 | /// </summary> |
---|
59 | public const int Size = sizeof(int) * Count; |
---|
60 | #endregion |
---|
61 | #region readonly fields |
---|
62 | /// <summary> |
---|
63 | /// Point(0,0) |
---|
64 | /// </summary> |
---|
65 | public static readonly Point2D Zero = new Point2D(); |
---|
66 | private static readonly string FormatString = MatrixHelper.CreateVectorFormatString(Count); |
---|
67 | private readonly static string FormatableString = MatrixHelper.CreateVectorFormatableString(Count); |
---|
68 | #endregion |
---|
69 | #region static methods |
---|
70 | /// <summary> |
---|
71 | /// Adds 2 Vectors2Ds. |
---|
72 | /// </summary> |
---|
73 | /// <param name="left">The left Point operand.</param> |
---|
74 | /// <param name="right">The right Point operand.</param> |
---|
75 | /// <returns>The Sum of the 2 Points.</returns> |
---|
76 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29#Vector_addition_and_subtraction"/></remarks> |
---|
77 | public static Point2D Add(Point2D left, Point2D right) |
---|
78 | { |
---|
79 | Point2D result; |
---|
80 | result.X = left.X + right.X; |
---|
81 | result.Y = left.Y + right.Y; |
---|
82 | return result; |
---|
83 | } |
---|
84 | public static void Add(ref Point2D left, ref Point2D right, out Point2D result) |
---|
85 | { |
---|
86 | result.X = left.X + right.X; |
---|
87 | result.Y = left.Y + right.Y; |
---|
88 | } |
---|
89 | /// <summary> |
---|
90 | /// Subtracts 2 Points. |
---|
91 | /// </summary> |
---|
92 | /// <param name="left">The left Point operand.</param> |
---|
93 | /// <param name="right">The right Point operand.</param> |
---|
94 | /// <returns>The Difference of the 2 Points.</returns> |
---|
95 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29#Vector_addition_and_subtraction"/></remarks> |
---|
96 | public static Point2D Subtract(Point2D left, Point2D right) |
---|
97 | { |
---|
98 | Point2D result; |
---|
99 | result.X = left.X - right.X; |
---|
100 | result.Y = left.Y - right.Y; |
---|
101 | return result; |
---|
102 | } |
---|
103 | public static void Subtract(ref Point2D left, ref Point2D right, out Point2D result) |
---|
104 | { |
---|
105 | result.X = left.X - right.X; |
---|
106 | result.Y = left.Y - right.Y; |
---|
107 | } |
---|
108 | /// <summary> |
---|
109 | /// Does Scaler Multiplication on a Point. |
---|
110 | /// </summary> |
---|
111 | /// <param name="scalar">The scalar value that will multiply the Point.</param> |
---|
112 | /// <param name="source">The Point to be multiplied.</param> |
---|
113 | /// <returns>The Product of the Scaler Multiplication.</returns> |
---|
114 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29#int_multiplication"/></remarks> |
---|
115 | public static Point2D Multiply(Point2D source, int scalar) |
---|
116 | { |
---|
117 | Point2D result; |
---|
118 | result.X = source.X * scalar; |
---|
119 | result.Y = source.Y * scalar; |
---|
120 | return result; |
---|
121 | } |
---|
122 | public static void Multiply(ref Point2D source, ref int scalar, out Point2D result) |
---|
123 | { |
---|
124 | result.X = source.X * scalar; |
---|
125 | result.Y = source.Y * scalar; |
---|
126 | } |
---|
127 | |
---|
128 | public static Point2D Multiply(int scalar, Point2D source) |
---|
129 | { |
---|
130 | Point2D result; |
---|
131 | result.X = scalar * source.X; |
---|
132 | result.Y = scalar * source.Y; |
---|
133 | return result; |
---|
134 | } |
---|
135 | public static void Multiply(ref int scalar, ref Point2D source, out Point2D result) |
---|
136 | { |
---|
137 | result.X = scalar * source.X; |
---|
138 | result.Y = scalar * source.Y; |
---|
139 | } |
---|
140 | |
---|
141 | /// <summary> |
---|
142 | /// Negates a Point. |
---|
143 | /// </summary> |
---|
144 | /// <param name="source">The Point to be Negated.</param> |
---|
145 | /// <returns>The Negated Point.</returns> |
---|
146 | public static Point2D Negate(Point2D source) |
---|
147 | { |
---|
148 | Point2D result; |
---|
149 | result.X = -source.X; |
---|
150 | result.Y = -source.Y; |
---|
151 | return result; |
---|
152 | } |
---|
153 | [CLSCompliant(false)] |
---|
154 | public static void Negate(ref Point2D source) |
---|
155 | { |
---|
156 | Negate(ref source, out source); |
---|
157 | } |
---|
158 | public static void Negate(ref Point2D source, out Point2D result) |
---|
159 | { |
---|
160 | result.X = -source.X; |
---|
161 | result.Y = -source.Y; |
---|
162 | } |
---|
163 | |
---|
164 | public static Point2D Max(Point2D value1, Point2D value2) |
---|
165 | { |
---|
166 | Point2D result; |
---|
167 | Max(ref value1, ref value2, out result); |
---|
168 | return result; |
---|
169 | } |
---|
170 | public static void Max(ref Point2D value1, ref Point2D value2, out Point2D result) |
---|
171 | { |
---|
172 | result.X = (value1.X < value2.X) ? (value2.X) : (value1.X); |
---|
173 | result.Y = (value1.Y < value2.Y) ? (value2.Y) : (value1.Y); |
---|
174 | } |
---|
175 | |
---|
176 | public static Point2D Min(Point2D value1, Point2D value2) |
---|
177 | { |
---|
178 | Point2D result; |
---|
179 | Min(ref value1, ref value2, out result); |
---|
180 | return result; |
---|
181 | } |
---|
182 | public static void Min(ref Point2D value1, ref Point2D value2, out Point2D result) |
---|
183 | { |
---|
184 | result.X = (value1.X > value2.X) ? (value2.X) : (value1.X); |
---|
185 | result.Y = (value1.Y > value2.Y) ? (value2.Y) : (value1.Y); |
---|
186 | } |
---|
187 | |
---|
188 | #endregion |
---|
189 | #region fields |
---|
190 | /// <summary> |
---|
191 | /// This is the X value. (Usually represents a horizontal position or direction.) |
---|
192 | /// </summary> |
---|
193 | [AdvBrowsable] |
---|
194 | [XmlAttribute] |
---|
195 | [System.ComponentModel.Description("The Magnitude on the X-Axis")] |
---|
196 | public int X; |
---|
197 | /// <summary> |
---|
198 | /// This is the Y value. (Usually represents a vertical position or direction.) |
---|
199 | /// </summary> |
---|
200 | [AdvBrowsable] |
---|
201 | [XmlAttribute] |
---|
202 | [System.ComponentModel.Description("The Magnitude on the Y-Axis")] |
---|
203 | public int Y; |
---|
204 | #endregion |
---|
205 | #region constructors |
---|
206 | /// <summary> |
---|
207 | /// Creates a New Point Instance on the Stack. |
---|
208 | /// </summary> |
---|
209 | /// <param name="X">The X value.</param> |
---|
210 | /// <param name="Y">The Y value.</param> |
---|
211 | [InstanceConstructor("X,Y")] |
---|
212 | public Point2D(int X, int Y) |
---|
213 | { |
---|
214 | this.X = X; |
---|
215 | this.Y = Y; |
---|
216 | } |
---|
217 | #endregion |
---|
218 | #region operators |
---|
219 | /// <summary> |
---|
220 | /// Adds 2 Vectors2Ds. |
---|
221 | /// </summary> |
---|
222 | /// <param name="left">The left Point operand.</param> |
---|
223 | /// <param name="right">The right Point operand.</param> |
---|
224 | /// <returns>The Sum of the 2 Points.</returns> |
---|
225 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29#Vector_addition_and_subtraction"/></remarks> |
---|
226 | public static Point2D operator +(Point2D left, Point2D right) |
---|
227 | { |
---|
228 | Point2D result; |
---|
229 | result.X = left.X + right.X; |
---|
230 | result.Y = left.Y + right.Y; |
---|
231 | return result; |
---|
232 | } |
---|
233 | /// <summary> |
---|
234 | /// Subtracts 2 Points. |
---|
235 | /// </summary> |
---|
236 | /// <param name="left">The left Point operand.</param> |
---|
237 | /// <param name="right">The right Point operand.</param> |
---|
238 | /// <returns>The Difference of the 2 Points.</returns> |
---|
239 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29#Vector_addition_and_subtraction"/></remarks> |
---|
240 | public static Point2D operator -(Point2D left, Point2D right) |
---|
241 | { |
---|
242 | Point2D result; |
---|
243 | result.X = left.X - right.X; |
---|
244 | result.Y = left.Y - right.Y; |
---|
245 | return result; |
---|
246 | } |
---|
247 | /// <summary> |
---|
248 | /// Does Scaler Multiplication on a Point. |
---|
249 | /// </summary> |
---|
250 | /// <param name="source">The Point to be multiplied.</param> |
---|
251 | /// <param name="scalar">The scalar value that will multiply the Point.</param> |
---|
252 | /// <returns>The Product of the Scaler Multiplication.</returns> |
---|
253 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29#int_multiplication"/></remarks> |
---|
254 | public static Point2D operator *(Point2D source, int scalar) |
---|
255 | { |
---|
256 | Point2D result; |
---|
257 | result.X = source.X * scalar; |
---|
258 | result.Y = source.Y * scalar; |
---|
259 | return result; |
---|
260 | } |
---|
261 | /// <summary> |
---|
262 | /// Does Scaler Multiplication on a Point. |
---|
263 | /// </summary> |
---|
264 | /// <param name="scalar">The scalar value that will multiply the Point.</param> |
---|
265 | /// <param name="source">The Point to be multiplied.</param> |
---|
266 | /// <returns>The Product of the Scaler Multiplication.</returns> |
---|
267 | /// <remarks><seealso href="http://en.wikipedia.org/wiki/Vector_%28spatial%29#int_multiplication"/></remarks> |
---|
268 | public static Point2D operator *(int scalar, Point2D source) |
---|
269 | { |
---|
270 | Point2D result; |
---|
271 | result.X = scalar * source.X; |
---|
272 | result.Y = scalar * source.Y; |
---|
273 | return result; |
---|
274 | } |
---|
275 | /// <summary> |
---|
276 | /// Negates a Point. |
---|
277 | /// </summary> |
---|
278 | /// <param name="source">The Point to be Negated.</param> |
---|
279 | /// <returns>The Negated Point.</returns> |
---|
280 | public static Point2D operator -(Point2D source) |
---|
281 | { |
---|
282 | Point2D result; |
---|
283 | result.X = -source.X; |
---|
284 | result.Y = -source.Y; |
---|
285 | return result; |
---|
286 | } |
---|
287 | |
---|
288 | /// <summary> |
---|
289 | /// Specifies whether the Points contain the same coordinates. |
---|
290 | /// </summary> |
---|
291 | /// <param name="left">The left Point to test.</param> |
---|
292 | /// <param name="right">The right Point to test.</param> |
---|
293 | /// <returns>true if the Points have the same coordinates; otherwise false</returns> |
---|
294 | public static bool operator ==(Point2D left, Point2D right) |
---|
295 | { |
---|
296 | return left.X == right.X && left.Y == right.Y; |
---|
297 | } |
---|
298 | /// <summary> |
---|
299 | /// Specifies whether the Points do not contain the same coordinates. |
---|
300 | /// </summary> |
---|
301 | /// <param name="left">The left Point to test.</param> |
---|
302 | /// <param name="right">The right Point to test.</param> |
---|
303 | /// <returns>true if the Points do not have the same coordinates; otherwise false</returns> |
---|
304 | public static bool operator !=(Point2D left, Point2D right) |
---|
305 | { |
---|
306 | return !(left.X == right.X && left.Y == right.Y); |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | #endregion |
---|
313 | #region overrides |
---|
314 | private string ToStringInternal(string FormatString) |
---|
315 | { |
---|
316 | return string.Format(FormatString, X, Y); |
---|
317 | } |
---|
318 | /// <summary> |
---|
319 | /// Converts the numeric value of this instance to its equivalent string representation, using the specified format. |
---|
320 | /// </summary> |
---|
321 | /// <param name="format">the format for each scaler in this Vector</param> |
---|
322 | /// <returns></returns> |
---|
323 | public string ToString(string format) |
---|
324 | { |
---|
325 | return ToStringInternal(string.Format(FormatableString, format)); |
---|
326 | } |
---|
327 | public override string ToString() |
---|
328 | { |
---|
329 | return ToStringInternal(FormatString); |
---|
330 | } |
---|
331 | |
---|
332 | #if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT |
---|
333 | public static bool TryParse(string s, out Point2D result) |
---|
334 | { |
---|
335 | if (s == null) |
---|
336 | { |
---|
337 | result = Zero; |
---|
338 | return false; |
---|
339 | } |
---|
340 | string[] vals = ParseHelper.SplitStringVector(s); |
---|
341 | if (vals.Length != Count) |
---|
342 | { |
---|
343 | result = Zero; |
---|
344 | return false; |
---|
345 | } |
---|
346 | if (int.TryParse(vals[0], out result.X) && |
---|
347 | int.TryParse(vals[1], out result.Y)) |
---|
348 | { |
---|
349 | return true; |
---|
350 | } |
---|
351 | else |
---|
352 | { |
---|
353 | result = Zero; |
---|
354 | return false; |
---|
355 | } |
---|
356 | } |
---|
357 | #endif |
---|
358 | [ParseMethod] |
---|
359 | public static Point2D Parse(string s) |
---|
360 | { |
---|
361 | if (s == null) |
---|
362 | { |
---|
363 | throw new ArgumentNullException("s"); |
---|
364 | } |
---|
365 | string[] vals = ParseHelper.SplitStringVector(s); |
---|
366 | if (vals.Length != Count) |
---|
367 | { |
---|
368 | ThrowHelper.ThrowVectorFormatException(s, Count, FormatString); |
---|
369 | } |
---|
370 | Point2D value; |
---|
371 | value.X = int.Parse(vals[0]); |
---|
372 | value.Y = int.Parse(vals[1]); |
---|
373 | return value; |
---|
374 | } |
---|
375 | /// <summary> |
---|
376 | /// Provides a unique hash code based on the member variables of this |
---|
377 | /// class. This should be done because the equality operators (==, !=) |
---|
378 | /// have been overriden by this class. |
---|
379 | /// <p/> |
---|
380 | /// The standard implementation is a simple XOR operation between all local |
---|
381 | /// member variables. |
---|
382 | /// </summary> |
---|
383 | /// <returns></returns> |
---|
384 | public override int GetHashCode() |
---|
385 | { |
---|
386 | return X.GetHashCode() ^ Y.GetHashCode(); |
---|
387 | } |
---|
388 | /// <summary> |
---|
389 | /// Compares this Vector to another object. This should be done because the |
---|
390 | /// equality operators (==, !=) have been overriden by this class. |
---|
391 | /// </summary> |
---|
392 | /// <param name="obj"></param> |
---|
393 | /// <returns></returns> |
---|
394 | public override bool Equals(object obj) |
---|
395 | { |
---|
396 | return (obj is Point2D) && Equals((Point2D)obj); |
---|
397 | } |
---|
398 | public bool Equals(Point2D other) |
---|
399 | { |
---|
400 | return Equals(ref this, ref other); |
---|
401 | } |
---|
402 | public static bool Equals(Point2D left, Point2D right) |
---|
403 | { |
---|
404 | return |
---|
405 | left.X == right.X && |
---|
406 | left.Y == right.Y; |
---|
407 | } |
---|
408 | [CLSCompliant(false)] |
---|
409 | public static bool Equals(ref Point2D left, ref Point2D right) |
---|
410 | { |
---|
411 | return |
---|
412 | left.X == right.X && |
---|
413 | left.Y == right.Y; |
---|
414 | } |
---|
415 | #endregion |
---|
416 | } |
---|
417 | } |
---|