-
Notifications
You must be signed in to change notification settings - Fork 0
/
size.go
49 lines (42 loc) · 1.15 KB
/
size.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
package topdown
import "fmt"
// Size is a 2D size.
type Size[T Number] struct {
Width T `json:"w"`
Height T `json:"h"`
}
// SizeSchemaStr is the JSON schema for a size.
const SizeSchemaStr = `{
"$id": "https://github.com/jamestunnell/topdown/size.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Size",
"description": "Dimensions for a 2D size.",
"type": "object",
"required": ["w", "h"],
"properties": {
"w": {
"type": "number",
"exclusiveMinimum": 0
},
"h": {
"type": "number",
"exclusiveMinimum": 0
}
}
}`
// Sz makes a new size.
func Sz[T Number](w, h T) Size[T] {
return Size[T]{Width: w, Height: h}
}
// Equal checks for equal width and height values.
func (s Size[T]) Equal(other Size[T]) bool {
return (s.Width == other.Width) && (s.Height == other.Height)
}
// Center makes a point that would be at the center of a rectangle with the current size, starting at (0,0).
func (s Size[T]) Center() Point[T] {
return Pt(s.Width/T(2), s.Height/T(2))
}
// String makes a string representing the size.
func (s Size[T]) String() string {
return fmt.Sprintf("%v x %v", s.Width, s.Height)
}