summaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/code.google.com/p/draw2d/draw2d/path_storage.go
blob: c2a887037ae81920430b87329f45f17bdf0026c8 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2010 The draw2d Authors. All rights reserved.
// created: 21/11/2010 by Laurent Le Goff

package draw2d

import (
	"fmt"
	"math"
)

type PathCmd int

const (
	MoveTo PathCmd = iota
	LineTo
	QuadCurveTo
	CubicCurveTo
	ArcTo
	Close
)

type PathStorage struct {
	commands []PathCmd
	vertices []float64
	x, y     float64
}

func NewPathStorage() (p *PathStorage) {
	p = new(PathStorage)
	p.commands = make([]PathCmd, 0, 256)
	p.vertices = make([]float64, 0, 256)
	return
}

func (p *PathStorage) Clear() {
	p.commands = p.commands[0:0]
	p.vertices = p.vertices[0:0]
	return
}

func (p *PathStorage) appendToPath(cmd PathCmd, vertices ...float64) {
	if cap(p.vertices) <= len(p.vertices)+6 {
		a := make([]PathCmd, len(p.commands), cap(p.commands)+256)
		b := make([]float64, len(p.vertices), cap(p.vertices)+256)
		copy(a, p.commands)
		p.commands = a
		copy(b, p.vertices)
		p.vertices = b
	}
	p.commands = p.commands[0 : len(p.commands)+1]
	p.commands[len(p.commands)-1] = cmd
	copy(p.vertices[len(p.vertices):len(p.vertices)+len(vertices)], vertices)
	p.vertices = p.vertices[0 : len(p.vertices)+len(vertices)]
}

func (src *PathStorage) Copy() (dest *PathStorage) {
	dest = new(PathStorage)
	dest.commands = make([]PathCmd, len(src.commands))
	copy(dest.commands, src.commands)
	dest.vertices = make([]float64, len(src.vertices))
	copy(dest.vertices, src.vertices)
	return dest
}

func (p *PathStorage) LastPoint() (x, y float64) {
	return p.x, p.y
}

func (p *PathStorage) IsEmpty() bool {
	return len(p.commands) == 0
}

func (p *PathStorage) Close() *PathStorage {
	p.appendToPath(Close)
	return p
}

func (p *PathStorage) MoveTo(x, y float64) *PathStorage {
	p.appendToPath(MoveTo, x, y)
	p.x = x
	p.y = y
	return p
}

func (p *PathStorage) RMoveTo(dx, dy float64) *PathStorage {
	x, y := p.LastPoint()
	p.MoveTo(x+dx, y+dy)
	return p
}

func (p *PathStorage) LineTo(x, y float64) *PathStorage {
	p.appendToPath(LineTo, x, y)
	p.x = x
	p.y = y
	return p
}

func (p *PathStorage) RLineTo(dx, dy float64) *PathStorage {
	x, y := p.LastPoint()
	p.LineTo(x+dx, y+dy)
	return p
}

func (p *PathStorage) QuadCurveTo(cx, cy, x, y float64) *PathStorage {
	p.appendToPath(QuadCurveTo, cx, cy, x, y)
	p.x = x
	p.y = y
	return p
}

func (p *PathStorage) RQuadCurveTo(dcx, dcy, dx, dy float64) *PathStorage {
	x, y := p.LastPoint()
	p.QuadCurveTo(x+dcx, y+dcy, x+dx, y+dy)
	return p
}

func (p *PathStorage) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) *PathStorage {
	p.appendToPath(CubicCurveTo, cx1, cy1, cx2, cy2, x, y)
	p.x = x
	p.y = y
	return p
}

func (p *PathStorage) RCubicCurveTo(dcx1, dcy1, dcx2, dcy2, dx, dy float64) *PathStorage {
	x, y := p.LastPoint()
	p.CubicCurveTo(x+dcx1, y+dcy1, x+dcx2, y+dcy2, x+dx, y+dy)
	return p
}

func (p *PathStorage) ArcTo(cx, cy, rx, ry, startAngle, angle float64) *PathStorage {
	endAngle := startAngle + angle
	clockWise := true
	if angle < 0 {
		clockWise = false
	}
	// normalize
	if clockWise {
		for endAngle < startAngle {
			endAngle += math.Pi * 2.0
		}
	} else {
		for startAngle < endAngle {
			startAngle += math.Pi * 2.0
		}
	}
	startX := cx + math.Cos(startAngle)*rx
	startY := cy + math.Sin(startAngle)*ry
	if len(p.commands) > 0 {
		p.LineTo(startX, startY)
	} else {
		p.MoveTo(startX, startY)
	}
	p.appendToPath(ArcTo, cx, cy, rx, ry, startAngle, angle)
	p.x = cx + math.Cos(endAngle)*rx
	p.y = cy + math.Sin(endAngle)*ry
	return p
}

func (p *PathStorage) RArcTo(dcx, dcy, rx, ry, startAngle, angle float64) *PathStorage {
	x, y := p.LastPoint()
	p.ArcTo(x+dcx, y+dcy, rx, ry, startAngle, angle)
	return p
}

func (p *PathStorage) String() string {
	s := ""
	j := 0
	for _, cmd := range p.commands {
		switch cmd {
		case MoveTo:
			s += fmt.Sprintf("MoveTo: %f, %f\n", p.vertices[j], p.vertices[j+1])
			j = j + 2
		case LineTo:
			s += fmt.Sprintf("LineTo: %f, %f\n", p.vertices[j], p.vertices[j+1])
			j = j + 2
		case QuadCurveTo:
			s += fmt.Sprintf("QuadCurveTo: %f, %f, %f, %f\n", p.vertices[j], p.vertices[j+1], p.vertices[j+2], p.vertices[j+3])
			j = j + 4
		case CubicCurveTo:
			s += fmt.Sprintf("CubicCurveTo: %f, %f, %f, %f, %f, %f\n", p.vertices[j], p.vertices[j+1], p.vertices[j+2], p.vertices[j+3], p.vertices[j+4], p.vertices[j+5])
			j = j + 6
		case ArcTo:
			s += fmt.Sprintf("ArcTo: %f, %f, %f, %f, %f, %f\n", p.vertices[j], p.vertices[j+1], p.vertices[j+2], p.vertices[j+3], p.vertices[j+4], p.vertices[j+5])
			j = j + 6
		case Close:
			s += "Close\n"
		}
	}
	return s
}