-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline.go
55 lines (43 loc) · 1.02 KB
/
line.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
package pgeo
import (
"database/sql/driver"
"errors"
"fmt"
"regexp"
)
var parseLineRegexp = regexp.MustCompile(`^\{(-?[0-9]+(?:\.[0-9]+)?),(-?[0-9]+(?:\.[0-9]+)?),(-?[0-9]+(?:\.[0-9]+)?)\}$`)
// Line represents a infinite line with the linear equation Ax + By + C = 0, where A and B are not both zero.
type Line struct {
A float64 `json:"a"`
B float64 `json:"b"`
C float64 `json:"c"`
}
func (l Line) Value() (driver.Value, error) {
return valueLine(l)
}
func (l *Line) Scan(src interface{}) error {
return scanLine(l, src)
}
func valueLine(l Line) (driver.Value, error) {
return fmt.Sprintf(`{%[1]v,%[2]v,%[3]v}`, l.A, l.B, l.C), nil
}
func scanLine(l *Line, src interface{}) error {
if src == nil {
*l = NewLine(0, 0, 0)
return nil
}
val, err := iToS(src)
if err != nil {
return err
}
pdzs := parseLineRegexp.FindStringSubmatch(val)
if len(pdzs) != 4 {
return errors.New("wrong line")
}
nums, err := parseNums(pdzs[1:4])
if err != nil {
return err
}
*l = NewLine(nums[0], nums[1], nums[2])
return nil
}