| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
) |
| 7 |
|
| 8 |
var ( |
| 9 |
|
| 10 |
BoxZero = Box{IsSet: true} |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
func NewBox(top, left, right, bottom int) Box { |
| 15 |
return Box{ |
| 16 |
IsSet: true, |
| 17 |
Top: top, |
| 18 |
Left: left, |
| 19 |
Right: right, |
| 20 |
Bottom: bottom, |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
type Box struct { |
| 26 |
Top int |
| 27 |
Left int |
| 28 |
Right int |
| 29 |
Bottom int |
| 30 |
IsSet bool |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func (b Box) IsZero() bool { |
| 35 |
if b.IsSet { |
| 36 |
return false |
| 37 |
} |
| 38 |
return b.Top == 0 && b.Left == 0 && b.Right == 0 && b.Bottom == 0 |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (b Box) String() string { |
| 43 |
return fmt.Sprintf("box(%d,%d,%d,%d)", b.Top, b.Left, b.Right, b.Bottom) |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func (b Box) GetTop(defaults ...int) int { |
| 48 |
if !b.IsSet && b.Top == 0 { |
| 49 |
if len(defaults) > 0 { |
| 50 |
return defaults[0] |
| 51 |
} |
| 52 |
return 0 |
| 53 |
} |
| 54 |
return b.Top |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func (b Box) GetLeft(defaults ...int) int { |
| 59 |
if !b.IsSet && b.Left == 0 { |
| 60 |
if len(defaults) > 0 { |
| 61 |
return defaults[0] |
| 62 |
} |
| 63 |
return 0 |
| 64 |
} |
| 65 |
return b.Left |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (b Box) GetRight(defaults ...int) int { |
| 70 |
if !b.IsSet && b.Right == 0 { |
| 71 |
if len(defaults) > 0 { |
| 72 |
return defaults[0] |
| 73 |
} |
| 74 |
return 0 |
| 75 |
} |
| 76 |
return b.Right |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func (b Box) GetBottom(defaults ...int) int { |
| 81 |
if !b.IsSet && b.Bottom == 0 { |
| 82 |
if len(defaults) > 0 { |
| 83 |
return defaults[0] |
| 84 |
} |
| 85 |
return 0 |
| 86 |
} |
| 87 |
return b.Bottom |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
func (b Box) Width() int { |
| 92 |
return AbsInt(b.Right - b.Left) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (b Box) Height() int { |
| 97 |
return AbsInt(b.Bottom - b.Top) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func (b Box) Center() (x, y int) { |
| 102 |
w2, h2 := b.Width()>>1, b.Height()>>1 |
| 103 |
return b.Left + w2, b.Top + h2 |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
func (b Box) Aspect() float64 { |
| 108 |
return float64(b.Width()) / float64(b.Height()) |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
func (b Box) Clone() Box { |
| 113 |
return Box{ |
| 114 |
IsSet: b.IsSet, |
| 115 |
Top: b.Top, |
| 116 |
Left: b.Left, |
| 117 |
Right: b.Right, |
| 118 |
Bottom: b.Bottom, |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
func (b Box) IsBiggerThan(other Box) bool { |
| 124 |
return b.Top < other.Top || |
| 125 |
b.Bottom > other.Bottom || |
| 126 |
b.Left < other.Left || |
| 127 |
b.Right > other.Right |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
func (b Box) IsSmallerThan(other Box) bool { |
| 132 |
return b.Top > other.Top && |
| 133 |
b.Bottom < other.Bottom && |
| 134 |
b.Left > other.Left && |
| 135 |
b.Right < other.Right |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (b Box) Equals(other Box) bool { |
| 140 |
return b.Top == other.Top && |
| 141 |
b.Left == other.Left && |
| 142 |
b.Right == other.Right && |
| 143 |
b.Bottom == other.Bottom |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
func (b Box) Grow(other Box) Box { |
| 148 |
return Box{ |
| 149 |
Top: MinInt(b.Top, other.Top), |
| 150 |
Left: MinInt(b.Left, other.Left), |
| 151 |
Right: MaxInt(b.Right, other.Right), |
| 152 |
Bottom: MaxInt(b.Bottom, other.Bottom), |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func (b Box) Shift(x, y int) Box { |
| 158 |
return Box{ |
| 159 |
Top: b.Top + y, |
| 160 |
Left: b.Left + x, |
| 161 |
Right: b.Right + x, |
| 162 |
Bottom: b.Bottom + y, |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
func (b Box) Corners() BoxCorners { |
| 168 |
return BoxCorners{ |
| 169 |
TopLeft: Point{b.Left, b.Top}, |
| 170 |
TopRight: Point{b.Right, b.Top}, |
| 171 |
BottomRight: Point{b.Right, b.Bottom}, |
| 172 |
BottomLeft: Point{b.Left, b.Bottom}, |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
func (b Box) Fit(other Box) Box { |
| 180 |
ba := b.Aspect() |
| 181 |
oa := other.Aspect() |
| 182 |
|
| 183 |
if oa == ba { |
| 184 |
return b.Clone() |
| 185 |
} |
| 186 |
|
| 187 |
bw, bh := float64(b.Width()), float64(b.Height()) |
| 188 |
bw2 := int(bw) >> 1 |
| 189 |
bh2 := int(bh) >> 1 |
| 190 |
if oa > ba { |
| 191 |
var noh2 int |
| 192 |
if oa > 1.0 { |
| 193 |
noh2 = int(bw/oa) >> 1 |
| 194 |
} else { |
| 195 |
noh2 = int(bh*oa) >> 1 |
| 196 |
} |
| 197 |
return Box{ |
| 198 |
Top: (b.Top + bh2) - noh2, |
| 199 |
Left: b.Left, |
| 200 |
Right: b.Right, |
| 201 |
Bottom: (b.Top + bh2) + noh2, |
| 202 |
} |
| 203 |
} |
| 204 |
var now2 int |
| 205 |
if oa > 1.0 { |
| 206 |
now2 = int(bh/oa) >> 1 |
| 207 |
} else { |
| 208 |
now2 = int(bw*oa) >> 1 |
| 209 |
} |
| 210 |
return Box{ |
| 211 |
Top: b.Top, |
| 212 |
Left: (b.Left + bw2) - now2, |
| 213 |
Right: (b.Left + bw2) + now2, |
| 214 |
Bottom: b.Bottom, |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
func (b Box) Constrain(other Box) Box { |
| 221 |
newBox := b.Clone() |
| 222 |
|
| 223 |
newBox.Top = MaxInt(newBox.Top, other.Top) |
| 224 |
newBox.Left = MaxInt(newBox.Left, other.Left) |
| 225 |
newBox.Right = MinInt(newBox.Right, other.Right) |
| 226 |
newBox.Bottom = MinInt(newBox.Bottom, other.Bottom) |
| 227 |
|
| 228 |
return newBox |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
func (b Box) OuterConstrain(bounds, other Box) Box { |
| 234 |
newBox := b.Clone() |
| 235 |
if other.Top < bounds.Top { |
| 236 |
delta := bounds.Top - other.Top |
| 237 |
newBox.Top = b.Top + delta |
| 238 |
} |
| 239 |
|
| 240 |
if other.Left < bounds.Left { |
| 241 |
delta := bounds.Left - other.Left |
| 242 |
newBox.Left = b.Left + delta |
| 243 |
} |
| 244 |
|
| 245 |
if other.Right > bounds.Right { |
| 246 |
delta := other.Right - bounds.Right |
| 247 |
newBox.Right = b.Right - delta |
| 248 |
} |
| 249 |
|
| 250 |
if other.Bottom > bounds.Bottom { |
| 251 |
delta := other.Bottom - bounds.Bottom |
| 252 |
newBox.Bottom = b.Bottom - delta |
| 253 |
} |
| 254 |
return newBox |
| 255 |
} |
| 256 |
|
| 257 |
func (b Box) Validate() error { |
| 258 |
if b.Left < 0 { |
| 259 |
return fmt.Errorf("invalid left; must be >= 0") |
| 260 |
} |
| 261 |
if b.Right < 0 { |
| 262 |
return fmt.Errorf("invalid right; must be > 0") |
| 263 |
} |
| 264 |
if b.Top < 0 { |
| 265 |
return fmt.Errorf("invalid top; must be > 0") |
| 266 |
} |
| 267 |
if b.Bottom < 0 { |
| 268 |
return fmt.Errorf("invalid bottom; must be > 0") |
| 269 |
} |
| 270 |
return nil |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
type BoxCorners struct { |
| 275 |
TopLeft, TopRight, BottomRight, BottomLeft Point |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
func (bc BoxCorners) Box() Box { |
| 280 |
return Box{ |
| 281 |
Top: MinInt(bc.TopLeft.Y, bc.TopRight.Y), |
| 282 |
Left: MinInt(bc.TopLeft.X, bc.BottomLeft.X), |
| 283 |
Right: MaxInt(bc.TopRight.X, bc.BottomRight.X), |
| 284 |
Bottom: MaxInt(bc.BottomLeft.Y, bc.BottomRight.Y), |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
func (bc BoxCorners) Width() int { |
| 290 |
minLeft := MinInt(bc.TopLeft.X, bc.BottomLeft.X) |
| 291 |
maxRight := MaxInt(bc.TopRight.X, bc.BottomRight.X) |
| 292 |
return maxRight - minLeft |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
func (bc BoxCorners) Height() int { |
| 297 |
minTop := MinInt(bc.TopLeft.Y, bc.TopRight.Y) |
| 298 |
maxBottom := MaxInt(bc.BottomLeft.Y, bc.BottomRight.Y) |
| 299 |
return maxBottom - minTop |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
func (bc BoxCorners) Center() (x, y int) { |
| 304 |
|
| 305 |
left := MeanInt(bc.TopLeft.X, bc.BottomLeft.X) |
| 306 |
right := MeanInt(bc.TopRight.X, bc.BottomRight.X) |
| 307 |
x = ((right - left) >> 1) + left |
| 308 |
|
| 309 |
top := MeanInt(bc.TopLeft.Y, bc.TopRight.Y) |
| 310 |
bottom := MeanInt(bc.BottomLeft.Y, bc.BottomRight.Y) |
| 311 |
y = ((bottom - top) >> 1) + top |
| 312 |
|
| 313 |
return |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
func (bc BoxCorners) Rotate(thetaDegrees float64) BoxCorners { |
| 318 |
cx, cy := bc.Center() |
| 319 |
|
| 320 |
thetaRadians := DegreesToRadians(thetaDegrees) |
| 321 |
|
| 322 |
tlx, tly := RotateCoordinate(cx, cy, bc.TopLeft.X, bc.TopLeft.Y, thetaRadians) |
| 323 |
trx, try := RotateCoordinate(cx, cy, bc.TopRight.X, bc.TopRight.Y, thetaRadians) |
| 324 |
brx, bry := RotateCoordinate(cx, cy, bc.BottomRight.X, bc.BottomRight.Y, thetaRadians) |
| 325 |
blx, bly := RotateCoordinate(cx, cy, bc.BottomLeft.X, bc.BottomLeft.Y, thetaRadians) |
| 326 |
|
| 327 |
return BoxCorners{ |
| 328 |
TopLeft: Point{tlx, tly}, |
| 329 |
TopRight: Point{trx, try}, |
| 330 |
BottomRight: Point{brx, bry}, |
| 331 |
BottomLeft: Point{blx, bly}, |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
func (bc BoxCorners) Equals(other BoxCorners) bool { |
| 337 |
return bc.TopLeft.Equals(other.TopLeft) && |
| 338 |
bc.TopRight.Equals(other.TopRight) && |
| 339 |
bc.BottomRight.Equals(other.BottomRight) && |
| 340 |
bc.BottomLeft.Equals(other.BottomLeft) |
| 341 |
} |
| 342 |
|
| 343 |
func (bc BoxCorners) String() string { |
| 344 |
return fmt.Sprintf("BoxC{%s,%s,%s,%s}", bc.TopLeft.String(), bc.TopRight.String(), bc.BottomRight.String(), bc.BottomLeft.String()) |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
type Point struct { |
| 349 |
X, Y int |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
func (p Point) DistanceTo(other Point) float64 { |
| 354 |
dx := math.Pow(float64(p.X-other.X), 2) |
| 355 |
dy := math.Pow(float64(p.Y-other.Y), 2) |
| 356 |
return math.Pow(dx+dy, 0.5) |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
func (p Point) Equals(other Point) bool { |
| 361 |
return p.X == other.X && p.Y == other.Y |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
func (p Point) String() string { |
| 366 |
return fmt.Sprintf("P{%d,%d}", p.X, p.Y) |
| 367 |
} |