Moved integer related methods to separate package.

This commit is contained in:
Sander Schobers 2019-12-21 12:48:57 +01:00
parent 40abdf0439
commit fc18ea9ce8
3 changed files with 77 additions and 74 deletions

View File

@ -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
}

71
ints/math.go Normal file
View File

@ -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
}

View File

@ -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".