-
Notifications
You must be signed in to change notification settings - Fork 1
/
line.go
42 lines (35 loc) · 880 Bytes
/
line.go
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
package cart
import (
"math"
)
// Line in 2D space
type Line2 struct {
U Vector2
P Point2
}
// Create line, y = mx + c
func NewLine2(m, c float64) *Line2 {
return &Line2{Vector2{1, m}, Point2{0, c}}
}
// Distance between line and point
func (l *Line2) DistPt(p *Point2) float64 {
v := Vector2{l.U[1], -l.U[0]} // vector perpendicular to line
r := l.P.To(p) // vector from point on line to p
return math.Abs(v.Dot(r)) / v.Len() // project r onto v
}
// Line in 3D space
type Line3 struct {
U Vector3
P Point3
}
// Distance between line and point
func (l *Line3) DistPt(p *Point3) float64 {
p2 := l.P.Add(&l.U) // 2nd point on line
return p.To(&l.P).X(p.To(p2)).Len() / l.U.Len()
}
// Distance between two lines
func (l1 *Line3) DistLn(l2 *Line3) float64 {
v := l1.U.X(&l2.U)
r := l1.P.To(&l2.P)
return math.Abs(v.Dot(r)) / v.Len()
}