github.com/richardwilkes/toolbox@v1.121.0/xmath/geom/insets.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package geom 11 12 import ( 13 "fmt" 14 15 "github.com/richardwilkes/toolbox/xmath" 16 ) 17 18 // Insets defines margins on each side of a rectangle. 19 type Insets[T xmath.Numeric] struct { 20 Top T `json:"top"` 21 Left T `json:"left"` 22 Bottom T `json:"bottom"` 23 Right T `json:"right"` 24 } 25 26 // NewInsets returns an Insets with the given values for its edges. 27 func NewInsets[T xmath.Numeric](top, left, bottom, right T) Insets[T] { 28 return Insets[T]{Top: top, Left: left, Bottom: bottom, Right: right} 29 } 30 31 // NewUniformInsets returns an Insets whose edges all have the same value. 32 func NewUniformInsets[T xmath.Numeric](amount T) Insets[T] { 33 return NewInsets(amount, amount, amount, amount) 34 } 35 36 // NewSymmetricInsets returns an Insets whose edges match their opposite edge. 37 func NewSymmetricInsets[T xmath.Numeric](h, v T) Insets[T] { 38 return NewInsets(v, h, v, h) 39 } 40 41 // NewHorizontalInsets returns an Insets whose left and right edges have the specified value. 42 func NewHorizontalInsets[T xmath.Numeric](amount T) Insets[T] { 43 return Insets[T]{Left: amount, Right: amount} 44 } 45 46 // NewVerticalInsets returns an Insets whose top and bottom edges have the specified value. 47 func NewVerticalInsets[T xmath.Numeric](amount T) Insets[T] { 48 return Insets[T]{Top: amount, Bottom: amount} 49 } 50 51 // ConvertInsets converts a Insets of type F into one of type T. 52 func ConvertInsets[T, F xmath.Numeric](i Insets[F]) Insets[T] { 53 return NewInsets(T(i.Top), T(i.Left), T(i.Bottom), T(i.Right)) 54 } 55 56 // Add returns a new Insets which is the result of adding this Insets with the provided Insets. 57 func (i Insets[T]) Add(in Insets[T]) Insets[T] { 58 return NewInsets(i.Top+in.Top, i.Left+in.Left, i.Bottom+in.Bottom, i.Right+in.Right) 59 } 60 61 // Sub returns a new Insets which is the result of subtracting the provided Insets from this Insets. 62 func (i Insets[T]) Sub(in Insets[T]) Insets[T] { 63 return NewInsets(i.Top-in.Top, i.Left-in.Left, i.Bottom-in.Bottom, i.Right-in.Right) 64 } 65 66 // Mul returns a new Insets which is the result of multiplying the values of this Insets by the value. 67 func (i Insets[T]) Mul(value T) Insets[T] { 68 return NewInsets(i.Top*value, i.Left*value, i.Bottom*value, i.Right*value) 69 } 70 71 // Div returns a new Insets which is the result of dividing the values of this Insets by the value. 72 func (i Insets[T]) Div(value T) Insets[T] { 73 return NewInsets(i.Top/value, i.Left/value, i.Bottom/value, i.Right/value) 74 } 75 76 // Size returns the Size of the Insets. 77 func (i Insets[T]) Size() Size[T] { 78 return NewSize(i.Width(), i.Height()) 79 } 80 81 // Width returns the sum of the left and right insets. 82 func (i Insets[T]) Width() T { 83 return i.Left + i.Right 84 } 85 86 // Height returns the sum of the top and bottom insets. 87 func (i Insets[T]) Height() T { 88 return i.Top + i.Bottom 89 } 90 91 // String implements fmt.Stringer. 92 func (i Insets[T]) String() string { 93 return fmt.Sprintf("%#v,%#v,%#v,%#v", i.Top, i.Left, i.Bottom, i.Right) 94 }