/
main.go
61 lines (50 loc) · 1.01 KB
/
main.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
// Package gpc parses GPC format files.
package gpc
import (
"fmt"
"io"
"os"
"github.com/go-gl/mathgl/mgl64"
)
// Polygon is a polygon.
type Polygon struct {
Contours []Contour
}
// Contour is a contour.
type Contour struct {
Vertices []mgl64.Vec2
}
// Parse parses a GPC format reader.
func Parse(r io.Reader) (Polygon, error) {
var p Polygon
var nc uint64
_, err := fmt.Fscan(r, &nc)
if err != nil {
return p, err
}
p.Contours = make([]Contour, nc)
for ci := range p.Contours {
var nv uint64
_, err := fmt.Fscan(r, &nv)
if err != nil {
return p, err
}
p.Contours[ci].Vertices = make([]mgl64.Vec2, nv)
for vi := range p.Contours[ci].Vertices {
_, err := fmt.Fscan(r, &p.Contours[ci].Vertices[vi][0], &p.Contours[ci].Vertices[vi][1])
if err != nil {
return p, err
}
}
}
return p, nil
}
// ParseFile parses a GPC format file.
func ParseFile(path string) (Polygon, error) {
f, err := os.Open(path)
if err != nil {
return Polygon{}, err
}
defer f.Close()
return Parse(f)
}