-
Notifications
You must be signed in to change notification settings - Fork 2
/
point.go
98 lines (83 loc) · 1.96 KB
/
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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* gouda/point: an n-dimensional point & tools
*
* Copyright (C) 2018 Pawel Foremski <[email protected]>
* Licensed to you under GNU GPL v3
*/
package point
import "fmt"
// Point represents an abstract point in n-dimensional space
type Point struct {
// point coordinates in all dimensions
V []float64
// auxilliary data
D interface{}
}
// New() creates a new point
func New(vals ...float64) *Point {
ret := &Point{}
if vals != nil {
ret.V = vals
} else {
ret.V = make([]float64, 0)
}
return ret
}
// NewD() creates a new point with data D
func NewD(D interface{}, vals ...float64) *Point {
ret := &Point{}
if vals != nil {
ret.V = vals
} else {
ret.V = make([]float64, 0)
}
ret.D = D
return ret
}
// NewZero() creates a new zero point with given number of axes
func NewZero(axes int) *Point {
ret := &Point{}
ret.V = make([]float64, axes)
return ret
}
// Copy() creates a new point by copying another one
func (p *Point) Copy() *Point {
ret := &Point{}
ret.V = make([]float64, len(p.V))
copy(ret.V, p.V)
ret.D = p.D
return ret
}
// Axes() returns the number of axes in p
func (p *Point) Axes() int { return len(p.V) }
// String() converts a Point to a string object
func (p *Point) String() (ret string) {
ret = fmt.Sprintf("%.4g", p.V)
if p.D != nil { ret += fmt.Sprintf("->(%T %v)", p.D, p.D) }
return
}
/********************************************************/
// Points is a collection of memory pointers to points
type Points []*Point
// NewZeros() creates new n zero Points with given number of axes
func NewZeros(n, axes int) Points {
ret := make(Points, n)
for i := range ret {
ret[i] = NewZero(axes)
}
return ret
}
// Zeros() zero-es given slice of points
func (points Points) Zeros() {
for i := range points {
points[i].Zero()
}
}
// Copy() creates new slice of points by copying another one
func (points Points) Copy() Points {
ret := make(Points, len(points))
for i := range points {
ret[i] = points[i].Copy()
}
return ret
}