-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpoint.go
135 lines (118 loc) · 3.18 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
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
125
126
127
128
129
130
131
132
133
134
135
package geo
import (
"database/sql/driver"
"encoding/json"
"fmt"
"math"
"strconv"
)
const (
pointWKTPrefix = `POINT`
pointWKT = `(%f %f)`
pointJSONPrefix = `{"type":"Point","coordinates":[`
)
// Point defines a point.
type Point [3]float64
// Equal compares one point to another.
func (point Point) Equal(g Geometry) bool {
pt, ok := g.(*Point)
if !ok {
return false
}
if point[0] != (*pt)[0] {
return false
}
if point[1] != (*pt)[1] {
return false
}
return true
}
// Contains is the exact same as Equal.
func (point Point) Contains(other Point) bool {
return point.Equal(&other)
}
// DistanceFrom computes the distance from one point to another.
func (point Point) DistanceFrom(other Point) float64 {
return math.Sqrt(math.Pow(point[1]-other[1], 2) + math.Pow(point[0]-other[0], 2))
}
// MarshalJSON returns the GeoJSON representation of the point.
func (point Point) MarshalJSON() ([]byte, error) {
s := pointJSONPrefix
s += strconv.FormatFloat(point[0], 'f', -1, 64) + ","
s += strconv.FormatFloat(point[1], 'f', -1, 64) + "]}"
return []byte(s), nil
}
// RayhIntersects returns true if the horizontal ray going from
// point to positive infinity intersects the line that connects a and b.
func (point Point) RayhIntersects(a, b Point) bool {
var (
left = math.Min(a[0], b[0])
right = math.Max(a[0], b[0])
bottom = math.Min(a[1], b[1])
top = math.Max(a[1], b[1])
)
if point[0] > right {
return false
}
if point[1] >= top || point[1] < bottom {
return false
}
if point[0] <= left {
return true
}
slope := (b[1] - a[1]) / (b[0] - a[0])
if slope >= 0 {
return ((point[1] - bottom) / (point[0] - left)) >= slope
}
return ((point[1] - top) / (point[0] - left)) <= slope
}
// Scan scans a point from Well Known Text.
func (point *Point) Scan(src interface{}) error {
switch v := src.(type) {
case []byte:
if _, err := fmt.Sscanf(string(v), pointWKTPrefix+pointWKT, &point[0], &point[1]); err != nil {
return err
}
case string:
if _, err := fmt.Sscanf(v, pointWKTPrefix+pointWKT, &point[0], &point[1]); err != nil {
return err
}
default:
return fmt.Errorf("could not scan point from %T", src)
}
return nil
}
// String convert the point to a string.
func (point Point) String() string {
s := "POINT("
s += strconv.FormatFloat(point[0], 'f', -1, 64)
s += " " + strconv.FormatFloat(point[1], 'f', -1, 64) + ")"
return s
}
// UnmarshalJSON unmarshals a point from GeoJSON.
func (point *Point) UnmarshalJSON(data []byte) error {
g := geometry{}
// Never fails because data is always valid JSON.
_ = json.Unmarshal(data, &g)
if expected, got := PointType, g.Type; expected != got {
return fmt.Errorf("expected %s type, got %s", expected, got)
}
pt := [3]float64{}
if err := json.Unmarshal(g.Coordinates, &pt); err != nil {
return err
}
*point = pt
return nil
}
// Value converts a point to Well Known Text.
func (point Point) Value() (driver.Value, error) {
return point.String(), nil
}
// Transform transforms the geometry point by point.
func (point *Point) Transform(t Transformer) {
*point = t.Transform(*point)
}
// VisitCoordinates visits each point in the geometry.
func (point Point) VisitCoordinates(v Visitor) {
v.Visit(point)
}