-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
triangle.go
219 lines (168 loc) · 4.77 KB
/
triangle.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
package gfx
import (
"image"
"image/color"
"image/draw"
"math"
)
// Triangle is an array of three vertexes
type Triangle [3]Vertex
// NewTriangle creates a new triangle.
func NewTriangle(i int, td *TrianglesData) Triangle {
var t Triangle
t[0].Position = td.Position(i)
t[1].Position = td.Position(i + 1)
t[2].Position = td.Position(i + 2)
t[0].Color = td.Color(i)
t[1].Color = td.Color(i + 1)
t[2].Color = td.Color(i + 2)
return t
}
// T constructs a new triangle based on three vertexes.
func T(a, b, c Vertex) Triangle {
return Triangle{a, b, c}
}
// Positions returns the three positions.
func (t Triangle) Positions() (Vec, Vec, Vec) {
return t[0].Position, t[1].Position, t[2].Position
}
// Colors returns the three colors.
func (t Triangle) Colors() (color.NRGBA, color.NRGBA, color.NRGBA) {
return t[0].Color, t[1].Color, t[2].Color
}
// Bounds returns the bounds of the triangle.
func (t Triangle) Bounds() image.Rectangle {
return t.Rect().Bounds()
}
// Rect returns the triangle Rect.
func (t Triangle) Rect() Rect {
a, b, c := t.Positions()
return R(
math.Min(a.X, math.Min(b.X, c.X)),
math.Min(a.Y, math.Min(b.Y, c.Y)),
math.Max(a.X, math.Max(b.X, c.X)),
math.Max(a.Y, math.Max(b.Y, c.Y)),
)
}
// Color returns the color at vector u.
func (t Triangle) Color(u Vec) color.Color {
o := t.Centroid()
if triangleContains(u, t[0].Position, t[1].Position, o) {
return t[1].Color
}
if triangleContains(u, t[1].Position, t[2].Position, o) {
return t[2].Color
}
return t[0].Color
}
// Contains returns true if the given vector is inside the triangle.
func (t Triangle) Contains(u Vec) bool {
a, b, c := t.Positions()
return triangleContains(u, a, b, c)
}
func triangleContains(u, a, b, c Vec) bool {
vs1 := b.Sub(a)
vs2 := c.Sub(a)
q := u.Sub(a)
bs := q.Cross(vs2) / vs1.Cross(vs2)
bt := vs1.Cross(q) / vs1.Cross(vs2)
return bs >= 0 && bt >= 0 && bs+bt <= 1
}
// Centroid returns the centroid O of the triangle.
func (t Triangle) Centroid() Vec {
a, b, c := t.Positions()
return V(
(a.X+b.X+c.X)/3,
(a.Y+b.Y+c.Y)/3,
)
}
// TriangleFunc is a function type that is called by Triangle.EachPixel
type TriangleFunc func(u Vec, t Triangle)
// EachPixel calls the given TriangleFunc for each pixel in the triangle.
func (t Triangle) EachPixel(tf TriangleFunc) {
b := t.Bounds()
for x := b.Min.X; x < b.Max.X; x++ {
for y := b.Min.Y; y < b.Max.Y; y++ {
if u := IV(x, y); t.Contains(u) {
tf(u, t)
}
}
}
}
// Draw the triangle to dst.
func (t Triangle) Draw(dst draw.Image) (drawCount int) {
b := t.Bounds()
for x := b.Min.X; x < b.Max.X; x++ {
for y := b.Min.Y; y < b.Max.Y; y++ {
if u := IV(x, y); t.Contains(u) {
drawCount++
SetVec(dst, u, t.Color(u))
}
}
}
return drawCount
}
// DrawOver draws the first color in the triangle over dst.
func (t Triangle) DrawOver(dst draw.Image) (drawCount int) {
a, _, _ := t.Colors()
return t.DrawColorOver(dst, a)
}
// DrawColor draws the triangle on dst using the given color.
func (t Triangle) DrawColor(dst draw.Image, c color.Color) (drawCount int) {
return t.drawColor(dst, c, draw.Src)
}
// DrawColorOver draws the triangle over dst using the given color.
func (t Triangle) DrawColorOver(dst draw.Image, c color.Color) (drawCount int) {
return t.drawColor(dst, c, draw.Over)
}
func (t Triangle) drawColor(dst draw.Image, c color.Color, op draw.Op) (drawCount int) {
b := t.Bounds()
var lefts, rights []Vec
var invalid = V(-math.MaxInt64, 0)
for y := b.Min.Y; y < b.Max.Y; y++ {
var left, right = invalid, invalid
for x := b.Min.X; x < b.Max.X; x++ {
if u := IV(x, y); t.Contains(u) {
left = u
break
}
}
for x := b.Max.X; x > b.Min.X; x-- {
if u := IV(x, y); t.Contains(u) {
right = u
break
}
}
if left != invalid && right != invalid {
lefts = append(lefts, left)
rights = append(rights, right)
}
}
uc := NewUniform(c)
for i := 0; i < len(lefts); i++ {
r := NewRect(lefts[i], rights[i].AddXY(0, 1)).Bounds()
draw.Draw(dst, r, uc, ZP, op)
drawCount++
}
return drawCount
}
// DrawWireframe draws the triangle as a wireframe on dst.
func (t Triangle) DrawWireframe(dst draw.Image, c color.Color) (drawCount int) {
if l := t[0].Position.To(t[1].Position).Len(); l > 25 {
DrawLine(dst, t[0].Position, t[1].Position, l/50, ColorWithAlpha(c, 128))
drawCount++
}
if l := t[1].Position.To(t[2].Position).Len(); l > 25 {
DrawLine(dst, t[1].Position, t[2].Position, l/50, ColorWithAlpha(c, 128))
drawCount++
}
if l := t[0].Position.To(t[2].Position).Len(); l > 25 {
DrawLine(dst, t[0].Position, t[2].Position, l/50, ColorWithAlpha(c, 128))
drawCount++
}
DrawLine(dst, t[0].Position, t[1].Position, 1, c)
DrawLine(dst, t[1].Position, t[2].Position, 1, c)
DrawLine(dst, t[0].Position, t[2].Position, 1, c)
drawCount += 3
return
}