-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathLine2.cs
130 lines (107 loc) · 3.78 KB
/
Line2.cs
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
using System;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Representation of a 2D line
/// </summary>
[Serializable]
public struct Line2 : IEquatable<Line2>, IFormattable
{
public Vector2 origin;
public Vector2 direction;
public static Line2 xAxis => new Line2(Vector2.zero, Vector2.right);
public static Line2 yAxis => new Line2(Vector2.zero, Vector2.up);
public Line2(Ray2D ray)
{
origin = ray.origin;
direction = ray.direction;
}
public Line2(Vector2 origin, Vector2 direction)
{
this.origin = origin;
this.direction = direction;
}
/// <summary>
/// Returns a point at <paramref name="distance"/> units from origin along the line
/// </summary>
public Vector2 GetPoint(float distance)
{
return origin + direction*distance;
}
/// <summary>
/// Linearly interpolates between two lines
/// </summary>
public static Line2 Lerp(Line2 a, Line2 b, float t)
{
t = Mathf.Clamp01(t);
return new Line2(a.origin + (b.origin - a.origin)*t, a.direction + (b.direction - a.direction)*t);
}
/// <summary>
/// Linearly interpolates between two lines without clamping the interpolant
/// </summary>
public static Line2 LerpUnclamped(Line2 a, Line2 b, float t)
{
return new Line2(a.origin + (b.origin - a.origin)*t, a.direction + (b.direction - a.direction)*t);
}
#region Casting operators
public static explicit operator Line2(Ray2D ray)
{
return new Line2(ray);
}
public static explicit operator Ray2D(Line2 line)
{
return new Ray2D(line.origin, line.direction);
}
public static explicit operator Ray(Line2 line)
{
return new Ray((Vector3) line.origin, (Vector3) line.direction);
}
public static explicit operator Line3(Line2 line)
{
return new Line3((Vector3) line.origin, (Vector3) line.direction);
}
#endregion Casting operators
public static Line2 operator +(Line2 line, Vector2 vector)
{
return new Line2(line.origin + vector, line.direction);
}
public static Line2 operator -(Line2 line, Vector2 vector)
{
return new Line2(line.origin - vector, line.direction);
}
public static bool operator ==(Line2 a, Line2 b)
{
return a.origin == b.origin && a.direction == b.direction;
}
public static bool operator !=(Line2 a, Line2 b)
{
return !(a == b);
}
public override int GetHashCode()
{
return origin.GetHashCode() ^ (direction.GetHashCode() << 2);
}
public override bool Equals(object other)
{
return other is Line2 && Equals((Line2) other);
}
public bool Equals(Line2 other)
{
return origin.Equals(other.origin) && direction.Equals(other.direction);
}
public override string ToString()
{
return string.Format("Line2(origin: {0}, direction: {1})", origin, direction);
}
public string ToString(string format)
{
return string.Format("Line2(origin: {0}, direction: {1})", origin.ToString(format), direction.ToString(format));
}
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format("Line2(origin: {0}, direction: {1})", origin.ToString(format, formatProvider),
direction.ToString(format, formatProvider));
}
}
}