-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrect.go
241 lines (211 loc) · 6.22 KB
/
rect.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package gfx
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
)
// ZR is the zero image.Rectangle.
var ZR = image.ZR
// Rect is a 2D rectangle aligned with the axes of the coordinate system. It is defined by two
// points, Min and Max.
//
// The invariant should hold, that Max's components are greater or equal than Min's components
// respectively.
type Rect struct {
Min, Max Vec
}
// NewRect creates a new Rect.
func NewRect(min, max Vec) Rect {
return Rect{
Min: min,
Max: max,
}
}
// R returns a new Rect given the Min and Max coordinates.
//
// Note that the returned rectangle is not automatically normalized.
func R(minX, minY, maxX, maxY float64) Rect {
return NewRect(Vec{minX, minY}, Vec{maxX, maxY})
}
// BoundsToRect converts an image.Rectangle to a Rect.
func BoundsToRect(ir image.Rectangle) Rect {
return R(float64(ir.Min.X), float64(ir.Min.Y), float64(ir.Max.X), float64(ir.Max.Y))
}
// BoundsCenter returns the vector in the center of an image.Rectangle
func BoundsCenter(ir image.Rectangle) Vec {
return BoundsToRect(ir).Center()
}
// BoundsCenterOrigin returns the center origin for the given image.Rectangle and z value.
func BoundsCenterOrigin(ir image.Rectangle, v Vec, z float64) Vec3 {
return BoundsToRect(ir).CenterOrigin(v, z)
}
// CenterOrigin returns a Vec3 based on Rect.Center()
// scaled by v, and its Z component set to the provided z.
func (r Rect) CenterOrigin(v Vec, z float64) Vec3 {
return r.Center().ScaledXY(v).Vec3(z)
}
// String returns the string representation of the Rect.
//
// r := gfx.R(100, 50, 200, 300)
// r.String() // returns "gfx.R(100, 50, 200, 300)"
// fmt.Println(r) // gfx.R(100, 50, 200, 300)
func (r Rect) String() string {
return fmt.Sprintf("gfx.R(%v, %v, %v, %v)", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
}
// Norm returns the Rect in normal form, such that Max is component-wise greater or equal than Min.
func (r Rect) Norm() Rect {
return Rect{
Min: Vec{
math.Min(r.Min.X, r.Max.X),
math.Min(r.Min.Y, r.Max.Y),
},
Max: Vec{
math.Max(r.Min.X, r.Max.X),
math.Max(r.Min.Y, r.Max.Y),
},
}
}
// W returns the width of the Rect.
func (r Rect) W() float64 {
return r.Max.X - r.Min.X
}
// H returns the height of the Rect.
func (r Rect) H() float64 {
return r.Max.Y - r.Min.Y
}
// Size returns the vector of width and height of the Rect.
func (r Rect) Size() Vec {
return V(r.W(), r.H())
}
// Area returns the area of r. If r is not normalized, area may be negative.
func (r Rect) Area() float64 {
return r.W() * r.H()
}
// Center returns the position of the center of the Rect.
func (r Rect) Center() Vec {
return r.Min.Lerp(r.Max, 0.5)
}
// Moved returns the Rect moved (both Min and Max) by the given vector delta.
func (r Rect) Moved(delta Vec) Rect {
return Rect{
Min: r.Min.Add(delta),
Max: r.Max.Add(delta),
}
}
// Resized returns the Rect resized to the given size while keeping the position of the given
// anchor.
//
// r.Resized(r.Min, size) // resizes while keeping the position of the lower-left corner
// r.Resized(r.Max, size) // same with the top-right corner
// r.Resized(r.Center(), size) // resizes around the center
//
// This function does not make sense for resizing a rectangle of zero area and will panic. Use
// ResizedMin in the case of zero area.
func (r Rect) Resized(anchor, size Vec) Rect {
if r.W()*r.H() == 0 {
panic(fmt.Errorf("(%T).Resize: zero area", r))
}
fraction := Vec{size.X / r.W(), size.Y / r.H()}
return Rect{
Min: anchor.Add(r.Min.Sub(anchor).ScaledXY(fraction)),
Max: anchor.Add(r.Max.Sub(anchor).ScaledXY(fraction)),
}
}
// ResizedMin returns the Rect resized to the given size while keeping the position of the Rect's
// Min.
//
// Sizes of zero area are safe here.
func (r Rect) ResizedMin(size Vec) Rect {
return Rect{
Min: r.Min,
Max: r.Min.Add(size),
}
}
// Contains checks whether a vector u is contained within this Rect (including it's borders).
func (r Rect) Contains(u Vec) bool {
return r.Min.X <= u.X && u.X <= r.Max.X && r.Min.Y <= u.Y && u.Y <= r.Max.Y
}
// Overlaps checks whether one Rect overlaps another Rect.
func (r Rect) Overlaps(s Rect) bool {
return r.Intersect(s) != Rect{}
}
// Union returns the minimal Rect which covers both r and s. Rects r and s must be normalized.
func (r Rect) Union(s Rect) Rect {
return R(
math.Min(r.Min.X, s.Min.X),
math.Min(r.Min.Y, s.Min.Y),
math.Max(r.Max.X, s.Max.X),
math.Max(r.Max.Y, s.Max.Y),
)
}
// Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized.
//
// If r and s don't overlap, this function returns R(0, 0, 0, 0).
func (r Rect) Intersect(s Rect) Rect {
t := R(
math.Max(r.Min.X, s.Min.X),
math.Max(r.Min.Y, s.Min.Y),
math.Min(r.Max.X, s.Max.X),
math.Min(r.Max.Y, s.Max.Y),
)
if t.Min.X >= t.Max.X || t.Min.Y >= t.Max.Y {
return Rect{}
}
return t
}
// Bounds returns the bounds of the rectangle.
func (r Rect) Bounds() image.Rectangle {
return image.Rectangle{
Min: r.Min.Pt(),
Max: r.Max.Pt(),
}
}
// Draw draws Rect to src over dst, at the zero point.
func (r Rect) Draw(dst draw.Image, src image.Image) {
draw.Draw(dst, r.Bounds(), src, ZP, draw.Src)
}
// DrawColor draws Rect with a uniform color on dst.
func (r Rect) DrawColor(dst draw.Image, c color.Color) {
draw.Draw(dst, r.Bounds(), NewUniform(c), ZP, draw.Src)
}
// DrawColorOver draws Rect with a uniform color over dst.
func (r Rect) DrawColorOver(dst draw.Image, c color.Color) {
draw.Draw(dst, r.Bounds(), NewUniform(c), ZP, draw.Over)
}
// EachVec calls the provided function for each vec in the given direction.
func (r Rect) EachVec(dir Vec, fn func(p Vec)) {
if dir.X == 0 {
dir.X = 1
}
if dir.Y == 0 {
dir.Y = 1
}
switch {
case dir.X > 0 && dir.Y < 0:
for y := r.Max.Y - 1; y >= r.Min.Y; y += dir.Y {
for x := r.Min.X; x < r.Max.X; x += dir.X {
fn(V(x, y))
}
}
case dir.X < 0 && dir.Y < 0:
for y := r.Max.Y - 1; y >= r.Min.Y; y += dir.Y {
for x := r.Max.X - 1; x >= r.Min.X; x += dir.X {
fn(V(x, y))
}
}
case dir.X < 0 && dir.Y > 0:
for y := r.Min.Y; y < r.Max.Y; y += dir.Y {
for x := r.Max.X - 1; x >= r.Min.X; x += dir.X {
fn(V(x, y))
}
}
default:
for y := r.Min.Y; y < r.Max.Y; y += dir.Y {
for x := r.Min.X; x < r.Max.X; x += dir.X {
fn(V(x, y))
}
}
}
}