-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.go
228 lines (199 loc) · 4.35 KB
/
enc.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package bvg
import (
"encoding/binary"
// "fmt"
// "fmt"
"io"
"math"
// "os"
)
// This stores the writer and commands to be written
type Bvg struct {
Writer io.Writer
Reader io.Reader
Points []*Point
Lines []*Line
Circles []*Circle
Triangles []*Triangle
Polys []*Poly
Bezs []*Bez
LineStrips []*LineStrip
}
// A point must lie between (-1,-1) and (1,1)
// anything outside will not be rendered
type Point struct {
X, Y float64
R, G, B, A uint8
}
// Returns a new Point with the given Position
func NewPt(x, y float64) *Point {
return &Point{
X: x,
Y: y,
}
}
func (p *Point) Dist(p1 *Point) float64 {
return math.Sqrt((p.X-p1.X)*(p.X-p1.X) + (p.Y-p1.Y)*(p.Y-p1.Y))
}
func (p *Point) Write(w io.Writer) {
binary.Write(w, binary.LittleEndian, p.X)
binary.Write(w, binary.LittleEndian, p.Y)
binary.Write(w, binary.LittleEndian, p.R)
binary.Write(w, binary.LittleEndian, p.G)
binary.Write(w, binary.LittleEndian, p.B)
binary.Write(w, binary.LittleEndian, p.A)
}
// Returns a new Point with the given position and color
func NewPtCol(x, y float64, r, g, b, a uint8) *Point {
return &Point{
X: x,
Y: y,
R: r,
G: g,
B: b,
A: a,
}
}
func (p *Point) RelPt(l, theta float64) *Point {
return &Point{
X: p.X + l*math.Cos(theta),
Y: p.Y + l*math.Sin(theta),
R: p.R,
G: p.G,
B: p.B,
A: p.A,
}
}
type Line struct {
P1 *Point
P2 *Point
}
func NewLine(p1, p2 *Point) *Line {
return &Line{
P1: p1,
P2: p2,
}
}
type Circle struct {
P *Point
// r is the complete radius of the circle
// the alpha at r is 0
// t is threshold upto which the color of the circle
// does not fade
R, T float64
}
func NewCircle(p *Point, r, t float64) *Circle {
return &Circle{
P: p,
R: r,
T: t,
}
}
type Triangle struct {
P1, P2, P3 *Point
}
func NewTriangle(p1, p2, p3 *Point) *Triangle {
return &Triangle{
P1: p1,
P2: p2,
P3: p3,
}
}
type Poly struct {
Pts []*Point
}
func NewPoly(p ...*Point) *Poly {
return &Poly{
Pts: p,
}
}
type Bez struct {
Pts []*Point
}
func NewBez(Points ...*Point) *Bez {
return &Bez{
Pts: Points,
}
}
type LineStrip struct {
Pts []*Point
}
func NewLineStrip(pts ...*Point) *LineStrip {
return &LineStrip{
Pts: pts,
}
}
// Returns a new bvg struct with the specified writer
func New(writer io.Writer) *Bvg {
return &Bvg{
Writer: writer,
}
}
func (b *Bvg) Encode() {
for _, val := range b.Lines {
b.DrawLine(*val)
}
for _, val := range b.Triangles {
b.DrawTriangle(*val)
}
for _, val := range b.Circles {
b.DrawCircle(*val)
}
for _, val := range b.Polys {
b.DrawPoly(*val)
}
for _, val := range b.LineStrips {
b.DrawLineStrip(*val)
}
}
// Draws a line from point p1 to p2 with the
// colors inside the point
func (b *Bvg) DrawLine(l Line) {
binary.Write(b.Writer, binary.LittleEndian, int8('l'))
l.P1.Write(b.Writer)
l.P2.Write(b.Writer)
}
// draw a triangle from p1, p2, p3
func (b *Bvg) DrawTriangle(t Triangle) {
binary.Write(b.Writer, binary.LittleEndian, int8('t'))
t.P1.Write(b.Writer)
t.P2.Write(b.Writer)
t.P3.Write(b.Writer)
}
// draw a circle with p1 as center and distance between p1
// an p2 as radius with colour gradient from colors of p1 at
// the center to colours of p2 at the circumference
func (b *Bvg) DrawCircle(c Circle) {
binary.Write(b.Writer, binary.LittleEndian, int8('c'))
c.P.Write(b.Writer)
binary.Write(b.Writer, binary.LittleEndian, c.R)
binary.Write(b.Writer, binary.LittleEndian, c.T)
}
// draws a polygon from the points given, it triangulates by
// using a triangle strip from the given points
func (b *Bvg) DrawPoly(p Poly) {
binary.Write(b.Writer, binary.LittleEndian, int8('p'))
binary.Write(b.Writer, binary.LittleEndian, uint32(len(p.Pts)))
// fmt.Println(len(p.Pts))
for _, p := range p.Pts {
p.Write(b.Writer)
}
}
// Draws a bezier curve with 1st and last point being
// Starting and Ending points and the rest inbetween being
// control points
func (b *Bvg) DrawBez(bez Bez) {
binary.Write(b.Writer, binary.LittleEndian, int8('b'))
binary.Write(b.Writer, binary.LittleEndian, uint32(len(bez.Pts)))
for _, p := range bez.Pts {
p.Write(b.Writer)
}
}
// Draw a line strip from the given points
func (b *Bvg) DrawLineStrip(l LineStrip) {
binary.Write(b.Writer, binary.LittleEndian, int8(60))
binary.Write(b.Writer, binary.LittleEndian, uint32(len(l.Pts)))
for _, p := range l.Pts {
p.Write(b.Writer)
}
}