d2vector

package
v0.0.0-...-7f92c57 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: GPL-3.0 Imports: 3 Imported by: 2

Documentation

Overview

Package d2vector provides an implementation of a 2D Euclidean vector using float64 to store the two values.

Vector uses d2math.Epsilon for approximate equality and comparison. Note: SetLength, Reflect, ReflectSurface and Rotate do not (per their unit tests) return exact values but ones within Epsilon range of the expected value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Position

type Position struct {
	Vector
}

Position is a vector in world space. The stored value is the sub tile position.

func NewPosition

func NewPosition(x, y float64) Position

NewPosition returns a Position struct with the given sub tile coordinates where 1 = 1 sub tile, with a fractional offset.

func NewPositionTile

func NewPositionTile(x, y float64) Position

NewPositionTile returns a Position struct with the given tile coordinates where 1 = 1 tile, with a fractional offset.

func (*Position) RenderOffset

func (p *Position) RenderOffset() *Vector

RenderOffset is the offset in sub tiles from the curren tile, + 1. This places the vector at the bottom vertex of an isometric diamond visually representing one sub tile. Sub tile indices increase to the lower right diagonal ('down') and to the lower left diagonal ('left') of the isometric grid. This renders the target one index above which visually is one tile below.

func (*Position) Set

func (p *Position) Set(x, y float64)

Set sets this position to the given sub tile coordinates where 1 = 1 sub tile, with a fractional offset.

func (*Position) SubTileOffset

func (p *Position) SubTileOffset() *Vector

SubTileOffset is the offset from the current map tile in sub tiles.

func (*Position) Tile

func (p *Position) Tile() *Vector

Tile is the position of the current map tile. It is the floor of World(), always a whole number.

func (*Position) World

func (p *Position) World() *Vector

World is the exact position where 1 = one map tile and 0.2 = one sub tile.

type Vector

type Vector struct {
	// contains filtered or unexported fields
}

Vector is an implementation of a Euclidean vector using float64 with common vector convenience methods.

func NewVector

func NewVector(x, y float64) *Vector

NewVector creates a new Vector with the given x and y values.

func VectorDown

func VectorDown() *Vector

VectorDown returns a new vector (0, -1)

func VectorLeft

func VectorLeft() *Vector

VectorLeft returns a new vector (-1, 0)

func VectorOne

func VectorOne() *Vector

VectorOne returns a new vector (1, 1)

func VectorRight

func VectorRight() *Vector

VectorRight returns a new vector (1, 0)

func VectorUp

func VectorUp() *Vector

VectorUp returns a new vector (0, 1)

func VectorZero

func VectorZero() *Vector

VectorZero returns a new vector (0, 0)

func (*Vector) Abs

func (v *Vector) Abs() *Vector

Abs sets the vector to it's absolute (positive) equivalent.

func (*Vector) Add

func (v *Vector) Add(o *Vector) *Vector

Add the given vector to this vector.

func (*Vector) AddScalar

func (v *Vector) AddScalar(s float64) *Vector

AddScalar the given value to both values of this vector.

func (*Vector) Angle

func (v *Vector) Angle(o *Vector) float64

Angle computes the unsigned angle in radians from this vector to the given vector. This angle will never exceed half a full circle. For angles describing a full circumference use SignedAngle.

func (*Vector) Clamp

func (v *Vector) Clamp(a, b *Vector) *Vector

Clamp limits the values of v to those of a and b. If the values of v are between those of a and b they will be unchanged.

func (*Vector) Clone

func (v *Vector) Clone() *Vector

Clone returns a new a copy of this Vector.

func (*Vector) CompareApprox

func (v *Vector) CompareApprox(o *Vector) (x, y int)

CompareApprox returns 2 ints describing the difference between the vectors. If the difference between either of the value pairs is smaller than d2math.Epsilon, they will be considered equal.

func (*Vector) Copy

func (v *Vector) Copy(o *Vector) *Vector

Copy sets this vector's values to those of the given vector.

func (*Vector) Cross

func (v *Vector) Cross(o *Vector) float64

Cross returns the cross product of this Vector and the given Vector. Note: Cross product is specific to 3D space. This a not cross product. It is the Z component of a 3D vector cross product calculation. The X and Y components use the value of z which doesn't exist in 2D. See: https://stackoverflow.com/questions/243945/calculating-a-2d-vectors-cross-product

The sign of Cross indicates whether the direction between the points described by vectors v and o around the origin (0,0) moves clockwise or anti-clockwise. The perspective is from the would-be position of positive Z and the direction is from v to o.

Negative = clockwise Positive = anti-clockwise 0 = vectors are identical.

func (*Vector) DirectionTo

func (v *Vector) DirectionTo(target Vector) int

DirectionTo returns the entity direction from this vector to the given vector.

func (*Vector) Distance

func (v *Vector) Distance(o *Vector) float64

Distance between this Vector's position and that of the given Vector.

func (*Vector) Divide

func (v *Vector) Divide(o *Vector) *Vector

Divide this vector by the given vector.

func (*Vector) DivideScalar

func (v *Vector) DivideScalar(s float64) *Vector

DivideScalar divides both values of this vector by the given value.

func (*Vector) Dot

func (v *Vector) Dot(o *Vector) float64

Dot returns the dot product of this Vector and the given Vector.

func (*Vector) Equals

func (v *Vector) Equals(o *Vector) bool

Equals returns true if the float64 values of this vector are exactly equal to the given Vector.

func (*Vector) EqualsApprox

func (v *Vector) EqualsApprox(o *Vector) bool

EqualsApprox returns true if the values of this Vector are approximately equal to those of the given Vector. If the difference between either of the value pairs is smaller than d2math.Epsilon, they will be considered equal.

func (*Vector) Floor

func (v *Vector) Floor() *Vector

Floor rounds the vector down to the nearest whole numbers.

func (*Vector) IsZero

func (v *Vector) IsZero() bool

IsZero returns true if this vector's values are both exactly zero.

func (*Vector) Length

func (v *Vector) Length() float64

Length (magnitude/quantity) of this Vector.

func (*Vector) Lerp

func (v *Vector) Lerp(o *Vector, interp float64) *Vector

Lerp sets this vector to the linear interpolation between this and the given vector. The interp argument determines the distance between the two vectors. An interp of 0 will return this vector and 1 will return the given vector.

func (*Vector) Multiply

func (v *Vector) Multiply(o *Vector) *Vector

Multiply this Vector by the given Vector.

func (*Vector) Negate

func (v *Vector) Negate() *Vector

Negate multiplies this vector by -1.

func (*Vector) NinetyAnti

func (v *Vector) NinetyAnti() *Vector

NinetyAnti rotates this vector by 90 degrees anti-clockwise.

func (*Vector) NinetyClock

func (v *Vector) NinetyClock() *Vector

NinetyClock rotates this vector by 90 degrees clockwise.

func (*Vector) Normalize

func (v *Vector) Normalize() *Vector

Normalize sets the vector length to 1 without changing the direction. The normalized vector may be scaled by the float64 return value to restore it's original length.

func (*Vector) Reflect

func (v *Vector) Reflect(normal *Vector) *Vector

Reflect sets this Vector to it's reflection off a line defined by the given normal. The result will be exact within d2math.Epsilon.

func (*Vector) ReflectSurface

func (v *Vector) ReflectSurface(surface *Vector) *Vector

ReflectSurface does the same thing as Reflect, except the given vector describes the surface line, not it's normal.

func (*Vector) Rotate

func (v *Vector) Rotate(angle float64) *Vector

Rotate moves the vector around it's origin clockwise, by the given angle in radians. The result will be exact within d2math.Epsilon. See d2math.EqualsApprox.

func (*Vector) Scale

func (v *Vector) Scale(s float64) *Vector

Scale multiplies both values of this vector by a single given value.

func (*Vector) Set

func (v *Vector) Set(x, y float64) *Vector

Set the vector values to the given float64 values.

func (*Vector) SetLength

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

SetLength sets the length of this Vector without changing the direction. The length will be exact within d2math.Epsilon.

func (*Vector) SignedAngle

func (v *Vector) SignedAngle(o *Vector) float64

SignedAngle computes the signed (clockwise) angle in radians from this vector to the given vector.

func (Vector) String

func (v Vector) String() string

func (*Vector) Subtract

func (v *Vector) Subtract(o *Vector) *Vector

Subtract the given vector from this vector.

func (*Vector) X

func (v *Vector) X() float64

X returns the x value of this vector.

func (*Vector) Y

func (v *Vector) Y() float64

Y returns the y value of this vector.

Jump to

Keyboard shortcuts

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