forked from FreyaHolmer/Mathfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineSegment2D.cs
44 lines (33 loc) · 1.78 KB
/
LineSegment2D.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
// collected and expended upon by Freya Holmér (https://github.com/FreyaHolmer/Mathfs)
using UnityEngine;
namespace Freya {
public struct LineSegment2D {
public Vector2 start;
public Vector2 end;
public LineSegment2D( Vector2 start, Vector2 end ) {
this.start = start;
this.end = end;
}
public Vector2 GetPoint( float t ) => Vector2.LerpUnclamped( start, end, t );
/// <summary>Calculates the length</summary>
public float Length => Vector2.Distance( start, end );
/// <summary>Returns the perpendicular bisector. Note: the returned normal is not normalized to save performance. Use Bisector() if you want to make sure it is normalized</summary>
public Line2D BisectorFast() => GetBisectorFast( start, end );
/// <summary>Returns the perpendicular bisector</summary>
public Line2D Bisector() => GetBisector( start, end );
/// <summary>Returns the perpendicular bisector of the input line segment. Note: the returned normal is not normalized to save performance. Use GetBisector() if you want to make sure it is normalized</summary>
/// <param name="startPoint">Starting point of the line segment</param>
/// <param name="endPoint">Endpoint of the line segment</param>
public static Line2D GetBisectorFast( Vector2 startPoint, Vector2 endPoint ) {
return new Line2D( ( startPoint + endPoint ) * 0.5f, ( startPoint - endPoint ).Rotate90CCW() );
}
/// <summary>Returns the perpendicular bisector of the input line segment</summary>
/// <param name="startPoint">Starting point of the line segment</param>
/// <param name="endPoint">Endpoint of the line segment</param>
public static Line2D GetBisector( Vector2 startPoint, Vector2 endPoint ) {
Line2D line = GetBisectorFast( startPoint, endPoint );
line.dir = line.dir.normalized;
return line;
}
}
}