Debian Codegraph @

vendor/github.com/xyproto/gfx/triangle.go

raw
1 package gfx
2
3 import (
4 "image"
5 "image/color"
6 "image/draw"
7 "math"
8 )
9
10 // Triangle is an array of three vertexes
11 type Triangle [3]Vertex
12
13 // NewTriangle creates a new triangle.
14 func NewTriangle(i int, td *TrianglesData) Triangle {
15 var t Triangle
16
17 t[0].Position = td.Position(i)
18 t[1].Position = td.Position(i + 1)
19 t[2].Position = td.Position(i + 2)
20
21 t[0].Color = td.Color(i)
22 t[1].Color = td.Color(i + 1)
23 t[2].Color = td.Color(i + 2)
24
25 return t
26 }
27
28 // T constructs a new triangle based on three vertexes.
29 func T(a, b, c Vertex) Triangle {
30 return Triangle{a, b, c}
31 }
32
33 // Positions returns the three positions.
34 func (t Triangle) Positions() (Vec, Vec, Vec) {
35 return t[0].Position, t[1].Position, t[2].Position
36 }
37
38 // Colors returns the three colors.
39 func (t Triangle) Colors() (color.NRGBA, color.NRGBA, color.NRGBA) {
40 return t[0].Color, t[1].Color, t[2].Color
41 }
42
43 // Bounds returns the bounds of the triangle.
44 func (t Triangle) Bounds() image.Rectangle {
45 return t.Rect().Bounds()
46 }
47
48 // Rect returns the triangle Rect.
49 func (t Triangle) Rect() Rect {
50 a, b, c := t.Positions()
51
52 return R(
53 math.Min(a.X, math.Min(b.X, c.X)),
54 math.Min(a.Y, math.Min(b.Y, c.Y)),
55 math.Max(a.X, math.Max(b.X, c.X)),
56 math.Max(a.Y, math.Max(b.Y, c.Y)),
57 )
58 }
59
60 // Color returns the color at vector u.
61 func (t Triangle) Color(u Vec) color.Color {
62 o := t.Centroid()
63
64 if triangleContains(u, t[0].Position, t[1].Position, o) {
65 return t[1].Color
66 }
67
68 if triangleContains(u, t[1].Position, t[2].Position, o) {
69 return t[2].Color
70 }
71
72 return t[0].Color
73 }
74
75 // Contains returns true if the given vector is inside the triangle.
76 func (t Triangle) Contains(u Vec) bool {
77 a, b, c := t.Positions()
78
79 return triangleContains(u, a, b, c)
80 }
81
82 func triangleContains(u, a, b, c Vec) bool {
83 vs1 := b.Sub(a)
84 vs2 := c.Sub(a)
85
86 q := u.Sub(a)
87
88 bs := q.Cross(vs2) / vs1.Cross(vs2)
89 bt := vs1.Cross(q) / vs1.Cross(vs2)
90
91 return bs >= 0 && bt >= 0 && bs+bt <= 1
92 }
93
94 // Centroid returns the centroid O of the triangle.
95 func (t Triangle) Centroid() Vec {
96 a, b, c := t.Positions()
97
98 return V(
99 (a.X+b.X+c.X)/3,
100 (a.Y+b.Y+c.Y)/3,
101 )
102 }
103
104 // TriangleFunc is a function type that is called by Triangle.EachPixel
105 type TriangleFunc func(u Vec, t Triangle)
106
107 // EachPixel calls the given TriangleFunc for each pixel in the triangle.
108 func (t Triangle) EachPixel(tf TriangleFunc) {
109 b := t.Bounds()
110
111 for x := b.Min.X; x < b.Max.X; x++ {
112 for y := b.Min.Y; y < b.Max.Y; y++ {
113 if u := IV(x, y); t.Contains(u) {
114 tf(u, t)
115 }
116 }
117 }
118 }
119
120 // Draw the triangle to dst.
121 func (t Triangle) Draw(dst draw.Image) (drawCount int) {
122 b := t.Bounds()
123
124 for x := b.Min.X; x < b.Max.X; x++ {
125 for y := b.Min.Y; y < b.Max.Y; y++ {
126 if u := IV(x, y); t.Contains(u) {
127 drawCount++
128 SetVec(dst, u, t.Color(u))
129 }
130 }
131 }
132
133 return drawCount
134 }
135
136 // DrawOver draws the first color in the triangle over dst.
137 func (t Triangle) DrawOver(dst draw.Image) (drawCount int) {
138 a, _, _ := t.Colors()
139
140 return t.DrawColorOver(dst, a)
141 }
142
143 // DrawColor draws the triangle on dst using the given color.
144 func (t Triangle) DrawColor(dst draw.Image, c color.Color) (drawCount int) {
145 return t.drawColor(dst, c, draw.Src)
146 }
147
148 // DrawColorOver draws the triangle over dst using the given color.
149 func (t Triangle) DrawColorOver(dst draw.Image, c color.Color) (drawCount int) {
150 return t.drawColor(dst, c, draw.Over)
151 }
152
153 func (t Triangle) drawColor(dst draw.Image, c color.Color, op draw.Op) (drawCount int) {
154 b := t.Bounds()
155
156 var lefts, rights []Vec
157 var invalid = V(-math.MaxInt64, 0)
158
159 for y := b.Min.Y; y < b.Max.Y; y++ {
160 var left, right = invalid, invalid
161
162 for x := b.Min.X; x < b.Max.X; x++ {
163 if u := IV(x, y); t.Contains(u) {
164 left = u
165 break
166 }
167 }
168
169 for x := b.Max.X; x > b.Min.X; x-- {
170 if u := IV(x, y); t.Contains(u) {
171 right = u
172 break
173 }
174 }
175
176 if left != invalid && right != invalid {
177 lefts = append(lefts, left)
178 rights = append(rights, right)
179 }
180 }
181
182 uc := NewUniform(c)
183
184 for i := 0; i < len(lefts); i++ {
185 r := NewRect(lefts[i], rights[i].AddXY(0, 1)).Bounds()
186
187 draw.Draw(dst, r, uc, ZP, op)
188
189 drawCount++
190 }
191
192 return drawCount
193 }
194
195 // DrawWireframe draws the triangle as a wireframe on dst.
196 func (t Triangle) DrawWireframe(dst draw.Image, c color.Color) (drawCount int) {
197 if l := t[0].Position.To(t[1].Position).Len(); l > 25 {
198 DrawLine(dst, t[0].Position, t[1].Position, l/50, ColorWithAlpha(c, 128))
199 drawCount++
200 }
201
202 if l := t[1].Position.To(t[2].Position).Len(); l > 25 {
203 DrawLine(dst, t[1].Position, t[2].Position, l/50, ColorWithAlpha(c, 128))
204 drawCount++
205 }
206
207 if l := t[0].Position.To(t[2].Position).Len(); l > 25 {
208 DrawLine(dst, t[0].Position, t[2].Position, l/50, ColorWithAlpha(c, 128))
209 drawCount++
210 }
211
212 DrawLine(dst, t[0].Position, t[1].Position, 1, c)
213 DrawLine(dst, t[1].Position, t[2].Position, 1, c)
214 DrawLine(dst, t[0].Position, t[2].Position, 1, c)
215
216 drawCount += 3
217
218 return
219 }