aboutsummaryrefslogtreecommitdiff
path: root/units.go
blob: 1acd3e1726f2111354d611876457c03839662654 (plain)
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
package robo

import (
	"fmt"
	"strings"
)

const (
	MM = Unit(20.0)
	CM = 10 * MM
	M  = 100 * CM
	IN = 25.4 * MM
	FT = 12 * IN
	PT = IN / 72
)

var A4 = Point{272 * MM, 200 * MM}

type Unit float64

func (u Unit) String() string {
	if u == Unit(int(u)) {
		return fmt.Sprint(int(u))
	} else {
		return fmt.Sprintf("%.3f", u)
	}
}

func parseUnit(s string) (u Unit) {
	fmt.Sscanf(s, "%v", &u)
	return
}

type Orientation int

const (
	Portrait Orientation = iota
	Landscape
)

var orientation = Portrait

type Point struct {
	X, Y Unit
}

func (p Point) String() (s string) {
	switch orientation {
	case Portrait:
		s = fmt.Sprintf("%v,%v", p.X, p.Y)
	case Landscape:
		s = fmt.Sprintf("%v,%v", p.Y, p.X)
	}
	return
}

func (p Point) Scale(f Unit) Point {
	return Point{p.X * f, p.Y * f}
}

func parsePoint(s string) (p Point) {
	fmt.Sscanf(s, "%v,%v", &p.X, &p.Y)
	return
}

type Triple struct {
	U, V, W Unit
}

func (t Triple) String() string {
	return fmt.Sprintf("%v,%v,%v", t.U, t.V, t.W)
}

func parseTriple(s string) (t Triple) {
	fmt.Sscanf(s, "%v,%v,%v", &t.U, &t.V, &t.W)
	return
}

type Path []Point

func (p Path) String() string {
	pp := make([]string, len(p))
	for i, pt := range p {
		pp[i] = pt.String()
	}
	return strings.Join(pp, ",")
}

func (ph Path) Scale(f Unit) Path {
	ret := make(Path, len(ph))
	for i, p := range ph {
		ret[i] = p.Scale(f)
	}
	return ret
}

type LineStyle int

const (
	Solid LineStyle = iota
	Dots
	ShortDash
	Dash
	LongDash
	DashDot
	DashLongDot
	DashDoubleDot
	DashLongDoubleDot
)

type Direction byte

const (
	Stop Direction = 1 << iota >> 1
	Down
	Up
	Right
	Left
)