Debian Codegraph @

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

raw
1 //go:build !tinygo
2 // +build !tinygo
3
4 package gfx
5
6 import (
7 "image"
8 "image/color"
9 "image/draw"
10 )
11
12 // DrawTarget draws to a draw.Image, projected through a Matrix.
13 type DrawTarget struct {
14 dst draw.Image
15 mat Matrix
16 }
17
18 var _ BasicTarget = (*DrawTarget)(nil)
19
20 // NewDrawTarget creates a new draw target.
21 func NewDrawTarget(dst draw.Image) *DrawTarget {
22 return &DrawTarget{
23 dst: dst,
24 mat: IM,
25 }
26 }
27
28 // SetMatrix sets the matrix of the draw target.
29 func (dt *DrawTarget) SetMatrix(mat Matrix) {
30 dt.mat = mat
31 }
32
33 // Bounds of the draw target.
34 func (dt *DrawTarget) Bounds() image.Rectangle {
35 return dt.dst.Bounds()
36 }
37
38 // Center vector of the draw target.
39 func (dt *DrawTarget) Center() Vec {
40 return BoundsCenter(dt.dst.Bounds())
41 }
42
43 // ColorModel of the draw target.
44 func (dt *DrawTarget) ColorModel() color.Model {
45 return dt.dst.ColorModel()
46 }
47
48 // At retrieves the color at (x, y).
49 func (dt *DrawTarget) At(x, y int) color.Color {
50 p := dt.mat.Project(IV(x, y)).Pt()
51
52 return dt.dst.At(p.X, p.Y)
53 }
54
55 // Set the color at (x, y). (Projected through the draw target Matrix)
56 func (dt *DrawTarget) Set(x, y int, c color.Color) {
57 p := dt.mat.Project(IV(x, y)).Pt()
58
59 dt.dst.Set(p.X, p.Y, c)
60 }
61
62 // MakePicture creates a TargetPicture for the provided Picture.
63 func (dt *DrawTarget) MakePicture(pic Picture) TargetPicture {
64 panic(Error("*DrawTarget: not implemented yet."))
65 }
66
67 // MakeTriangles creates TargetTriangles for the given Triangles
68 func (dt *DrawTarget) MakeTriangles(t Triangles) TargetTriangles {
69 return &targetTriangles{Triangles: t, dt: dt}
70 }
71
72 type targetTriangles struct {
73 Triangles
74 dt *DrawTarget
75 }
76
77 func (tt *targetTriangles) Draw() {
78 td := MakeTrianglesData(tt.Len())
79
80 td.Update(tt.Triangles)
81
82 for i := 0; i < td.Len(); i += 3 {
83 t := NewTriangle(i, td)
84 b := t.Bounds()
85
86 for x := b.Min.X; x < b.Max.X; x++ {
87 for y := b.Min.Y; y < b.Max.Y; y++ {
88
89 if u := IV(x, y); t.Contains(u) {
90 tt.dt.Set(x, y, t.Color(u))
91 }
92 }
93 }
94 }
95 }