-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.cs
More file actions
80 lines (67 loc) · 1.72 KB
/
Line.cs
File metadata and controls
80 lines (67 loc) · 1.72 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Line
{
Coords A, B, v;
public enum LINETYPE
{
LINE,
SEGMENT,
RAY
}
LINETYPE type;
public Line(Coords _A, Coords _B, LINETYPE _type)
{
A = _A;
B = _B;
type = _type;
v = new Coords(B.x - A.x, B.y - A.y, B.z - A.z);
}
public Line(Coords _A, Coords _V)
{
A = _A;
v = _V;
B = _A + v;
type = LINETYPE.SEGMENT;
}
public float IntersectsAt(Plane p)
{
Coords normal = HolisticMath.Cross(p.u, p.v);
if(HolisticMath.Dot(normal,v) == 0)
{
return float.NaN;
}
float t = HolisticMath.Dot(normal, p.A - A) / HolisticMath.Dot(normal, v);
return t;
}
public float IntersectsAt(Line l)
{
if(HolisticMath.Dot(Coords.Perp(l.v), v) == 0)
{
return float.NaN;
}
Coords c = l.A - this.A;
float t = HolisticMath.Dot(Coords.Perp(l.v), c) / HolisticMath.Dot(Coords.Perp(l.v), v);
if(t < 0 || t > 1 && type == LINETYPE.SEGMENT)
{
return float.NaN;
}
return t;
}
public void Draw(float width, Color col)
{
Coords.DrawLine(A, B, width, col);
}
public Coords Lerp(float t)
{
if (type == LINETYPE.SEGMENT)
t = Mathf.Clamp(t, 0, 1);
else if (type == LINETYPE.RAY && t < 0)
t = 0;
float xt = A.x + v.x * t;
float yt = A.y + v.y * t;
float zt = A.z + v.z * t;
return new Coords(xt, yt, zt);
}
}