Jypeli 10
The simple game programming library
Line.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
27#if UseDouble
28using Scalar = System.Double;
29#else
30using Scalar = System.Single;
31#endif
32using System;
33using System.Runtime.InteropServices;
36{
37 [StructLayout(LayoutKind.Sequential, Size = Line.Size)]
38 [AdvBrowsableOrder("Normal,D")]
39#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE && !NETFX_CORE
40 [Serializable]
41 [System.ComponentModel.TypeConverter(typeof(AdvTypeConverter<Line>))]
42#endif
43 public struct Line : IEquatable<Line>
44 {
45 public static Line Transform(Matrix3x3 matrix, Line line)
46 {
47 Line result;
48 Transform(ref matrix, ref line, out result);
49 return result;
50 }
51 public static void Transform(ref Matrix3x3 matrix, ref Line line, out Line result)
52 {
53 Vector2D point;
54 Vector2D origin = Vector2D.Zero;
55 Vector2D.Multiply(ref line.Normal, ref line.D, out point);
56 Vector2D.Transform(ref matrix, ref point, out point);
57 Vector2D.Transform(ref matrix, ref origin, out origin);
58 Vector2D.Subtract(ref point, ref origin, out result.Normal);
59 Vector2D.Normalize(ref result.Normal, out result.Normal);
60 Vector2D.Dot(ref point, ref result.Normal, out result.D);
61 }
62 public static Line Transform(Matrix2x3 matrix, Line line)
63 {
64 Line result;
65 Transform(ref matrix, ref line, out result);
66 return result;
67 }
68 public static void Transform(ref Matrix2x3 matrix, ref Line line, out Line result)
69 {
70 Vector2D point;
71 Vector2D origin = Vector2D.Zero;
72 Vector2D.Multiply(ref line.Normal, ref line.D, out point);
73 Vector2D.Transform(ref matrix, ref point, out point);
74 Vector2D.Transform(ref matrix, ref origin, out origin);
75 Vector2D.Subtract(ref point, ref origin, out result.Normal);
76 Vector2D.Normalize(ref result.Normal, out result.Normal);
77 Vector2D.Dot(ref point, ref result.Normal, out result.D);
78 }
79
80
81 public const int Size = sizeof(Scalar) + Vector2D.Size;
82 [AdvBrowsable]
84 [AdvBrowsable]
85 public Scalar D;
86 [InstanceConstructor("Normal,D")]
87 public Line(Vector2D normal, Scalar d)
88 {
89 this.Normal = normal;
90 this.D = d;
91 }
92 public Line(Scalar nX, Scalar nY, Scalar d)
93 {
94 this.Normal.X = nX;
95 this.Normal.Y = nY;
96 this.D = d;
97 }
98 public Line(Vector2D point1, Vector2D point2)
99 {
100 Scalar x = point1.X - point2.X;
101 Scalar y = point1.Y - point2.Y;
102 Scalar magInv = 1 / MathHelper.Sqrt(x * x + y * y);
103 this.Normal.X = -y * magInv;
104 this.Normal.Y = x * magInv;
105 this.D = point1.X * this.Normal.X + point1.Y * this.Normal.Y;
106 }
107
109 {
110 Scalar result;
111 GetDistance(ref point, out result);
112 return result;
113 }
114 public void GetDistance(ref Vector2D point, out Scalar result)
115 {
116 Vector2D.Dot(ref point, ref Normal, out result);
117 result -= D;
118 }
119
121 {
122 Scalar result;
123 Intersects(ref ray, out result);
124 return result;
125 }
127 {
128 bool result;
129 Intersects(ref rect, out result);
130 return result;
131 }
132 public bool Intersects(BoundingCircle circle)
133 {
134 bool result;
135 circle.Intersects(ref this, out result);
136 return result;
137 }
138 public bool Intersects(BoundingPolygon polygon)
139 {
140 bool result;
141 Intersects(ref polygon, out result);
142 return result;
143 }
144
145 public void Intersects(ref Ray ray, out Scalar result)
146 {
147 Scalar dir;
148 Vector2D.Dot(ref Normal, ref ray.Direction, out dir);
149 if (-dir > 0)
150 {
151 Scalar DistanceFromOrigin = Normal * ray.Origin + D;
152 Vector2D.Dot(ref Normal, ref ray.Origin, out DistanceFromOrigin);
153 DistanceFromOrigin = -((DistanceFromOrigin + D) / dir);
154 if (DistanceFromOrigin >= 0)
155 {
156 result = DistanceFromOrigin;
157 return;
158 }
159 }
160 result = -1;
161 }
162 public void Intersects(ref BoundingRectangle box, out bool result)
163 {
164 Vector2D[] vertexes = box.Corners();
165 Scalar distance;
166 GetDistance(ref vertexes[0], out distance);
167
168 int sign = Math.Sign(distance);
169 result = false;
170 for (int index = 1; index < vertexes.Length; ++index)
171 {
172 GetDistance(ref vertexes[index], out distance);
173
174 if (Math.Sign(distance) != sign)
175 {
176 result = true;
177 break;
178 }
179 }
180 }
181 public void Intersects(ref BoundingCircle circle, out bool result)
182 {
183 circle.Intersects(ref this, out result);
184 }
185 public void Intersects(ref BoundingPolygon polygon, out bool result)
186 {
187 if (polygon == null) { throw new ArgumentNullException("polygon"); }
188 Vector2D[] vertexes = polygon.Vertexes;
189 Scalar distance;
190 GetDistance(ref vertexes[0], out distance);
191
192 int sign = Math.Sign(distance);
193 result = false;
194 for (int index = 1; index < vertexes.Length; ++index)
195 {
196 GetDistance(ref vertexes[index], out distance);
197
198 if (Math.Sign(distance) != sign)
199 {
200 result = true;
201 break;
202 }
203 }
204 }
205
206
207 public override string ToString()
208 {
209 return string.Format("N: {0} D: {1}", Normal, D);
210 }
211 public override int GetHashCode()
212 {
213 return Normal.GetHashCode() ^ D.GetHashCode();
214 }
215 public override bool Equals(object obj)
216 {
217 return obj is Line && Equals((Line)obj);
218 }
219 public bool Equals(Line other)
220 {
221 return Equals(ref this, ref other);
222 }
223 public static bool Equals(Line line1, Line line2)
224 {
225 return Equals(ref line1, ref line2);
226 }
227 public static bool Equals(ref Line line1, ref Line line2)
228 {
229 return Vector2D.Equals(ref line1.Normal, ref line2.Normal) && line1.D == line2.D;
230 }
231
232 public static bool operator ==(Line line1, Line line2)
233 {
234 return Equals(ref line1, ref line2);
235 }
236 public static bool operator !=(Line line1, Line line2)
237 {
238 return !Equals(ref line1, ref line2);
239 }
240 }
241}
System.Single Scalar
Definition: Clamped.cs:29
System.Single Scalar
Definition: Line.cs:30
static Scalar Sqrt(Scalar d)
Definition: MathHelper.cs:314
void GetDistance(ref Vector2D point, out Scalar result)
Definition: Line.cs:114
static Line Transform(Matrix3x3 matrix, Line line)
Definition: Line.cs:45
static void Transform(ref Matrix2x3 matrix, ref Line line, out Line result)
Definition: Line.cs:68
Line(Scalar nX, Scalar nY, Scalar d)
Definition: Line.cs:92
static bool operator==(Line line1, Line line2)
Definition: Line.cs:232
bool Intersects(BoundingRectangle rect)
Definition: Line.cs:126
static bool operator!=(Line line1, Line line2)
Definition: Line.cs:236
void Intersects(ref BoundingCircle circle, out bool result)
Definition: Line.cs:181
static bool Equals(ref Line line1, ref Line line2)
Definition: Line.cs:227
Line(Vector2D normal, Scalar d)
Definition: Line.cs:87
static void Transform(ref Matrix3x3 matrix, ref Line line, out Line result)
Definition: Line.cs:51
void Intersects(ref BoundingRectangle box, out bool result)
Definition: Line.cs:162
void Intersects(ref Ray ray, out Scalar result)
Definition: Line.cs:145
Scalar GetDistance(Vector2D point)
Definition: Line.cs:108
static Line Transform(Matrix2x3 matrix, Line line)
Definition: Line.cs:62
void Intersects(ref BoundingPolygon polygon, out bool result)
Definition: Line.cs:185
bool Equals(Line other)
Definition: Line.cs:219
Line(Vector2D point1, Vector2D point2)
Definition: Line.cs:98
override int GetHashCode()
Definition: Line.cs:211
override string ToString()
Definition: Line.cs:207
bool Intersects(BoundingCircle circle)
Definition: Line.cs:132
bool Intersects(BoundingPolygon polygon)
Definition: Line.cs:138
Scalar Intersects(Ray ray)
Definition: Line.cs:120
override bool Equals(object obj)
Definition: Line.cs:215
static bool Equals(Line line1, Line line2)
Definition: Line.cs:223
A 2x3 matrix which can represent rotations around axes.
Definition: Matrix2x3.cs:62
A 3x3 matrix which can represent rotations around axes.
Definition: Matrix3x3.cs:62
This is the Vector Class.
Definition: Vector2D.cs:50
static readonly Vector2D Zero
Vector2D(0,0)
Definition: Vector2D.cs:69
static Vector2D Multiply(Vector2D source, Scalar scalar)
Does Scaler Multiplication on a Vector2D.
Definition: Vector2D.cs:413
const int Size
The Size of the class in bytes;
Definition: Vector2D.cs:59
Scalar X
This is the X value. (Usually represents a horizontal position or direction.)
Definition: Vector2D.cs:796
static Vector2D Transform(Matrix3x3 matrix, Vector2D source)
Uses a matrix multiplication to Transform the vector.
Definition: Vector2D.cs:325
static Scalar Dot(Vector2D left, Vector2D right)
Does a Dot Operation Also know as an Inner Product.
Definition: Vector2D.cs:445
static Vector2D Normalize(Vector2D source)
This returns the Normalized Vector2D that is passed. This is also known as a Unit Vector.
Definition: Vector2D.cs:615
override int GetHashCode()
Provides a unique hash code based on the member variables of this class. This should be done because ...
Definition: Vector2D.cs:1249
static Vector2D Subtract(Vector2D left, Vector2D right)
Subtracts 2 Vector2Ds.
Definition: Vector2D.cs:306
Scalar Y
This is the Y value. (Usually represents a vertical position or direction.)
Definition: Vector2D.cs:805
override bool Equals(object obj)
Compares this Vector to another object. This should be done because the equality operators (==,...
Definition: Vector2D.cs:1259