-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathLine2D.cs
More file actions
311 lines (276 loc) · 11.8 KB
/
Line2D.cs
File metadata and controls
311 lines (276 loc) · 11.8 KB
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.Diagnostics.Contracts;
using System.Xml.Schema;
using System.Xml;
using System.Xml.Serialization;
using MathNet.Numerics;
using MathNet.Spatial.Internals;
using MathNet.Spatial.Units;
using System.Xml.Linq;
namespace MathNet.Spatial.Euclidean
{
/// <summary>
/// This structure represents a line between two points in 2-space. It allows for operations such as
/// computing the length, direction, projections to, comparisons, and shifting by a vector.
/// </summary>
[Serializable]
public struct Line2D : IEquatable<Line2D>, IXmlSerializable
{
/// <summary>
/// The starting point of the line segment
/// </summary>
public Point2D StartPoint { get; private set; }
/// <summary>
/// The end point of the line segment
/// </summary>
public Point2D EndPoint { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="Line2D"/> struct.
/// Throws an ArgumentException if the <paramref name="startPoint"/> is equal to the <paramref name="endPoint"/>.
/// </summary>
/// <param name="startPoint">the starting point of the line segment.</param>
/// <param name="endPoint">the ending point of the line segment</param>
public Line2D(Point2D startPoint, Point2D endPoint)
{
if (startPoint == endPoint)
{
throw new ArgumentException("The Line2D starting and ending points cannot be identical");
}
StartPoint = startPoint;
EndPoint = endPoint;
}
/// <summary>
/// Gets the distance from <see cref="StartPoint"/> to <see cref="EndPoint"/>
/// </summary>
[Pure]
public double Length => StartPoint.DistanceTo(EndPoint);
/// <summary>
/// Gets a normalized vector in the direction from <see cref="StartPoint"/> to <see cref="EndPoint"/>
/// </summary>
[Pure]
public Vector2D Direction => StartPoint.VectorTo(EndPoint).Normalize();
/// <summary>
/// Returns a value that indicates whether each pair of elements in two specified lines is equal.
/// </summary>
/// <param name="left">The first line to compare</param>
/// <param name="right">The second line to compare</param>
/// <returns>True if the lines are the same; otherwise false.</returns>
public static bool operator ==(Line2D left, Line2D right)
{
return left.Equals(right);
}
/// <summary>
/// Returns a value that indicates whether any pair of elements in two specified lines is not equal.
/// </summary>
/// <param name="left">The first line to compare</param>
/// <param name="right">The second line to compare</param>
/// <returns>True if the lines are different; otherwise false.</returns>
public static bool operator !=(Line2D left, Line2D right)
{
return !left.Equals(right);
}
/// <summary>
/// Adds a vector to the start point and end point of the line
/// </summary>
/// <param name="offset">The vector to add</param>
/// <param name="line">The line</param>
/// <returns>A new <see cref="Line2D"/> at the adjusted points</returns>
public static Line2D operator +(Vector2D offset, Line2D line)
{
return new Line2D(line.StartPoint + offset, line.EndPoint + offset);
}
/// <summary>
/// Adds a vector to the start point and end point of the line
/// </summary>
/// <param name="line">The line</param>
/// <param name="offset">The vector to add</param>
/// <returns>A new line at the adjusted points</returns>
public static Line2D operator +(Line2D line, Vector2D offset)
{
return offset + line;
}
/// <summary>
/// Subtracts a vector from the start point and end point of the line
/// </summary>
/// <param name="line">The line</param>
/// <param name="offset">The vector to subtract</param>
/// <returns>A new line at the adjusted points</returns>
public static Line2D operator -(Line2D line, Vector2D offset)
{
return line + (-offset);
}
/// <summary>
/// Returns a new <see cref="Line2D"/> from a pair of strings which represent points.
/// See <see cref="Point2D.Parse(string, IFormatProvider)" /> for details on acceptable formats.
/// </summary>
/// <param name="startPointString">The string representation of the first point.</param>
/// <param name="endPointString">The string representation of the second point.</param>
/// <returns>A line segment from the first point to the second point.</returns>
public static Line2D Parse(string startPointString, string endPointString)
{
return new Line2D(Point2D.Parse(startPointString), Point2D.Parse(endPointString));
}
/// <summary>
/// Returns the straight line Distance to the given point.
/// </summary>
/// <param name="p">the given point</param>
/// <returns>a distance measure</returns>
[Pure]
public double DistanceTo(Point2D p)
{
var closestPoint = ClosestPointTo(p, false); // this is Line2D, not LineSegment2D.
var result = closestPoint.DistanceTo(p);
return result;
}
/// <summary>
/// Returns the shortest line between this line and a point.
/// </summary>
/// <param name="p">the point to create a line to</param>
/// <param name="mustStartBetweenAndEnd">If false the start point can extend beyond the start and endpoint of the line</param>
/// <returns>The shortest line between the line and the point</returns>
[Pure]
public Line2D LineTo(Point2D p, bool mustStartBetweenAndEnd)
{
return new Line2D(ClosestPointTo(p, mustStartBetweenAndEnd), p);
}
/// <summary>
/// Returns the closest point on the line to the given point.
/// </summary>
/// <param name="p">The point that the returned point is the closest point on the line to</param>
/// <param name="mustBeOnSegment">If true the returned point is contained by the segment ends, otherwise it can be anywhere on the projected line</param>
/// <returns>The closest point on the line to the provided point</returns>
[Pure]
public Point2D ClosestPointTo(Point2D p, bool mustBeOnSegment)
{
var v = StartPoint.VectorTo(p);
var dotProduct = v.DotProduct(Direction);
if (mustBeOnSegment)
{
if (dotProduct < 0)
{
dotProduct = 0;
}
var l = Length;
if (dotProduct > l)
{
dotProduct = l;
}
}
var alongVector = dotProduct * Direction;
return StartPoint + alongVector;
}
/// <summary>
/// Compute the intersection between two lines with parallelism considered by the double floating point precision
/// on the cross product of the two directions.
/// </summary>
/// <param name="other">The other line to compute the intersection with</param>
/// <returns>The point at the intersection of two lines, or null if the lines are parallel.</returns>
[Pure]
public Point2D? IntersectWith(Line2D other)
{
if (IsParallelTo(other))
{
return null;
}
// http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
var p = StartPoint;
var q = other.StartPoint;
var r = StartPoint.VectorTo(EndPoint);
var s = other.StartPoint.VectorTo(other.EndPoint);
var t = (q - p).CrossProduct(s) / r.CrossProduct(s);
return p + (t * r);
}
/// <summary>
/// Compute the intersection between two lines if the angle between them is greater than a specified
/// angle tolerance.
/// </summary>
/// <param name="other">The other line to compute the intersection with</param>
/// <param name="tolerance">The tolerance used when checking if the lines are parallel</param>
/// <returns>The point at the intersection of two lines, or null if the lines are parallel.</returns>
[Pure]
public Point2D? IntersectWith(Line2D other, Angle tolerance)
{
if (IsParallelTo(other, tolerance))
{
return null;
}
// http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
var p = StartPoint;
var q = other.StartPoint;
var r = StartPoint.VectorTo(EndPoint);
var s = other.StartPoint.VectorTo(other.EndPoint);
var t = (q - p).CrossProduct(s) / r.CrossProduct(s);
return p + (t * r);
}
/// <summary>
/// Checks to determine whether or not two lines are parallel to each other, using the dot product within
/// the double precision specified in the MathNet.Numerics package.
/// </summary>
/// <param name="other">The other line to check this one against</param>
/// <returns>True if the lines are parallel, false if they are not</returns>
[Pure]
public bool IsParallelTo(Line2D other)
{
return Direction.IsParallelTo(other.Direction, Precision.DoublePrecision * 2);
}
/// <summary>
/// Checks to determine whether or not two lines are parallel to each other within a specified angle tolerance
/// </summary>
/// <param name="other">The other line to check this one against</param>
/// <param name="tolerance">If the angle between line directions is less than this value, the method returns true</param>
/// <returns>True if the lines are parallel within the angle tolerance, false if they are not</returns>
[Pure]
public bool IsParallelTo(Line2D other, Angle tolerance)
{
return Direction.IsParallelTo(other.Direction, tolerance);
}
/// <inheritdoc/>
[Pure]
public override string ToString()
{
return $"StartPoint: {StartPoint}, EndPoint: {EndPoint}";
}
/// <inheritdoc/>
[Pure]
public bool Equals(Line2D other)
{
return StartPoint.Equals(other.StartPoint) && EndPoint.Equals(other.EndPoint);
}
/// <inheritdoc />
[Pure]
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is Line2D d && Equals(d);
}
/// <inheritdoc />
[Pure]
public override int GetHashCode()
{
unchecked
{
return (StartPoint.GetHashCode() * 397) ^ EndPoint.GetHashCode();
}
}
/// <inheritdoc />
XmlSchema IXmlSerializable.GetSchema() => null;
/// <inheritdoc/>
void IXmlSerializable.ReadXml(XmlReader reader)
{
reader.MoveToContent();
var e = (XElement)XNode.ReadFrom(reader);
this = new Line2D(
Point2D.ReadFrom(e.SingleElement("StartPoint").CreateReader()),
Point2D.ReadFrom(e.SingleElement("EndPoint").CreateReader()));
}
/// <inheritdoc />
void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteElement("StartPoint", StartPoint);
writer.WriteElement("EndPoint", EndPoint);
}
}
}