Added PointF32 implementation of some methods.
This commit is contained in:
parent
dd6d42a60a
commit
8ad3ad6a18
6
point.go
6
point.go
@ -2,6 +2,7 @@ package geom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
_image "image"
|
_image "image"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Point is exposing the standard library image.Point in the geom package.
|
// Point is exposing the standard library image.Point in the geom package.
|
||||||
@ -11,3 +12,8 @@ type Point _image.Point
|
|||||||
func Pt(x, y int) Point {
|
func Pt(x, y int) Point {
|
||||||
return Point{x, y}
|
return Point{x, y}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String formats the point p as a string.
|
||||||
|
func (p Point) String() string {
|
||||||
|
return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")"
|
||||||
|
}
|
||||||
|
@ -2,7 +2,6 @@ package geom
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PointF is an X, Y coordinate pair (floating point).
|
// PointF is an X, Y coordinate pair (floating point).
|
||||||
@ -18,11 +17,6 @@ func PtF(x, y float64) PointF {
|
|||||||
return PointF{X: x, Y: y}
|
return PointF{X: x, Y: y}
|
||||||
}
|
}
|
||||||
|
|
||||||
// String formats the point p as a string.
|
|
||||||
func (p Point) String() string {
|
|
||||||
return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")"
|
|
||||||
}
|
|
||||||
|
|
||||||
// To32 transforms the point p into a PointF32.
|
// To32 transforms the point p into a PointF32.
|
||||||
func (p PointF) To32() PointF32 {
|
func (p PointF) To32() PointF32 {
|
||||||
return PointF32{float32(p.X), float32(p.Y)}
|
return PointF32{float32(p.X), float32(p.Y)}
|
||||||
|
43
pointf32.go
43
pointf32.go
@ -1,5 +1,9 @@
|
|||||||
package geom
|
package geom
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
// PointF32 is an X, Y coordinate pair (floating point, 32 bits).
|
// PointF32 is an X, Y coordinate pair (floating point, 32 bits).
|
||||||
type PointF32 struct {
|
type PointF32 struct {
|
||||||
X, Y float32
|
X, Y float32
|
||||||
@ -13,6 +17,45 @@ func PtF32(x, y float32) PointF32 {
|
|||||||
return PointF32{x, y}
|
return PointF32{x, y}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// To64 transforms the point p into a PointF.
|
||||||
|
func (p PointF32) To64() PointF {
|
||||||
|
return PointF{float64(p.X), float64(p.Y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds q as a vector to p.
|
||||||
|
func (p PointF32) Add(q PointF32) PointF32 {
|
||||||
|
return PointF32{p.X + q.X, p.Y + q.Y}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sub subtracts q as a vector from p.
|
||||||
|
func (p PointF32) Sub(q PointF32) PointF32 {
|
||||||
|
return PointF32{p.X - q.X, p.Y - q.Y}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AngleTo calculates the angle [0..2*Pi) from point p to point q.
|
||||||
|
func (p PointF32) AngleTo(q PointF32) float32 {
|
||||||
|
a := float32(math.Atan(float64((p.Y - q.Y) / (p.X - q.X))))
|
||||||
|
if q.X < p.X {
|
||||||
|
return a + math.Pi
|
||||||
|
}
|
||||||
|
if a < 0 {
|
||||||
|
a += 2 * math.Pi
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
// Distance calculates the distance between points p and q.
|
||||||
|
func (p PointF32) Distance(q PointF32) float32 {
|
||||||
|
return float32(math.Sqrt(float64(p.Distance2(q))))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Distance2 calculates the squared distance between points p and q.
|
||||||
|
func (p PointF32) Distance2(q PointF32) float32 {
|
||||||
|
dx := q.X - p.X
|
||||||
|
dy := q.Y - p.Y
|
||||||
|
return dx*dx + dy*dy
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user