Jypeli 10
The simple game programming library
Point2D.cs
Siirry tämän tiedoston dokumentaatioon.
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
27using Scalar = System.Double;
28#else
29using Scalar = System.Single;
30#endif
31using System;
32using System.Runtime.InteropServices;
33using System.Xml.Serialization;
34
35using AdvanceMath.Design ;
36namespace AdvanceMath
37{
38
43 [StructLayout(LayoutKind.Sequential, Size = Point2D.Size)]
44 [AdvBrowsableOrder("X,Y")]
45#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
46 [Serializable]
47 [System.ComponentModel.TypeConverter(typeof(AdvTypeConverter<Point2D>))]
48#endif
49 public struct Point2D : IEquatable<Point2D>
50 {
51 #region const fields
55 public const int Count = 2;
59 public const int Size = sizeof(int) * Count;
60 #endregion
61 #region readonly fields
65 public static readonly Point2D Zero = new Point2D();
66 private static readonly string FormatString = MatrixHelper.CreateVectorFormatString(Count);
68 #endregion
69 #region static methods
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 }
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 }
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
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 public static void Negate(ref Point2D source)
154 {
155 Negate(ref source, out source);
156 }
157 public static void Negate(ref Point2D source, out Point2D result)
158 {
159 result.X = -source.X;
160 result.Y = -source.Y;
161 }
162
163 public static Point2D Max(Point2D value1, Point2D value2)
164 {
165 Point2D result;
166 Max(ref value1, ref value2, out result);
167 return result;
168 }
169 public static void Max(ref Point2D value1, ref Point2D value2, out Point2D result)
170 {
171 result.X = (value1.X < value2.X) ? (value2.X) : (value1.X);
172 result.Y = (value1.Y < value2.Y) ? (value2.Y) : (value1.Y);
173 }
174
175 public static Point2D Min(Point2D value1, Point2D value2)
176 {
177 Point2D result;
178 Min(ref value1, ref value2, out result);
179 return result;
180 }
181 public static void Min(ref Point2D value1, ref Point2D value2, out Point2D result)
182 {
183 result.X = (value1.X > value2.X) ? (value2.X) : (value1.X);
184 result.Y = (value1.Y > value2.Y) ? (value2.Y) : (value1.Y);
185 }
186
187 #endregion
188 #region fields
192 [AdvBrowsable]
193 [XmlAttribute]
194#if !WINDOWS_STOREAPP
195 [System.ComponentModel.Description("The Magnitude on the X-Axis")]
196#endif
197 public int X;
201 [AdvBrowsable]
202 [XmlAttribute]
203#if !WINDOWS_STOREAPP
204 [System.ComponentModel.Description("The Magnitude on the Y-Axis")]
205#endif
206 public int Y;
207 #endregion
208 #region constructors
214 [InstanceConstructor("X,Y")]
215 public Point2D(int X, int Y)
216 {
217 this.X = X;
218 this.Y = Y;
219 }
220 #endregion
221 #region operators
229 public static Point2D operator +(Point2D left, Point2D right)
230 {
231 Point2D result;
232 result.X = left.X + right.X;
233 result.Y = left.Y + right.Y;
234 return result;
235 }
243 public static Point2D operator -(Point2D left, Point2D right)
244 {
245 Point2D result;
246 result.X = left.X - right.X;
247 result.Y = left.Y - right.Y;
248 return result;
249 }
257 public static Point2D operator *(Point2D source, int scalar)
258 {
259 Point2D result;
260 result.X = source.X * scalar;
261 result.Y = source.Y * scalar;
262 return result;
263 }
271 public static Point2D operator *(int scalar, Point2D source)
272 {
273 Point2D result;
274 result.X = scalar * source.X;
275 result.Y = scalar * source.Y;
276 return result;
277 }
283 public static Point2D operator -(Point2D source)
284 {
285 Point2D result;
286 result.X = -source.X;
287 result.Y = -source.Y;
288 return result;
289 }
290
297 public static bool operator ==(Point2D left, Point2D right)
298 {
299 return left.X == right.X && left.Y == right.Y;
300 }
307 public static bool operator !=(Point2D left, Point2D right)
308 {
309 return !(left.X == right.X && left.Y == right.Y);
310 }
311
312
313
314
315 #endregion
316 #region overrides
317 private string ToStringInternal(string FormatString)
318 {
319 return string.Format(FormatString, X, Y);
320 }
326 public string ToString(string format)
327 {
328 return ToStringInternal(string.Format(FormatableString, format));
329 }
330 public override string ToString()
331 {
333 }
334
335#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
336 public static bool TryParse(string s, out Point2D result)
337 {
338 if (s == null)
339 {
340 result = Zero;
341 return false;
342 }
343 string[] vals = ParseHelper.SplitStringVector(s);
344 if (vals.Length != Count)
345 {
346 result = Zero;
347 return false;
348 }
349 if (int.TryParse(vals[0], out result.X) &&
350 int.TryParse(vals[1], out result.Y))
351 {
352 return true;
353 }
354 else
355 {
356 result = Zero;
357 return false;
358 }
359 }
360#endif
361 [ParseMethod]
362 public static Point2D Parse(string s)
363 {
364 if (s == null)
365 {
366 throw new ArgumentNullException("s");
367 }
368 string[] vals = ParseHelper.SplitStringVector(s);
369 if (vals.Length != Count)
370 {
372 }
373 Point2D value;
374 value.X = int.Parse(vals[0]);
375 value.Y = int.Parse(vals[1]);
376 return value;
377 }
387 public override int GetHashCode()
388 {
389 return X.GetHashCode() ^ Y.GetHashCode();
390 }
397 public override bool Equals(object obj)
398 {
399 return (obj is Point2D) && Equals((Point2D)obj);
400 }
401 public bool Equals(Point2D other)
402 {
403 return Equals(ref this, ref other);
404 }
405 public static bool Equals(Point2D left, Point2D right)
406 {
407 return
408 left.X == right.X &&
409 left.Y == right.Y;
410 }
411 public static bool Equals(ref Point2D left, ref Point2D right)
412 {
413 return
414 left.X == right.X &&
415 left.Y == right.Y;
416 }
417 #endregion
418 }
419}
System.Single Scalar
Definition: Clamped.cs:29
static string CreateVectorFormatableString(int Count)
Definition: MatrixHelper.cs:51
static string CreateVectorFormatString(int Count)
Definition: MatrixHelper.cs:47
static string[] SplitStringVector(string s)
Definition: ParseHelper.cs:39
static void ThrowVectorFormatException(string value, int count, string format)
Definition: ThrowHelper.cs:54
This is the Vector Class.
Definition: Point2D.cs:50
static Point2D operator*(Point2D source, int scalar)
Does Scaler Multiplication on a Point.
Definition: Point2D.cs:257
static void Multiply(ref int scalar, ref Point2D source, out Point2D result)
Definition: Point2D.cs:135
static Point2D Negate(Point2D source)
Negates a Point.
Definition: Point2D.cs:146
static void Multiply(ref Point2D source, ref int scalar, out Point2D result)
Definition: Point2D.cs:122
static void Negate(ref Point2D source, out Point2D result)
Definition: Point2D.cs:157
static Point2D Multiply(Point2D source, int scalar)
Does Scaler Multiplication on a Point.
Definition: Point2D.cs:115
bool Equals(Point2D other)
Definition: Point2D.cs:401
static Point2D Add(Point2D left, Point2D right)
Adds 2 Vectors2Ds.
Definition: Point2D.cs:77
string ToString(string format)
Converts the numeric value of this instance to its equivalent string representation,...
Definition: Point2D.cs:326
Point2D(int X, int Y)
Creates a New Point Instance on the Stack.
Definition: Point2D.cs:215
static bool operator==(Point2D left, Point2D right)
Specifies whether the Points contain the same coordinates.
Definition: Point2D.cs:297
static bool Equals(ref Point2D left, ref Point2D right)
Definition: Point2D.cs:411
static bool operator!=(Point2D left, Point2D right)
Specifies whether the Points do not contain the same coordinates.
Definition: Point2D.cs:307
const int Size
The Size of the class in bytes;
Definition: Point2D.cs:59
static Point2D Min(Point2D value1, Point2D value2)
Definition: Point2D.cs:175
static void Negate(ref Point2D source)
Definition: Point2D.cs:153
static void Min(ref Point2D value1, ref Point2D value2, out Point2D result)
Definition: Point2D.cs:181
static Point2D Parse(string s)
Definition: Point2D.cs:362
static Point2D operator-(Point2D left, Point2D right)
Subtracts 2 Points.
Definition: Point2D.cs:243
override bool Equals(object obj)
Compares this Vector to another object. This should be done because the equality operators (==,...
Definition: Point2D.cs:397
static Point2D Multiply(int scalar, Point2D source)
Definition: Point2D.cs:128
override int GetHashCode()
Provides a unique hash code based on the member variables of this class. This should be done because ...
Definition: Point2D.cs:387
override string ToString()
Definition: Point2D.cs:330
string ToStringInternal(string FormatString)
Definition: Point2D.cs:317
static Point2D operator+(Point2D left, Point2D right)
Adds 2 Vectors2Ds.
Definition: Point2D.cs:229
static readonly string FormatString
Definition: Point2D.cs:66
const int Count
The number of int values in the class.
Definition: Point2D.cs:55
int Y
This is the Y value. (Usually represents a vertical position or direction.)
Definition: Point2D.cs:206
static bool TryParse(string s, out Point2D result)
Definition: Point2D.cs:336
static void Max(ref Point2D value1, ref Point2D value2, out Point2D result)
Definition: Point2D.cs:169
static Point2D Max(Point2D value1, Point2D value2)
Definition: Point2D.cs:163
static readonly Point2D Zero
Point(0,0)
Definition: Point2D.cs:65
int X
This is the X value. (Usually represents a horizontal position or direction.)
Definition: Point2D.cs:197
static void Add(ref Point2D left, ref Point2D right, out Point2D result)
Definition: Point2D.cs:84
static readonly string FormatableString
Definition: Point2D.cs:67
static Point2D Subtract(Point2D left, Point2D right)
Subtracts 2 Points.
Definition: Point2D.cs:96
static void Subtract(ref Point2D left, ref Point2D right, out Point2D result)
Definition: Point2D.cs:103
static bool Equals(Point2D left, Point2D right)
Definition: Point2D.cs:405