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 ¶
- type Position
- type Vector
- func (v *Vector) Abs() *Vector
- func (v *Vector) Add(o *Vector) *Vector
- func (v *Vector) AddScalar(s float64) *Vector
- func (v *Vector) Angle(o *Vector) float64
- func (v *Vector) Clamp(a, b *Vector) *Vector
- func (v *Vector) Clone() *Vector
- func (v *Vector) CompareApprox(o *Vector) (x, y int)
- func (v *Vector) Copy(o *Vector) *Vector
- func (v *Vector) Cross(o *Vector) float64
- func (v *Vector) DirectionTo(target Vector) int
- func (v *Vector) Distance(o *Vector) float64
- func (v *Vector) Divide(o *Vector) *Vector
- func (v *Vector) DivideScalar(s float64) *Vector
- func (v *Vector) Dot(o *Vector) float64
- func (v *Vector) Equals(o *Vector) bool
- func (v *Vector) EqualsApprox(o *Vector) bool
- func (v *Vector) Floor() *Vector
- func (v *Vector) IsZero() bool
- func (v *Vector) Length() float64
- func (v *Vector) Lerp(o *Vector, interp float64) *Vector
- func (v *Vector) Multiply(o *Vector) *Vector
- func (v *Vector) Negate() *Vector
- func (v *Vector) NinetyAnti() *Vector
- func (v *Vector) NinetyClock() *Vector
- func (v *Vector) Normalize() *Vector
- func (v *Vector) Reflect(normal *Vector) *Vector
- func (v *Vector) ReflectSurface(surface *Vector) *Vector
- func (v *Vector) Rotate(angle float64) *Vector
- func (v *Vector) Scale(s float64) *Vector
- func (v *Vector) Set(x, y float64) *Vector
- func (v *Vector) SetLength(length float64) *Vector
- func (v *Vector) SignedAngle(o *Vector) float64
- func (v Vector) String() string
- func (v *Vector) Subtract(o *Vector) *Vector
- func (v *Vector) X() float64
- func (v *Vector) Y() float64
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 ¶
NewPosition returns a Position struct with the given sub tile coordinates where 1 = 1 sub tile, with a fractional offset.
func NewPositionTile ¶
NewPositionTile returns a Position struct with the given tile coordinates where 1 = 1 tile, with a fractional offset.
func (*Position) RenderOffset ¶
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 ¶
Set sets this position to the given sub tile coordinates where 1 = 1 sub tile, with a fractional offset.
func (*Position) SubTileOffset ¶
SubTileOffset is the offset from the current map tile in sub tiles.
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 (*Vector) Angle ¶
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 ¶
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) CompareApprox ¶
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) Cross ¶
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 ¶
DirectionTo returns the entity direction from this vector to the given vector.
func (*Vector) DivideScalar ¶
DivideScalar divides both values of this vector by the given value.
func (*Vector) Equals ¶
Equals returns true if the float64 values of this vector are exactly equal to the given Vector.
func (*Vector) EqualsApprox ¶
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) Lerp ¶
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) NinetyAnti ¶
NinetyAnti rotates this vector by 90 degrees anti-clockwise.
func (*Vector) NinetyClock ¶
NinetyClock rotates this vector by 90 degrees clockwise.
func (*Vector) Normalize ¶
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 ¶
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 ¶
ReflectSurface does the same thing as Reflect, except the given vector describes the surface line, not it's normal.
func (*Vector) Rotate ¶
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) SetLength ¶
SetLength sets the length of this Vector without changing the direction. The length will be exact within d2math.Epsilon.
func (*Vector) SignedAngle ¶
SignedAngle computes the signed (clockwise) angle in radians from this vector to the given vector.