-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize.go
More file actions
148 lines (125 loc) · 3 KB
/
size.go
File metadata and controls
148 lines (125 loc) · 3 KB
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
// SPDX-FileCopyrightText: 2018 Raph Levien
// SPDX-FileCopyrightText: 2024 Dominik Honnef and contributors
//
// SPDX-License-Identifier: MIT
// SPDX-FileAttributionText: https://github.com/linebender/kurbo
package curve
import (
"fmt"
"math"
)
type Size struct {
Width float64
Height float64
}
// Sz returns the size x×y.
func Sz(w, h float64) Size {
return Size{
Width: w,
Height: h,
}
}
func (sz Size) String() string {
return fmt.Sprintf("%g×%g", sz.Width, sz.Height)
}
func (sz Size) AsVec2() Vec2 {
return Vec2{
X: sz.Width,
Y: sz.Height,
}
}
func (sz Size) MaxSide() float64 {
return max(sz.Width, sz.Height)
}
func (sz Size) MinSide() float64 {
return min(sz.Width, sz.Height)
}
func (sz Size) Area() float64 {
return sz.Width * sz.Height
}
// Min returns the component-wise minimum of two sizes.
func (sz Size) Min(o Size) Size {
return Size{
Width: min(sz.Width, o.Width),
Height: min(sz.Height, o.Height),
}
}
// Max returns the component-wise maximum of two sizes.
func (sz Size) Max(o Size) Size {
return Size{
Width: max(sz.Width, o.Width),
Height: max(sz.Height, o.Height),
}
}
func (sz Size) Clamp(minSz, maxSz Size) Size {
w := min(max(sz.Width, minSz.Width), maxSz.Width)
h := min(max(sz.Height, minSz.Height), maxSz.Height)
return Size{
Width: w,
Height: h,
}
}
func (sz Size) Splat() (w float64, h float64) {
return sz.Width, sz.Height
}
// Round returns a new size with width and height rounded to the nearest integers.
func (sz Size) Round() Size {
return Size{
Width: math.Round(sz.Width),
Height: math.Round(sz.Height),
}
}
// Ceil returns a new size with width and height rounded up to the nearest integers.
func (sz Size) Ceil() Size {
return Size{
Width: math.Ceil(sz.Width),
Height: math.Ceil(sz.Height),
}
}
// Floor returns a new size with width and height rounded down to the nearest integers.
func (sz Size) Floor() Size {
return Size{
Width: math.Floor(sz.Width),
Height: math.Floor(sz.Height),
}
}
// Expand returns a new size with width and height rounded away from zero to the
// nearest integers.
func (sz Size) Expand() Size {
return Size{
Width: expand(sz.Width),
Height: expand(sz.Height),
}
}
// Trunc returns a new size with width and height rounded towards zero to the nearest
// integers.
func (sz Size) Trunc() Size {
return Size{
Width: math.Trunc(sz.Width),
Height: math.Trunc(sz.Height),
}
}
func (sz Size) AspectRatio() float64 {
return sz.Width / sz.Height
}
// IsInf reports whether at least one of width and height is infinite.
func (sz Size) IsInf() bool {
return math.IsInf(sz.Width, 0) || math.IsInf(sz.Height, 0)
}
// IsNaN reports whether at least one of width and height is NaN.
func (sz Size) IsNaN() bool {
return math.IsNaN(sz.Width) || math.IsNaN(sz.Height)
}
// Scale multiplies sz by f.
func (sz Size) Scale(f float64) Size {
return Size{
Width: sz.Width * f,
Height: sz.Height * f,
}
}
func (sz Size) Add(v Vec2) Size {
return Size{
Width: sz.Width + v.X,
Height: sz.Height + v.Y,
}
}