Debian Codegraph @

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

raw
1 package gfx
2
3 import (
4 "image"
5 "image/color"
6 "image/draw"
7 )
8
9 // Draw draws src on dst, at the zero point using draw.Src.
10 func Draw(dst draw.Image, r image.Rectangle, src image.Image) {
11 draw.Draw(dst, r, src, ZP, draw.Src)
12 }
13
14 // DrawColor draws an image.Rectangle of uniform color on dst.
15 func DrawColor(dst draw.Image, r image.Rectangle, c color.Color) {
16 draw.Draw(dst, r, NewUniform(c), ZP, draw.Src)
17 }
18
19 // DrawColorOver draws an image.Rectangle of uniform color over dst.
20 func DrawColorOver(dst draw.Image, r image.Rectangle, c color.Color) {
21 draw.Draw(dst, r, NewUniform(c), ZP, draw.Over)
22 }
23
24 // DrawSrc draws src on dst.
25 func DrawSrc(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point) {
26 draw.Draw(dst, r, src, sp, draw.Src)
27 }
28
29 // DrawOver draws src over dst.
30 func DrawOver(dst draw.Image, r image.Rectangle, src image.Image, sp image.Point) {
31 draw.Draw(dst, r, src, sp, draw.Over)
32 }
33
34 // DrawPalettedImage draws a PalettedImage over a PalettedDrawImage.
35 func DrawPalettedImage(dst PalettedDrawImage, r image.Rectangle, src PalettedImage) {
36 w, h, m := r.Dx(), r.Dy(), r.Min
37
38 for x := m.X; x != w; x++ {
39 for y := m.Y; y != h; y++ {
40 if src.AlphaAt(x, y) > 0 {
41 dst.SetColorIndex(x, y, src.ColorIndexAt(x, y))
42 }
43 }
44 }
45 }
46
47 // DrawPalettedLayer draws a *Layer over a *Paletted.
48 // (slightly faster than using the generic DrawPalettedImage)
49 func DrawPalettedLayer(dst *Paletted, r image.Rectangle, src *Layer) {
50 w, h, m := r.Dx(), r.Dy(), r.Min
51
52 for x := m.X; x != w; x++ {
53 for y := m.Y; y != h; y++ {
54 if src.AlphaAt(x, y) > 0 {
55 dst.SetColorIndex(x, y, src.ColorIndexAt(x, y))
56 }
57 }
58 }
59 }
60
61 // DrawLine draws a line of the given color.
62 // A thickness of <= 1 is drawn using DrawBresenhamLine.
63 func DrawLine(dst draw.Image, from, to Vec, thickness float64, c color.Color) {
64 if thickness <= 1 {
65 DrawLineBresenham(dst, from, to, c)
66 return
67 }
68
69 polylineFromTo(from, to, thickness).Fill(dst, c)
70 }
71
72 // DrawTriangles draws triangles on dst.
73 func DrawTriangles(dst draw.Image, triangles []Triangle) {
74 for _, t := range triangles {
75 t.Draw(dst)
76 }
77 }
78
79 // DrawTrianglesOver draws triangles over dst.
80 func DrawTrianglesOver(dst draw.Image, triangles []Triangle) {
81 for _, t := range triangles {
82 t.DrawOver(dst)
83 }
84 }
85
86 // DrawTrianglesWireframe draws triangles on dst.
87 func DrawTrianglesWireframe(dst draw.Image, triangles []Triangle) {
88 for _, t := range triangles {
89 t.DrawWireframe(dst, t.Color(V(0, 0)))
90 }
91 }
92
93 // DrawCircle draws a circle with radius and thickness. (filled if thickness == 0)
94 func DrawCircle(dst draw.Image, u Vec, radius, thickness float64, c color.Color) {
95 if thickness == 0 {
96 DrawCircleFilled(dst, u, radius, c)
97 return
98 }
99
100 bounds := IR(int(u.X-radius), int(u.Y-radius), int(u.X+radius), int(u.Y+radius))
101
102 EachPixel(dst.Bounds().Intersect(bounds), func(x, y int) {
103 v := V(float64(x), float64(y))
104
105 l := u.To(v).Len() + 0.5
106
107 if l < radius && l > radius-thickness {
108 Mix(dst, x, y, c)
109 }
110 })
111 }
112
113 // DrawCircleFilled draws a filled circle.
114 func DrawCircleFilled(dst draw.Image, u Vec, radius float64, c color.Color) {
115 bounds := IR(int(u.X-radius+1), int(u.Y-radius+1), int(u.X+radius+1), int(u.Y+radius+1))
116
117 EachPixel(dst.Bounds().Intersect(bounds), func(x, y int) {
118 v := V(float64(x), float64(y))
119
120 if u.To(v).Len() < radius {
121 Mix(dst, x, y, c)
122 }
123 })
124 }
125
126 // DrawCicleFast draws a (crude) filled circle.
127 func DrawCicleFast(dst draw.Image, u Vec, radius float64, c color.Color) {
128 ir := int(radius)
129 r2 := ir * ir
130 pt := u.Pt()
131
132 for y := -ir; y <= ir; y++ {
133 for x := -ir; x <= ir; x++ {
134 if x*x+y*y <= r2 {
135 SetPoint(dst, pt.Add(Pt(x, y)), c)
136 }
137 }
138 }
139 }
140
141 // DrawPointCircle draws a circle at the given point.
142 func DrawPointCircle(dst draw.Image, p image.Point, radius, thickness float64, c color.Color) {
143 points := circlePoints(p, int(radius))
144
145 switch {
146 case thickness <= 1:
147 for i := range points {
148 SetPoint(dst, points[i], c)
149 }
150 default:
151 center := PV(p)
152
153 for i := range points {
154 from := PV(points[i])
155 to := from.Add(from.To(center).Unit().Scaled(thickness))
156
157 DrawLine(dst, from, to, thickness, c)
158 }
159 }
160 }
161
162 func circlePoints(p image.Point, radius int) Points {
163 var cp []image.Point
164
165 x, y, dx, dy := radius-1, 0, 1, 1
166
167 e := dx - (radius << 1)
168
169 for x >= y {
170 cp = append(cp,
171 p.Add(Pt(x, y)),
172 p.Add(Pt(y, x)),
173 p.Add(Pt(-y, x)),
174 p.Add(Pt(-x, y)),
175 p.Add(Pt(-x, -y)),
176 p.Add(Pt(-y, -x)),
177 p.Add(Pt(y, -x)),
178 p.Add(Pt(x, -y)),
179 )
180
181 if e <= 0 {
182 y++
183 e += dy
184 dy += 2
185 }
186
187 if e > 0 {
188 x--
189 dx += 2
190 e += dx - (radius << 1)
191 }
192 }
193
194 return cp
195 }