math

package
v0.11.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	North = iota
	NorthEast
	East
	SouthEast
	South
	SouthWest
	West
	NorthWest
)
View Source
const FeetToNauticalMiles = 1 / NauticalMilesToFeet
View Source
const NMPerLatitude = 60
View Source
const NauticalMilesToFeet = 6076.12

Variables

This section is empty.

Functions

func Abs

func Abs[V constraints.Integer | constraints.Float](x V) V

func Add2f

func Add2f(a [2]float32, b [2]float32) [2]float32

a+b

func AngleBetween

func AngleBetween(v1, v2 [2]float32) float32

Equivalent to acos(Dot(a, b)), but more numerically stable. via http://www.plunk.org/~hatch/rightway.html

func Atan2

func Atan2(y, x float32) float32

func Ceil

func Ceil(v float32) float32

func CirclePoints

func CirclePoints(nsegs int) [][2]float32

CirclePoints returns the vertices for a unit circle at the origin with the given number of segments; it creates the vertex slice if this tessellation rate hasn't been seen before and otherwise returns a preexisting one.

func Clamp

func Clamp[T constraints.Ordered](x T, low T, high T) T

func ClosestPointOnLine

func ClosestPointOnLine(line [2][2]float32, p [2]float32) [2]float32

ClosestPointOnLine returns the closest point on the (infinite) line to the given point p.

func Compass

func Compass(heading float32) string

compass converts a heading expressed into degrees into a string corresponding to the closest compass direction.

func Cos

func Cos(a float32) float32

func Degrees

func Degrees(r float32) float32

Degrees converts an angle expressed in degrees to radians

func Distance2f

func Distance2f(a [2]float32, b [2]float32) float32

Distance between two points

func Dot

func Dot(a, b [2]float32) float32

func EquilateralTriangleVertices

func EquilateralTriangleVertices(height float32) [3][2]float32

Returns the vertex coordinates of an equilateral triangle centered at the origin with specified height.

func Exp

func Exp(x float32) float32

func Floor

func Floor(v float32) float32

func GCD

func GCD(a, b int) int

greatest common divisor

func GenRange

func GenRange[T constraints.Integer | constraints.Float](low, high, step T) []T

func Heading2LL

func Heading2LL(from Point2LL, to Point2LL, nmPerLongitude float32, magCorrection float32) float32

Heading2ll returns the heading from the point |from| to the point |to| in degrees. The provided points should be in latitude-longitude coordinates and the provided magnetic correction is applied to the result.

func HeadingAsHour

func HeadingAsHour(heading float32) int

headingAsHour converts a heading expressed in degrees into the closest "o'clock" value, with an integer result in the range [1,12].

func HeadingDifference

func HeadingDifference(a float32, b float32) float32

HeadingDifference returns the minimum difference between two headings. (i.e., the result is always in the range [0,180].)

func LCM

func LCM(a, b int) int

least common multiple

func LL2NM

func LL2NM(p Point2LL, nmPerLongitude float32) [2]float32

LL2NM converts a point expressed in latitude-longitude coordinates to nautical mile coordinates; this is useful for example for reasoning about distances, since both axes then have the same measure.

func Length2f

func Length2f(v [2]float32) float32

Length of v

func Lerp

func Lerp(x, a, b float32) float32

func Lerp2f

func Lerp2f(x float32, a [2]float32, b [2]float32) [2]float32

Linearly interpolate x of the way between a and b. x==0 corresponds to a, x==1 corresponds to b, etc.

func LineLineIntersect

func LineLineIntersect(p1f, p2f, p3f, p4f [2]float32) ([2]float32, bool)

LineLineIntersect returns the intersection point of the two lines specified by the vertices (p1f, p2f) and (p3f, p4f). An additional returned Boolean value indicates whether a valid intersection was found. (There's no intersection for parallel lines, and none may be found in cases with tricky numerics.)

func Max

func Max[T constraints.Ordered](a, b T) T

func Mid2f

func Mid2f(a [2]float32, b [2]float32) [2]float32

midpoint of a and b

func Min

func Min[T constraints.Ordered](a, b T) T

func Mod

func Mod(a, b float32) float32

func NMDistance2LL

func NMDistance2LL(a Point2LL, b Point2LL) float32

NMDistance2ll returns the distance in nautical miles between two provided lat-long coordinates.

func NMLength2LL

func NMLength2LL(a Point2LL, nmPerLongitude float32) float32

NMLength2ll returns the length of a vector expressed in lat-long coordinates.

func NMPerLongitude

func NMPerLongitude(p Point2LL) float32

func Normalize2f

func Normalize2f(a [2]float32) [2]float32

Normalizes the given vector.

func NormalizeHeading

func NormalizeHeading(h float32) float32

Reduces it to [0,360).

func OppositeHeading

func OppositeHeading(h float32) float32

func Overlaps

func Overlaps(a Extent2D, b Extent2D) bool

Overlaps returns true if the two provided Extent2Ds overlap.

func PointInPolygon

func PointInPolygon(p [2]float32, pts [][2]float32) bool

PointInPolygon checks whether the given point is inside the given polygon; it assumes that the last vertex does not repeat the first one, and so includes the edge from pts[len(pts)-1] to pts[0] in its test.

func PointInPolygon2LL

func PointInPolygon2LL(p Point2LL, pts []Point2LL) bool

func PointLineDistance

func PointLineDistance(p, p0, p1 [2]float32) float32

PointLineDistance returns the minimum distance from the point p to the infinite line defined by (p0, p1).

func PointSegmentDistance

func PointSegmentDistance(p, v, w [2]float32) float32

Return minimum distance between line segment vw and point p https://stackoverflow.com/a/1501725

func Pow

func Pow(a, b float32) float32

func Radians

func Radians(d float32) float32

Radians converts an angle expressed in radians to degrees

func RayRayMinimumDistance

func RayRayMinimumDistance(p0, d0, p1, d1 [2]float32) float32

RayRayMinimumDistance takes two rays p0+d0*t and p1+d1*t and returns the value of t where their distance is minimized.

func Rotator2f

func Rotator2f(angle float32) func([2]float32) [2]float32

rotator2f returns a function that rotates points by the specified angle (given in degrees).

func SafeACos

func SafeACos(a float32) float32

func SafeASin

func SafeASin(a float32) float32

func Scale2f

func Scale2f(a [2]float32, s float32) [2]float32

a*s

func SetLocationResolver

func SetLocationResolver(r LocationResolver)

func ShortCompass

func ShortCompass(heading float32) string

ShortCompass converts a heading expressed in degrees into an abbreviated string corresponding to the closest compass direction.

func Sign

func Sign(v float32) float32

func SignedPointLineDistance

func SignedPointLineDistance(p, p0, p1 [2]float32) float32

SignedPointLineDistance returns the signed distance from the point p to the infinite line defined by (p0, p1) where points to the right of the line have negative distances.

func Sin

func Sin(a float32) float32

func Sqr

func Sqr[V constraints.Integer | constraints.Float](v V) V

func Sqrt

func Sqrt(a float32) float32

func Sub2f

func Sub2f(a [2]float32, b [2]float32) [2]float32

a-b

func Tan

func Tan(a float32) float32

Types

type CardinalOrdinalDirection

type CardinalOrdinalDirection int

func ParseCardinalOrdinalDirection

func ParseCardinalOrdinalDirection(s string) (CardinalOrdinalDirection, error)

func (CardinalOrdinalDirection) Heading

func (co CardinalOrdinalDirection) Heading() float32

func (CardinalOrdinalDirection) ShortString

func (co CardinalOrdinalDirection) ShortString() string

type Extent2D

type Extent2D struct {
	P0, P1 [2]float32
}

Extent2D represents a 2D bounding box with the two vertices at its opposite minimum and maximum corners.

func EmptyExtent2D

func EmptyExtent2D() Extent2D

EmptyExtent2D returns an Extent2D representing an empty bounding box.

func Extent2DFromPoints

func Extent2DFromPoints(pts [][2]float32) Extent2D

Extent2DFromPoints returns an Extent2D that bounds all of the provided points.

func Union

func Union(e Extent2D, p [2]float32) Extent2D

func (Extent2D) Center

func (e Extent2D) Center() [2]float32

func (Extent2D) ClosestPointInBox

func (e Extent2D) ClosestPointInBox(p [2]float32) [2]float32

ClosestPointInBox returns the closest point to p that is inside the Extent2D. (If p is already inside it, then it is returned.)

func (Extent2D) Expand

func (e Extent2D) Expand(d float32) Extent2D

Expand expands the extent by the given distance in all directions.

func (Extent2D) Height

func (e Extent2D) Height() float32

func (Extent2D) Inside

func (e Extent2D) Inside(p [2]float32) bool

func (Extent2D) IntersectRay

func (e Extent2D) IntersectRay(org, dir [2]float32) (bool, float32, float32)

IntersectRay find the intersections of the ray with given origin and direction with the Extent2D. The returned Boolean value indicates whether an intersection was found. If true, the two returned floating-point values give the parametric distances along the ray where the intersections occurred.

func (Extent2D) Lerp

func (e Extent2D) Lerp(p [2]float32) [2]float32

func (Extent2D) Offset

func (e Extent2D) Offset(p [2]float32) Extent2D

func (Extent2D) Scale

func (e Extent2D) Scale(s float32) Extent2D

func (Extent2D) Width

func (e Extent2D) Width() float32

type LocationResolver

type LocationResolver interface {
	Resolve(s string) (Point2LL, error)
}

type Matrix3

type Matrix3 [3][3]float32

func Identity3x3

func Identity3x3() Matrix3

func MakeMatrix3

func MakeMatrix3(m00, m01, m02, m10, m11, m12, m20, m21, m22 float32) Matrix3

func (Matrix3) Determinant

func (m Matrix3) Determinant() float32

func (Matrix3) Inverse

func (m Matrix3) Inverse() Matrix3

func (Matrix3) Ortho

func (m Matrix3) Ortho(x0, x1, y0, y1 float32) Matrix3

func (Matrix3) PostMultiply

func (m Matrix3) PostMultiply(m2 Matrix3) Matrix3

func (Matrix3) Rotate

func (m Matrix3) Rotate(theta float32) Matrix3

func (Matrix3) Scale

func (m Matrix3) Scale(x, y float32) Matrix3

func (Matrix3) TransformPoint

func (m Matrix3) TransformPoint(p [2]float32) [2]float32

func (Matrix3) TransformVector

func (m Matrix3) TransformVector(p [2]float32) [2]float32

func (Matrix3) Translate

func (m Matrix3) Translate(x, y float32) Matrix3

type Point2LL

type Point2LL [2]float32

Point2LL represents a 2D point on the Earth in latitude-longitude. Important: 0 (x) is longitude, 1 (y) is latitude

func Add2LL

func Add2LL(a Point2LL, b Point2LL) Point2LL

func Mid2LL

func Mid2LL(a Point2LL, b Point2LL) Point2LL

func NM2LL

func NM2LL(p [2]float32, nmPerLongitude float32) Point2LL

NM2LL converts a point expressed in nautical mile coordinates to lat-long.

func ParseLatLong

func ParseLatLong(llstr []byte) (Point2LL, error)

func Sub2LL

func Sub2LL(a Point2LL, b Point2LL) Point2LL

func (Point2LL) DDString

func (p Point2LL) DDString() string

DDString returns the position in decimal degrees, e.g.: (39.860901, -75.274864)

func (Point2LL) DMSString

func (p Point2LL) DMSString() string

DMSString returns the position in degrees minutes, seconds, e.g. N039.51.39.243,W075.16.29.511

func (Point2LL) IsZero

func (p Point2LL) IsZero() bool

func (Point2LL) Latitude

func (p Point2LL) Latitude() float32

func (Point2LL) Longitude

func (p Point2LL) Longitude() float32

func (Point2LL) MarshalJSON

func (p Point2LL) MarshalJSON() ([]byte, error)

Store Point2LLs as strings is JSON, for compactness/friendliness...

func (*Point2LL) UnmarshalJSON

func (p *Point2LL) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL