-
Notifications
You must be signed in to change notification settings - Fork 1
/
point.go
49 lines (38 loc) · 974 Bytes
/
point.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
43
44
45
46
47
48
49
package cart
import (
"fmt"
)
// Point in 2D space
type Point2 [2]float64
// Vector p1 -> p2
func (p1 *Point2) To(p2 *Point2) *Vector2 {
return &Vector2{p2[0] - p1[0], p2[1] - p1[1]}
}
// Add vector to point
func (p *Point2) Add(v *Vector2) *Point2 {
return &Point2{p[0] + v[0], p[1] + v[1]}
}
// Distance between points
func (p1 *Point2) DistPt(p2 *Point2) float64 {
return p1.To(p2).Len()
}
func (p *Point2) String() string {
return fmt.Sprintf("(%f, %f)", p[0], p[1])
}
// Point in 3D space
type Point3 [3]float64
// Vector p1 -> p2
func (p1 *Point3) To(p2 *Point3) *Vector3 {
return &Vector3{p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]}
}
// Add vector to point
func (p *Point3) Add(v *Vector3) *Point3 {
return &Point3{p[0] + v[0], p[1] + v[1], p[2] + v[2]}
}
// Distance between points
func (p1 *Point3) DistPt(p2 *Point3) float64 {
return p1.To(p2).Len()
}
func (p *Point3) String() string {
return fmt.Sprintf("(%f, %f, %f)", p[0], p[1], p[2])
}