forked from andybalholm/stroke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.go
39 lines (31 loc) · 866 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
package stroke
import "strconv"
// A Point is a two dimensional point.
type Point struct {
X, Y float32
}
// String return a string representation of p.
func (p Point) String() string {
return "(" + strconv.FormatFloat(float64(p.X), 'f', -1, 32) +
"," + strconv.FormatFloat(float64(p.Y), 'f', -1, 32) + ")"
}
// Pt is shorthand for Point{X: x, Y: y}.
func Pt(x, y float32) Point {
return Point{X: x, Y: y}
}
// Add return the point p+p2.
func (p Point) Add(p2 Point) Point {
return Point{X: p.X + p2.X, Y: p.Y + p2.Y}
}
// Sub returns the vector p-p2.
func (p Point) Sub(p2 Point) Point {
return Point{X: p.X - p2.X, Y: p.Y - p2.Y}
}
// Mul returns p scaled by s.
func (p Point) Mul(s float32) Point {
return Point{X: p.X * s, Y: p.Y * s}
}
// Div returns the vector p/s.
func (p Point) Div(s float32) Point {
return Point{X: p.X / s, Y: p.Y / s}
}