-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawer.go
177 lines (150 loc) · 3.39 KB
/
drawer.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
package main
import (
"errors"
)
// DrawMode defines the type of each drawing mode
type DrawMode int
// Drawer is a struct that draws on an image
type Drawer struct {
frame *Image // underlying image
em *Matrix // edge/polygon matrix
cs *Stack // coordinate system stack
}
func NewDrawer(height, width int) *Drawer {
return &Drawer{
frame: NewImage(height, width),
em: NewMatrix(4, 0),
cs: NewStack(),
}
}
func (d *Drawer) apply() error {
product, err := d.cs.Peek().Multiply(d.em)
if err != nil {
return err
}
d.em = product
return nil
}
func (d *Drawer) DrawLines(c Color) error {
err := d.frame.DrawLines(d.em, c)
d.clear()
return err
}
func (d *Drawer) DrawPolygons(c Color) error {
err := d.frame.DrawPolygons(d.em, c)
d.clear()
return err
}
func (d *Drawer) DrawShadedPolygons(constants [][]float64, lightSources map[string]LightSource) error {
err := d.frame.DrawShadedPolygons(d.em, ambient, constants, lightSources)
d.clear()
return err
}
func (d *Drawer) clear() {
d.em = NewMatrix(4, 0)
}
// Reset clears the image and edge matrix
func (d *Drawer) Reset() {
d.clear()
d.cs = NewStack()
d.frame = NewImage(d.frame.height, d.frame.width)
}
func (d *Drawer) Line(x0, y0, z0, x1, y1, z1 float64) error {
d.em.AddEdge(x0, y0, z0, x1, y1, z1)
err := d.apply()
return err
}
func (d *Drawer) Scale(sx, sy, sz float64) error {
dilation := MakeDilation(sx, sy, sz)
top := d.cs.Pop()
top, err := top.Multiply(dilation)
if err != nil {
return err
}
d.cs.Push(top)
return nil
}
func (d *Drawer) Move(x, y, z float64) error {
translation := MakeTranslation(x, y, z)
top := d.cs.Pop()
top, err := top.Multiply(translation)
if err != nil {
return err
}
d.cs.Push(top)
return nil
}
func (d *Drawer) Rotate(axis string, theta float64) error {
theta = degreesToRadians(theta)
var rotation *Matrix
switch axis {
case "x":
rotation = MakeRotX(theta)
case "y":
rotation = MakeRotY(theta)
case "z":
rotation = MakeRotZ(theta)
default:
return errors.New("axis must be \"x\", \"y\", or \"z\"")
}
top := d.cs.Pop()
top, err := top.Multiply(rotation)
if err != nil {
return err
}
d.cs.Push(top)
return nil
}
func (d *Drawer) Save(filename string) error {
err := d.frame.Save(filename)
return err
}
func (d *Drawer) Display() error {
err := d.frame.Display()
return err
}
func (d *Drawer) Circle(cx, cy, cz, radius float64) error {
d.em.AddCircle(cx, cy, cz, radius)
err := d.apply()
return err
}
func (d *Drawer) Hermite(x0, y0, x1, y1, dx0, dy0, dx1, dy1 float64) error {
d.em.AddHermite(x0, y0, x1, y1, dx0, dy0, dx1, dy1)
err := d.apply()
return err
}
func (d *Drawer) Bezier(x0, y0, x1, y1, x2, y2, x3, y3 float64) error {
d.em.AddBezier(x0, y0, x1, y1, x2, y2, x3, y3)
err := d.apply()
return err
}
func (d *Drawer) Box(x, y, z, width, height, depth float64) error {
d.em.AddBox(x, y, z, width, height, depth)
err := d.apply()
return err
}
func (d *Drawer) Sphere(cx, cy, cz, radius float64) error {
d.em.AddSphere(cx, cy, cz, radius)
err := d.apply()
return err
}
func (d *Drawer) Torus(cx, cy, cz, r1, r2 float64) error {
d.em.AddTorus(cx, cy, cz, r1, r2)
err := d.apply()
return err
}
func (d *Drawer) Pop() {
d.cs.Pop()
}
func (d *Drawer) Push() {
var new *Matrix
if d.cs.IsEmpty() {
new = IdentityMatrix()
} else {
new = d.cs.Peek().Copy()
}
d.cs.Push(new)
}
func (d *Drawer) AddPoint(x, y, z float64) {
d.em.AddPoint(x, y, z)
}