-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRay.go
More file actions
41 lines (35 loc) · 996 Bytes
/
Ray.go
File metadata and controls
41 lines (35 loc) · 996 Bytes
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
package types
// Ray is a line extending infinitely in one direction.
type Ray struct {
Origin Vector3
Direction Vector3
}
// ClosestPoint returns the position on the ray that is nearest to *point*.
func (r Ray) ClosestPoint(point Vector3) Vector3 {
e := r.Direction.Dot(point.Sub(r.Origin))
if e <= 0 {
return r.Origin
}
return r.Direction.MulN(e).Add(r.Origin)
}
// Distance returns the distance between *point* and the point on the ray
// nearest to *point*.
func (r Ray) Distance(point Vector3) float64 {
return r.ClosestPoint(point).Sub(r.Origin).Magnitude()
}
// Type returns a string that identifies the type.
func (Ray) Type() string {
return "Ray"
}
// String returns a human-readable string representation of the value.
func (r Ray) String() string {
var b []byte
b = append(b, r.Origin.String()...)
b = append(b, "; "...)
b = append(b, r.Direction.String()...)
return string(b)
}
// Copy returns a copy of the value.
func (r Ray) Copy() PropValue {
return r
}