This repository was archived by the owner on Apr 2, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.go
More file actions
124 lines (105 loc) · 2.52 KB
/
point.go
File metadata and controls
124 lines (105 loc) · 2.52 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package curve
import (
"fmt"
"math"
)
type Point struct {
X float64
Y float64
}
// Pt returns the point (x, y).
func Pt(x, y float64) Point {
return Point{X: x, Y: y}
}
func (pt Point) Splat() (float64, float64) {
return pt.X, pt.Y
}
func (pt Point) String() string {
return fmt.Sprintf("(%g, %g)", pt.X, pt.Y)
}
func (pt Point) Translate(o Vec2) Point {
return Point{
X: pt.X + o.X,
Y: pt.Y + o.Y,
}
}
func (pt Point) Transform(aff Affine) Point {
return Point{
X: aff.N0*pt.X + aff.N2*pt.Y + aff.N4,
Y: aff.N1*pt.X + aff.N3*pt.Y + aff.N5,
}
}
// Sub computes p−o.
// To subtract a vector from p, use Add and negate the vector.
func (pt Point) Sub(o Point) Vec2 {
return Vec2{
X: pt.X - o.X,
Y: pt.Y - o.Y,
}
}
// Lerp linearly interpolates between two points.
func (pt Point) Lerp(o Point, t float64) Point {
return Point(Vec2(pt).Lerp(Vec2(o), t))
}
// Midpoint returns the midpoint of two points.
func (pt Point) Midpoint(o Point) Point {
return Point{
X: 0.5 * (pt.X + o.X),
Y: 0.5 * (pt.Y + o.Y),
}
}
// Distance returns the euclidean distance between two points.
func (pt Point) Distance(o Point) float64 {
x := pt.X - o.X
y := pt.Y - o.Y
return math.Hypot(x, y)
}
// DistanceSquared returns the squared euclidean distance between two points.
func (pt Point) DistanceSquared(o Point) float64 {
x := pt.X - o.X
y := pt.Y - o.Y
return x*x + y*y
}
// Round returns a new point with x and y rounded to the nearest integers.
func (pt Point) Round() Point {
return Point{
X: math.Round(pt.X),
Y: math.Round(pt.Y),
}
}
// Ceil returns a new point with x and y rounded up to the nearest integers.
func (pt Point) Ceil() Point {
return Point{
X: math.Ceil(pt.X),
Y: math.Ceil(pt.Y),
}
}
// Floor returns a new point with x and y rounded down to the nearest integers.
func (pt Point) Floor() Point {
return Point{
X: math.Floor(pt.X),
Y: math.Floor(pt.Y),
}
}
// Expand returns a new point with x and y rounded away from zero to the nearest integers.
func (pt Point) Expand() Point {
return Point{
X: expand(pt.X),
Y: expand(pt.Y),
}
}
// Trunc returns a new point with x and y rounded towards zero to the nearest integers.
func (pt Point) Trunc() Point {
return Point{
X: math.Trunc(pt.X),
Y: math.Trunc(pt.Y),
}
}
// IsInf reports whether at least one of x and y is infinite.
func (pt Point) IsInf() bool {
return math.IsInf(pt.X, 0) || math.IsInf(pt.Y, 0)
}
// IsNaN reports whether at least one of x and y is NaN.
func (pt Point) IsNaN() bool {
return math.IsNaN(pt.X) || math.IsNaN(pt.Y)
}