Added Div and Mul operations for points.

Moved Less method (for Point) to sort alphabetically.
This commit is contained in:
Sander Schobers 2019-12-23 15:09:33 +01:00
parent 4df2655b53
commit 3eeedf121e
3 changed files with 30 additions and 5 deletions

View File

@ -54,6 +54,16 @@ func (p Point) Add2D(x, y int) Point {
return Pt(p.X+x, p.Y+y) return Pt(p.X+x, p.Y+y)
} }
// Div divides the X and Y values of point p with t and returns the result.
func (p Point) Div(t int) Point {
return Pt(p.X/t, p.Y/t)
}
// DistInt returns the integer distance between the points p and q.
func (p Point) DistInt(q Point) int {
return ints.SubAbs(p.X, q.X) + ints.SubAbs(p.Y, q.Y)
}
// Less returns true if q is above (Y is smaller) or left (X is smaller) than p. Otherwise returns false. // Less returns true if q is above (Y is smaller) or left (X is smaller) than p. Otherwise returns false.
func (p Point) Less(q Point) bool { func (p Point) Less(q Point) bool {
if p.Y == q.Y { if p.Y == q.Y {
@ -62,11 +72,6 @@ func (p Point) Less(q Point) bool {
return p.Y < q.Y return p.Y < q.Y
} }
// DistInt returns the integer distance between the points p and q.
func (p Point) DistInt(q Point) int {
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. // Mul multiplier the X and Y values of point p with t and returns the result.
func (p Point) Mul(t int) Point { func (p Point) Mul(t int) Point {
return Pt(p.X*t, p.Y*t) return Pt(p.X*t, p.Y*t)

View File

@ -91,6 +91,11 @@ func (p PointF) DistanceToPolygon(q PolygonF) float64 {
return p.DistanceToLines(append(q.Points, q.Points[0])) return p.DistanceToLines(append(q.Points, q.Points[0]))
} }
// Div divides the X and Y values of point p with t and returns the result.
func (p PointF) Div(t float64) PointF {
return PtF(p.X/t, p.Y/t)
}
// In tests if the point p is inside the rectangle r. // In tests if the point p is inside the rectangle r.
func (p PointF) In(r RectangleF) bool { func (p PointF) In(r RectangleF) bool {
if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y { if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y {
@ -122,6 +127,11 @@ func (p PointF) Invert() PointF {
return PointF{-p.X, -p.Y} return PointF{-p.X, -p.Y}
} }
// Mul multiplier the X and Y values of point p with t and returns the result.
func (p PointF) Mul(t float64) PointF {
return PtF(p.X*t, p.Y*t)
}
// Sub subtracts q as a vector from p. // Sub subtracts q as a vector from p.
func (p PointF) Sub(q PointF) PointF { func (p PointF) Sub(q PointF) PointF {
return PointF{p.X - q.X, p.Y - q.Y} return PointF{p.X - q.X, p.Y - q.Y}

View File

@ -91,6 +91,11 @@ func (p PointF32) DistanceToPolygon(q PolygonF32) float32 {
return p.DistanceToLines(append(q.Points, q.Points[0])) return p.DistanceToLines(append(q.Points, q.Points[0]))
} }
// Div divides the X and Y values of point p with t and returns the result.
func (p PointF32) Div(t float32) PointF32 {
return PtF32(p.X/t, p.Y/t)
}
// In tests if the point p is inside the rectangle r. // In tests if the point p is inside the rectangle r.
func (p PointF32) In(r RectangleF32) bool { func (p PointF32) In(r RectangleF32) bool {
if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y { if p.X < r.Min.X || p.X >= r.Max.X || p.Y < r.Min.Y || p.Y >= r.Max.Y {
@ -122,6 +127,11 @@ func (p PointF32) Invert() PointF32 {
return PointF32{-p.X, -p.Y} return PointF32{-p.X, -p.Y}
} }
// Mul multiplier the X and Y values of point p with t and returns the result.
func (p PointF32) Mul(t float32) PointF32 {
return PtF32(p.X*t, p.Y*t)
}
// Sub subtracts q as a vector from p. // Sub subtracts q as a vector from p.
func (p PointF32) Sub(q PointF32) PointF32 { func (p PointF32) Sub(q PointF32) PointF32 {
return PointF32{p.X - q.X, p.Y - q.Y} return PointF32{p.X - q.X, p.Y - q.Y}