Debian Codegraph @

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

raw
1 package gfx
2
3 import (
4 "fmt"
5 "image"
6 "image/color"
7 "image/draw"
8 "math"
9 )
10
11 // ZR is the zero image.Rectangle.
12 var ZR = image.Rectangle{}
13
14 // Rect is a 2D rectangle aligned with the axes of the coordinate system. It is defined by two
15 // points, Min and Max.
16 //
17 // The invariant should hold, that Max's components are greater or equal than Min's components
18 // respectively.
19 type Rect struct {
20 Min, Max Vec
21 }
22
23 // NewRect creates a new Rect.
24 func NewRect(min, max Vec) Rect {
25 return Rect{
26 Min: min,
27 Max: max,
28 }
29 }
30
31 // R returns a new Rect given the Min and Max coordinates.
32 //
33 // Note that the returned rectangle is not automatically normalized.
34 func R(minX, minY, maxX, maxY float64) Rect {
35 return NewRect(Vec{minX, minY}, Vec{maxX, maxY})
36 }
37
38 // BoundsToRect converts an image.Rectangle to a Rect.
39 func BoundsToRect(ir image.Rectangle) Rect {
40 return R(float64(ir.Min.X), float64(ir.Min.Y), float64(ir.Max.X), float64(ir.Max.Y))
41 }
42
43 // BoundsCenter returns the vector in the center of an image.Rectangle
44 func BoundsCenter(ir image.Rectangle) Vec {
45 return BoundsToRect(ir).Center()
46 }
47
48 // BoundsCenterOrigin returns the center origin for the given image.Rectangle and z value.
49 func BoundsCenterOrigin(ir image.Rectangle, v Vec, z float64) Vec3 {
50 return BoundsToRect(ir).CenterOrigin(v, z)
51 }
52
53 // CenterOrigin returns a Vec3 based on Rect.Center()
54 // scaled by v, and its Z component set to the provided z.
55 func (r Rect) CenterOrigin(v Vec, z float64) Vec3 {
56 return r.Center().ScaledXY(v).Vec3(z)
57 }
58
59 // String returns the string representation of the Rect.
60 //
61 // r := gfx.R(100, 50, 200, 300)
62 // r.String() // returns "gfx.R(100, 50, 200, 300)"
63 // fmt.Println(r) // gfx.R(100, 50, 200, 300)
64 func (r Rect) String() string {
65 return fmt.Sprintf("gfx.R(%v, %v, %v, %v)", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
66 }
67
68 // Norm returns the Rect in normal form, such that Max is component-wise greater or equal than Min.
69 func (r Rect) Norm() Rect {
70 return Rect{
71 Min: Vec{
72 math.Min(r.Min.X, r.Max.X),
73 math.Min(r.Min.Y, r.Max.Y),
74 },
75 Max: Vec{
76 math.Max(r.Min.X, r.Max.X),
77 math.Max(r.Min.Y, r.Max.Y),
78 },
79 }
80 }
81
82 // W returns the width of the Rect.
83 func (r Rect) W() float64 {
84 return r.Max.X - r.Min.X
85 }
86
87 // H returns the height of the Rect.
88 func (r Rect) H() float64 {
89 return r.Max.Y - r.Min.Y
90 }
91
92 // Size returns the vector of width and height of the Rect.
93 func (r Rect) Size() Vec {
94 return V(r.W(), r.H())
95 }
96
97 // Area returns the area of r. If r is not normalized, area may be negative.
98 func (r Rect) Area() float64 {
99 return r.W() * r.H()
100 }
101
102 // Center returns the position of the center of the Rect.
103 func (r Rect) Center() Vec {
104 return r.Min.Lerp(r.Max, 0.5)
105 }
106
107 // Moved returns the Rect moved (both Min and Max) by the given vector delta.
108 func (r Rect) Moved(delta Vec) Rect {
109 return Rect{
110 Min: r.Min.Add(delta),
111 Max: r.Max.Add(delta),
112 }
113 }
114
115 // Resized returns the Rect resized to the given size while keeping the position of the given
116 // anchor.
117 //
118 // r.Resized(r.Min, size) // resizes while keeping the position of the lower-left corner
119 // r.Resized(r.Max, size) // same with the top-right corner
120 // r.Resized(r.Center(), size) // resizes around the center
121 //
122 // This function does not make sense for resizing a rectangle of zero area and will panic. Use
123 // ResizedMin in the case of zero area.
124 func (r Rect) Resized(anchor, size Vec) Rect {
125 if r.W()*r.H() == 0 {
126 panic(fmt.Errorf("(%T).Resize: zero area", r))
127 }
128 fraction := Vec{size.X / r.W(), size.Y / r.H()}
129 return Rect{
130 Min: anchor.Add(r.Min.Sub(anchor).ScaledXY(fraction)),
131 Max: anchor.Add(r.Max.Sub(anchor).ScaledXY(fraction)),
132 }
133 }
134
135 // ResizedMin returns the Rect resized to the given size while keeping the position of the Rect's
136 // Min.
137 //
138 // Sizes of zero area are safe here.
139 func (r Rect) ResizedMin(size Vec) Rect {
140 return Rect{
141 Min: r.Min,
142 Max: r.Min.Add(size),
143 }
144 }
145
146 // Contains checks whether a vector u is contained within this Rect (including it's borders).
147 func (r Rect) Contains(u Vec) bool {
148 return r.Min.X <= u.X && u.X <= r.Max.X && r.Min.Y <= u.Y && u.Y <= r.Max.Y
149 }
150
151 // Overlaps checks whether one Rect overlaps another Rect.
152 func (r Rect) Overlaps(s Rect) bool {
153 return r.Intersect(s) != Rect{}
154 }
155
156 // Union returns the minimal Rect which covers both r and s. Rects r and s must be normalized.
157 func (r Rect) Union(s Rect) Rect {
158 return R(
159 math.Min(r.Min.X, s.Min.X),
160 math.Min(r.Min.Y, s.Min.Y),
161 math.Max(r.Max.X, s.Max.X),
162 math.Max(r.Max.Y, s.Max.Y),
163 )
164 }
165
166 // Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized.
167 //
168 // If r and s don't overlap, this function returns R(0, 0, 0, 0).
169 func (r Rect) Intersect(s Rect) Rect {
170 t := R(
171 math.Max(r.Min.X, s.Min.X),
172 math.Max(r.Min.Y, s.Min.Y),
173 math.Min(r.Max.X, s.Max.X),
174 math.Min(r.Max.Y, s.Max.Y),
175 )
176 if t.Min.X >= t.Max.X || t.Min.Y >= t.Max.Y {
177 return Rect{}
178 }
179 return t
180 }
181
182 // Bounds returns the bounds of the rectangle.
183 func (r Rect) Bounds() image.Rectangle {
184 return image.Rectangle{
185 Min: r.Min.Pt(),
186 Max: r.Max.Pt(),
187 }
188 }
189
190 // Draw draws Rect to src over dst, at the zero point.
191 func (r Rect) Draw(dst draw.Image, src image.Image) {
192 draw.Draw(dst, r.Bounds(), src, ZP, draw.Src)
193 }
194
195 // DrawColor draws Rect with a uniform color on dst.
196 func (r Rect) DrawColor(dst draw.Image, c color.Color) {
197 draw.Draw(dst, r.Bounds(), NewUniform(c), ZP, draw.Src)
198 }
199
200 // DrawColorOver draws Rect with a uniform color over dst.
201 func (r Rect) DrawColorOver(dst draw.Image, c color.Color) {
202 draw.Draw(dst, r.Bounds(), NewUniform(c), ZP, draw.Over)
203 }
204
205 // EachVec calls the provided function for each vec in the given direction.
206 func (r Rect) EachVec(dir Vec, fn func(p Vec)) {
207 if dir.X == 0 {
208 dir.X = 1
209 }
210
211 if dir.Y == 0 {
212 dir.Y = 1
213 }
214
215 switch {
216 case dir.X > 0 && dir.Y < 0:
217 for y := r.Max.Y - 1; y >= r.Min.Y; y += dir.Y {
218 for x := r.Min.X; x < r.Max.X; x += dir.X {
219 fn(V(x, y))
220 }
221 }
222 case dir.X < 0 && dir.Y < 0:
223 for y := r.Max.Y - 1; y >= r.Min.Y; y += dir.Y {
224 for x := r.Max.X - 1; x >= r.Min.X; x += dir.X {
225 fn(V(x, y))
226 }
227 }
228 case dir.X < 0 && dir.Y > 0:
229 for y := r.Min.Y; y < r.Max.Y; y += dir.Y {
230 for x := r.Max.X - 1; x >= r.Min.X; x += dir.X {
231 fn(V(x, y))
232 }
233 }
234 default:
235 for y := r.Min.Y; y < r.Max.Y; y += dir.Y {
236 for x := r.Min.X; x < r.Max.X; x += dir.X {
237 fn(V(x, y))
238 }
239 }
240 }
241 }