forked from FreyaHolmer/Mathfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine2D.cs
More file actions
55 lines (42 loc) · 2.16 KB
/
Copy pathLine2D.cs
File metadata and controls
55 lines (42 loc) · 2.16 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
// collected and expended upon by Freya Holmér (https://github.com/FreyaHolmer/Mathfs)
using UnityEngine;
using static Freya.Mathfs;
namespace Freya {
// 2D line math
public struct Line2D {
public Vector2 origin;
public Vector2 dir;
public Line2D( Vector2 origin, Vector2 dir ) {
this.origin = origin;
this.dir = dir;
}
public Vector2 GetPoint( float t ) => origin + t * dir;
/// <summary>Projects a point onto an infinite line</summary>
/// <param name="lineOrigin">Line origin</param>
/// <param name="lineDir">Line direction (does not have to be normalized)</param>
/// <param name="point">The point to project onto the line</param>
public static Vector2 ProjectPointToLine( Vector2 lineOrigin, Vector2 lineDir, Vector2 point ) {
Vector2 coord = point - lineOrigin;
float t = Vector2.Dot( lineDir, coord ) / Vector2.Dot( lineDir, lineDir );
return lineOrigin + lineDir * t;
}
/// <summary>Projects a point onto an infinite line</summary>
/// <param name="line">Line to project onto</param>
/// <param name="point">The point to project onto the line</param>
public static Vector2 ProjectPointToLine( Line2D line, Vector2 point ) => ProjectPointToLine( line.origin, line.dir, point );
/// <summary>Returns the signed distance to a 2D plane</summary>
/// <param name="planeOrigin">Plane origin</param>
/// <param name="planeNormal">Plane normal (has to be normalized for a true distance)</param>
/// <param name="point">The point to use when checking distance to the plane</param>
public static float PointToPlaneSignedDistance( Vector2 planeOrigin, Vector2 planeNormal, Vector2 point ) {
return Vector2.Dot( point - planeOrigin, planeNormal );
}
/// <summary>Returns the distance to a 2D plane</summary>
/// <param name="planeOrigin">Plane origin</param>
/// <param name="planeNormal">Plane normal (has to be normalized for a true distance)</param>
/// <param name="point">The point to use when checking distance to the plane</param>
public static float PointToPlaneDistance( Vector2 planeOrigin, Vector2 planeNormal, Vector2 point ) {
return Abs( PointToPlaneSignedDistance( planeOrigin, planeNormal, point ) );
}
}
}