From fc18ea9ce83e83617126b6e47b49f8215820cf02 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sat, 21 Dec 2019 12:48:57 +0100 Subject: [PATCH] Moved integer related methods to separate package. --- ints.go => ints/conv.go | 72 +---------------------------------------- ints/math.go | 71 ++++++++++++++++++++++++++++++++++++++++ point.go | 8 +++-- 3 files changed, 77 insertions(+), 74 deletions(-) rename ints.go => ints/conv.go (69%) create mode 100644 ints/math.go diff --git a/ints.go b/ints/conv.go similarity index 69% rename from ints.go rename to ints/conv.go index f188015..d8914bb 100644 --- a/ints.go +++ b/ints/conv.go @@ -1,4 +1,4 @@ -package geom +package ints import ( "bytes" @@ -6,14 +6,6 @@ import ( "strings" ) -// AbsInt returns the absolute value of i. -func AbsInt(i int) int { - if 0 > i { - return -i - } - return i -} - // Itoa returns i as a string. func Itoa(i int) string { return strconv.Itoa(i) @@ -48,38 +40,6 @@ func FmtInts64(ints []int64, sep string) string { return out.String() } -// MinInt returns the minimum of a and b. -func MinInt(a, b int) int { - if a < b { - return a - } - return b -} - -// MinInt64 returns the minimum of a and b. -func MinInt64(a, b int64) int64 { - if a < b { - return a - } - return b -} - -// MaxInt returns the maximum of a and b. -func MaxInt(a, b int) int { - if a > b { - return a - } - return b -} - -// MaxInt64 returns the maximum of a and b. -func MaxInt64(a, b int64) int64 { - if a > b { - return a - } - return b -} - // MustAtoi parses string s as an integer. Method panics if the conversion fails. func MustAtoi(s string) int { var i, err = strconv.Atoi(s) @@ -116,28 +76,6 @@ func MustAstois64(s ...string) []int64 { return res } -// NormInt returns the normalized value of i (-1, 0 or 1). -func NormInt(i int) int { - if i < 0 { - return -1 - } - if i > 0 { - return 1 - } - return 0 -} - -// NormInt64 returns the normalized value of i (-1, 0 or 1). -func NormInt64(i int64) int64 { - if i < 0 { - return -1 - } - if i > 0 { - return 1 - } - return 0 -} - // SplitInts splits the string s by separator sep, converts every part to an integer and returns all the integers as a slice. Method panics if one of the conversion fails. func SplitInts(s, sep string) []int { var numbers = strings.Split(s, sep) @@ -157,11 +95,3 @@ func SplitInts64(s, sep string) []int64 { } return ints } - -// SubAbsInt subtracts a from b and returns the absolute value of the result. -func SubAbsInt(a, b int) int { - if a > b { - return a - b - } - return b - a -} diff --git a/ints/math.go b/ints/math.go new file mode 100644 index 0000000..2ae68f6 --- /dev/null +++ b/ints/math.go @@ -0,0 +1,71 @@ +package ints + +// Abs returns the absolute value of i. +func Abs(i int) int { + if 0 > i { + return -i + } + return i +} + +// Min returns the minimum of a and b. +func Min(a, b int) int { + if a < b { + return a + } + return b +} + +// Min64 returns the minimum of a and b. +func Min64(a, b int64) int64 { + if a < b { + return a + } + return b +} + +// Max returns the maximum of a and b. +func Max(a, b int) int { + if a > b { + return a + } + return b +} + +// Max64 returns the maximum of a and b. +func Max64(a, b int64) int64 { + if a > b { + return a + } + return b +} + +// Norm returns the normalized value of i (-1, 0 or 1). +func Norm(i int) int { + if i < 0 { + return -1 + } + if i > 0 { + return 1 + } + return 0 +} + +// Norm64 returns the normalized value of i (-1, 0 or 1). +func Norm64(i int64) int64 { + if i < 0 { + return -1 + } + if i > 0 { + return 1 + } + return 0 +} + +// SubAbs subtracts a from b and returns the absolute value of the result. +func SubAbs(a, b int) int { + if a > b { + return a - b + } + return b - a +} diff --git a/point.go b/point.go index 308ffb7..587f7f8 100644 --- a/point.go +++ b/point.go @@ -2,6 +2,8 @@ package geom import ( "fmt" + + "opslag.de/schobers/geom/ints" ) // ZeroPt represents the coordinate (0, 0) @@ -39,7 +41,7 @@ func NewPt(x, y int) *Point { // Abs returns a point the absolute values of X and Y. func (p Point) Abs() Point { - return Pt(AbsInt(p.X), AbsInt(p.Y)) + return Pt(ints.Abs(p.X), ints.Abs(p.Y)) } // Add returns the sum of p and q. @@ -62,7 +64,7 @@ func (p Point) Less(q Point) bool { // DistInt returns the integer distance between the points p and q. func (p Point) DistInt(q Point) int { - return SubAbsInt(p.X, q.X) + SubAbsInt(p.Y, q.Y) + return ints.SubAbs(p.X, q.X) + ints.SubAbs(p.Y, q.Y) } // Mul multiplier the X and Y values of point p with t and returns the result. @@ -72,7 +74,7 @@ func (p Point) Mul(t int) Point { // Norm returns the point with both X and Y normalized. func (p Point) Norm() Point { - return Pt(NormInt(p.X), NormInt(p.Y)) + return Pt(ints.Norm(p.X), ints.Norm(p.Y)) } // String returns a string representation of point p "X, Y".