vectors

package
v0.0.0-...-ce97658 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2024 License: Apache-2.0 Imports: 2 Imported by: 16

README

vectors

This package contains various helper functions for vector math. There's nothing fancy or special about it :)

Here you can find:

  • 2d vectors
    • Add
    • Multiply
    • Normalize
    • Length
  • 3d vectors
    • Add / Subtract
    • Multiply
    • Normalize
    • Length
    • Cross product
    • Dot product

Documentation

Overview

Package vectors implements various vector thingies.

Index

Constants

This section is empty.

Variables

View Source
var Up = Vec3{0, 1, 0}

Up is a vector pointing up (0, 1, 0).

Functions

func Angle2

func Angle2(v Vec2) float64

Angle2 returns the angle of a vector in degrees.

func AngleBetween

func AngleBetween(p1, p2, p3 Vec2) float64

AngleBetween returns the angle between the three points.

func AngleBetweenTwoVectors

func AngleBetweenTwoVectors(v1, v2 Vec2) float64

AngleBetweenTwoVectors returns the angle between two vectors.

func Cross2

func Cross2(v1, v2 Vec2) float64

Cross2 returns the crossproduct of two vectors.

func Dist2

func Dist2(a, b Vec2) float64

Dist2 returns the eucledian distance between two vectors.

func Dist3

func Dist3(a, b Vec3) float64

Dist3 returns the eucledian distance between two vectors.

func Dot2

func Dot2(v1, v2 Vec2) float64

Dot2 returns the dotproduct of two vectors.

func Dot3

func Dot3(v1, v2 Vec3) float64

Dot3 returns the dotproduct of two vectors.

func EqualVec2

func EqualVec2(a, b Vec2) bool

EqualVec2 returns true if the two vectors are equal (within a small epsilon).

func Equalish

func Equalish(a, b float64) bool

Equalish returns true if the two values are equalish.

func MinDegreesDifference2

func MinDegreesDifference2(a, b float64) float64

MinDegreesDifference2 returns the minimum difference between two angles in degrees.

func PointInTriangle

func PointInTriangle(p Vec2, triangle []Vec2) bool

Types

type IVec2

type IVec2 struct {
	X int64
	Y int64
}

IVec2 represents an integer vector of type int64.

func NewIVec2

func NewIVec2(x, y int64) IVec2

NewIVec2 returns a new vector.

type Mat3

type Mat3 [3][3]float64

Column-major order 0 3 6 1 4 7 2 5 8

func (Mat3) MulVec3

func (m Mat3) MulVec3(v Vec3) Vec3

type Ray2

type Ray2 struct {
	Origin Vec2 // Origin of the ray.
	Dir    Vec2 // Direction of the ray.
}

Ray2 represents a ray in 2D space.

func (Ray2) Intersects

func (r Ray2) Intersects(s Segment) bool

Intersects returns true if the ray intersects with the line segment.

type Segment

type Segment struct {
	Start Vec2
	End   Vec2
}

Segment represents a line segment.

func NewSegment

func NewSegment(start, end Vec2) Segment

NewSegment returns a new segment.

func (Segment) ClosestPoint

func (s1 Segment) ClosestPoint(p Vec2) Vec2

ClosestPoint returns the closest point on the segment to the given point.

func (Segment) DistanceToPoint

func (s1 Segment) DistanceToPoint(p Vec2) float64

DistanceToPoint returns the distance to the given point.

func (Segment) Intersect

func (s1 Segment) Intersect(s2 Segment) bool

Intersect returns true if the two segments intersect.

func (Segment) IntersectPoint

func (s1 Segment) IntersectPoint(s2 Segment) *Vec2

IntersectPoint returns the intersection point of the two segments.

func (Segment) Intersects

func (s1 Segment) Intersects(l2 Segment) (bool, Vec2)

Intersects returns true if the line intersects with the other line.

func (Segment) IsPointOnLine

func (s1 Segment) IsPointOnLine(p Vec2) bool

IsPointOnLine returns true if the point is on the line.

func (Segment) Len

func (s Segment) Len() float64

Len returns the length of the segment.

func (Segment) Midpoint

func (s1 Segment) Midpoint() Vec2

Midpoint returns the midpoint of the segment.

type Vec2

type Vec2 struct {
	X float64
	Y float64
}

Vec2 represents a two dimensional vector.

func NewVec2

func NewVec2(x, y float64) Vec2

NewVec2 returns a new two dimensional vector.

func Normalize

func Normalize(vec Vec2) (dest Vec2)

Normalize returns the normalized vector (with a length/magnitude of 1).

func RandomVec2

func RandomVec2(scale float64) Vec2

RandomVec2 returns a randomized vector.

func Sub2

func Sub2(v1, v2 Vec2) Vec2

Sub2 subtracts v2 from v1 and returns the result.

func (Vec2) Add

func (v1 Vec2) Add(v2 Vec2) Vec2

Add adds the current vector with v2 and returns the result.

func (*Vec2) AddToThis

func (v1 *Vec2) AddToThis(v2 Vec2)

AddToThis adds v2 to the current vector.

func (Vec2) Angle

func (v Vec2) Angle() float64

Angle returns the angle of the vector.

func (Vec2) Cross

func (v1 Vec2) Cross(v2 Vec2) float64

Cross returns the crossproduct of two vectors.

func (Vec2) DistanceTo

func (v Vec2) DistanceTo(v2 Vec2) float64

DistanceTo returns the distance to the given vector.

func (Vec2) DistanceToSquared

func (v Vec2) DistanceToSquared(v2 Vec2) float64

DistanceToSquared returns the squared distance to the given vector.

func (Vec2) Dot

func (v1 Vec2) Dot(v2 Vec2) float64

Dot returns the dotproduct of two vectors.

func (Vec2) Equal

func (v1 Vec2) Equal(v2 Vec2) bool

Equal returns true if the vectors are equal.

func (Vec2) Equalish

func (v1 Vec2) Equalish(v2 Vec2) bool

Equalish returns true if the vectors are equalish.

func (Vec2) Len

func (v1 Vec2) Len() float64

Len returns the length (or magnitude) of the vector.

func (Vec2) LengthSquared

func (v Vec2) LengthSquared() float64

LengthSquared returns the squared length of the vector.

func (Vec2) Mul

func (v1 Vec2) Mul(c float64) Vec2

Mul multiplies the current vector with c and returns the result.

func (*Vec2) MulWithThis

func (v1 *Vec2) MulWithThis(c float64)

MulWithThis multiplies the current vector with c.

func (Vec2) Negate

func (v Vec2) Negate() Vec2

Negate negates the vector.

func (Vec2) Normal

func (v Vec2) Normal() Vec2

Normal returns the normal of the vector.

func (Vec2) Normalize

func (v Vec2) Normalize() Vec2

Normalize returns the normalized vector (with a length/magnitude of 1).

func (Vec2) Perpendicular

func (v1 Vec2) Perpendicular() Vec2

Perpendicular returns the perpendicular vector.

func (Vec2) Reflect

func (v Vec2) Reflect(normal Vec2) Vec2

Reflect reflects the vector at the given normal.

func (Vec2) Rotate

func (v1 Vec2) Rotate(angle float64) Vec2

Rotate rotates the vector by the given angle (in degrees).

func (Vec2) RotateAroundPoint

func (v Vec2) RotateAroundPoint(p Vec2, angle float64) Vec2

RotateAroundPoint rotates the vector around the given point.

func (*Vec2) SetLength

func (v *Vec2) SetLength(length float64) Vec2

SetLength sets the length of the vector.

func (*Vec2) SetXY

func (v1 *Vec2) SetXY(x, y float64)

SetXY sets the x and y values of the vector.

func (Vec2) Sub

func (v1 Vec2) Sub(v2 Vec2) Vec2

Sub subtracts v2 from the current vector and returns the result.

type Vec3

type Vec3 struct {
	X float64
	Y float64
	Z float64
}

Vec3 represents a three dimensional vector.

func Add3

func Add3(v1, v2 Vec3) Vec3

Add3 adds two Vec3 vectors and returns the result.

func Cross3

func Cross3(v1, v2 Vec3) Vec3

Cross3 returns the crossproduct of two vectors.

func Cross3XYZ

func Cross3XYZ(v1x, v1y, v1z, v2x, v2y, v2z float64) Vec3

Cross3XYZ returns the cross product of two sets of coordinates.

func NewVec3

func NewVec3(x, y, z float64) Vec3

NewVec3 returns a new vector3.

func Sub3

func Sub3(v1, v2 Vec3) Vec3

Sub3 subtracts v2 from v1 and returns the result.

func (Vec3) Add

func (v1 Vec3) Add(v2 Vec3) Vec3

Add adds the current vector with v2 and returns the result.

func (*Vec3) AddToThis

func (v1 *Vec3) AddToThis(v2 Vec3)

AddToThis adds v2 to the current vector.

func (Vec3) Cross

func (v1 Vec3) Cross(v2 Vec3) Vec3

Cross returns the crossproduct of the current and the supplied vector.

func (Vec3) Dot

func (v1 Vec3) Dot(v2 Vec3) float64

Dot returns the dotproduct of the current and the supplied vector.

func (Vec3) Len

func (v1 Vec3) Len() float64

Len returns the vector's length (or magnitude).

NOTE: This is equivalent to the square root of the sum of the squares of all elements. E.G. for a Vec2 it's math.Hypot(v[0], v[1]).

func (Vec3) Mul

func (v1 Vec3) Mul(c float64) Vec3

Mul multiplies the current vector with c and returns the result.

func (Vec3) Normalize

func (v1 Vec3) Normalize() Vec3

Normalize returns the normalized vector (with a length/magnitude of 1).

func (Vec3) Rotate

func (v1 Vec3) Rotate(axis Vec3, angle float64) Vec3

Rotate rotates the vector around the supplied axis by the supplied angle.

func (Vec3) Sub

func (v1 Vec3) Sub(v2 Vec3) Vec3

Sub subtracts v2 from the current vector and returns the result.

Jump to

Keyboard shortcuts

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