...
1 package textmeasure
2
3 import (
4 "math"
5
6 "oss.terrastruct.com/d2/lib/geo"
7 )
8
9 type rect struct {
10 tl *geo.Point
11 br *geo.Point
12 }
13
14 func newRect() *rect {
15 return &rect{
16 tl: geo.NewPoint(0, 0),
17 br: geo.NewPoint(0, 0),
18 }
19 }
20
21 func (r rect) w() float64 {
22 return r.br.X - r.tl.X
23 }
24
25 func (r rect) h() float64 {
26 return r.br.Y - r.tl.Y
27 }
28
29
30 func (r rect) norm() *rect {
31 return &rect{
32 tl: geo.NewPoint(
33 math.Min(r.tl.X, r.br.X),
34 math.Min(r.tl.Y, r.br.Y),
35 ),
36 br: geo.NewPoint(
37 math.Max(r.tl.X, r.br.X),
38 math.Max(r.tl.Y, r.br.Y),
39 ),
40 }
41 }
42
43 func (r1 *rect) union(r2 *rect) *rect {
44 r := newRect()
45 r.tl.X = math.Min(r1.tl.X, r2.tl.X)
46 r.tl.Y = math.Min(r1.tl.Y, r2.tl.Y)
47 r.br.X = math.Max(r1.br.X, r2.br.X)
48 r.br.Y = math.Max(r1.br.Y, r2.br.Y)
49
50 return r
51 }
52
View as plain text