coord

package
v0.0.0-...-d322e89 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2018 License: BSD-2-Clause Imports: 2 Imported by: 24

Documentation

Overview

Package coord provides vector types for various coordinates systems.

Floating-point Coordinates

Three structs with float32 fields give access to geometric transformations, and other useful mathematical operations (see subpackages plane and space):

// 2D cartesian coordinates
var a = XY{1.0, 2.0}

// 3D cartesian coordinates (or 2D projective coordinates)
var b = XYZ{1.0, 2.0, 3.0}

// 3D projective coordinates
var c = XYZW{1.0, 2.0, 3.0, 4.0}

Integer Coordinates

Two structs with int16 fields are used both for on-screen coordinates and in-game grids:

// 2D cartesian grid coordinates (e.g. pixel coordinates)
var a = CR{1, 2}

// CRD for 3D cartesian grid coordinates (e.g. voxel coordinates)
var b = CRD{1, 2, 3}

Other Coordinate systems

Various structs for hexagonal and triangular grids:

// Hexagonal grid coordinates
var a = QR{1, 2}

// Axial (hex) coordinates
var b = AL{1.0, 2.0}

// triangular grid coordinates
var c = QRT{1, 2, 0}

And finally structs to manipulate angles:

// Polar coordinates
var a = RA{1.0, 3.14}

// Cylindrical coordinates
var b = RAZ{1.0, 3.14, 2.0}

// Spherical coordinates
var c = RAS{1.0, 3.14, 6.28}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CR

type CR struct {
	C, R int16
}

CR represents 2D integer coordinates. C stands for "column" and corresponds to the x-coordinate, while R stands for "row" and corresponds to the y-coordinate.

func CRof

func CRof(v Coordinates) CR

CRof returns an integer vector corresponding to the first two coordinates of v.

func (CR) CRD

func (a CR) CRD(d int16) CRD

CRD returns the integer coordinates of the vector in 3D space, with d as third coordinate.

func (CR) Cartesian

func (a CR) Cartesian() (x, y, z float32)

Cartesian returns the coordinates of the vector in 3D space. C and R are casted to float32, and the third coordinate is always 0.

func (CR) Col

func (a CR) Col() CR

Col returns the vector projected on the C axis (i.e. with R nulled).

func (CR) FlipC

func (a CR) FlipC() CR

FlipC returns the vector with the sign of C flipped.

func (CR) FlipR

func (a CR) FlipR() CR

FlipR returns the vector with the sign of R flipped.

func (CR) Minus

func (a CR) Minus(b CR) CR

Minus returns the difference with another vector.

func (CR) MinusS

func (a CR) MinusS(s int16) CR

MinusS returns the difference with the vector (s, s).

func (CR) Mod

func (a CR) Mod(s int16) CR

Mod returns the remainder (modulus) of the division by a scalar (which must be non-zero).

func (CR) ModCR

func (a CR) ModCR(b CR) CR

ModCR returns the remainder (modulus) of the component-wise division by another vector (of which both C and R must be non-zero).

func (CR) Null

func (a CR) Null() bool

Null returns true if both coordinates are null.

func (CR) Opposite

func (a CR) Opposite() CR

Opposite returns the opposite of the vector.

func (CR) Perp

func (a CR) Perp() CR

Perp returns the vector rotated by 90 in counter-clockwise direction.

func (CR) Plus

func (a CR) Plus(b CR) CR

Plus returns the sum with another vector.

func (CR) PlusS

func (a CR) PlusS(s int16) CR

PlusS returns the sum with the vector (s, s).

func (CR) RC

func (a CR) RC() CR

RC returns the vector with coordinates C and R swapped.

func (CR) Row

func (a CR) Row() CR

Row returns the vector projected on the R axis (i.e. with C nulled).

func (CR) Slash

func (a CR) Slash(s int16) CR

Slash returns the integer quotient of the division by a scalar (which must be non-zero).

func (CR) SlashCR

func (a CR) SlashCR(b CR) CR

SlashCR returns the integer quotients of the component-wise division by another vector (of which both C and R must be non-zero).

func (CR) Times

func (a CR) Times(s int16) CR

Times returns the product with a scalar.

func (CR) TimesCR

func (a CR) TimesCR(b CR) CR

TimesCR returns the component-wise product with another vector.

func (CR) XY

func (a CR) XY() XY

XY returns the floating point coordinates of the vector.

func (CR) XYZ

func (a CR) XYZ(z float32) XYZ

XYZ returns the floating-point coordinates in 3D space, with the third dimension set to z.

func (CR) XYZW

func (a CR) XYZW(z, w float32) XYZW

XYZW returns the floating-point coordinates in 3D projective space, with the third and fourth dimensions set to z and w.

type CRD

type CRD struct {
	C, R, D int16
}

CRD represents 3D integer coordinates. C stands for "column" and corresponds to the x-coordinate, R stands for "row" and corresponds to the y-coordinate, and D stands for "depth" and corresponds to the z-coordinate.

func CRDof

func CRDof(v Coordinates) CRD

CRDof returns the integer coordinates corresponding to v.

func (CRD) Cartesian

func (a CRD) Cartesian() (x, y, z float32)

Cartesian returns the coordinates of the vector in 3D space. C,R and D are casted to float32.

func (CRD) Col

func (a CRD) Col() CRD

Col returns the vector projected on the C axis (i.e. with R and D nulled).

func (CRD) Depth

func (a CRD) Depth() CRD

Depth returns the vector projected on the D axis (i.e. with C and D nulled).

func (CRD) FlipC

func (a CRD) FlipC() CRD

FlipC returns the vector with the sign of C flipped.

func (CRD) FlipD

func (a CRD) FlipD() CRD

FlipD returns the vector with the sign of D flipped.

func (CRD) FlipR

func (a CRD) FlipR() CRD

FlipR returns the vector with the sign of R flipped.

func (CRD) Minus

func (a CRD) Minus(b CRD) CRD

Minus returns the difference with another vector.

func (CRD) Minuss

func (a CRD) Minuss(s int16) CRD

Minuss returns the difference with the vector (s, s).

func (CRD) Mod

func (a CRD) Mod(s int16) CRD

Mod returns the remainder (modulus) of the division by a scalar (which must be non-zero).

func (CRD) Modcr

func (a CRD) Modcr(b CRD) CRD

Modcr returns the remainder (modulus) of the component-wise division by another vector (of which both C and R must be non-zero).

func (CRD) Opposite

func (a CRD) Opposite() CRD

Opposite returns the opposite of the vector.

func (CRD) Plus

func (a CRD) Plus(b CRD) CRD

Plus returns the sum with another vector.

func (CRD) Pluss

func (a CRD) Pluss(s int16) CRD

Pluss returns the sum with the vector (s, s, s).

func (CRD) Row

func (a CRD) Row() CRD

Row returns the vector projected on the R axis (i.e. with C and D nulled).

func (CRD) Slash

func (a CRD) Slash(s int16) CRD

Slash returns the integer quotient of the division by a scalar (which must be non-zero).

func (CRD) Slashcr

func (a CRD) Slashcr(b CRD) CRD

Slashcr returns the integer quotients of the component-wise division by another vector (of which both C and R must be non-zero).

func (CRD) Times

func (a CRD) Times(s int16) CRD

Times returns the product with a scalar.

func (CRD) Timescr

func (a CRD) Timescr(b CRD) CRD

Timescr returns the component-wise product with another vector.

func (CRD) XY

func (a CRD) XY() XY

XY returns the 3D floating-point vector corresponding to the first two dimensions of the vector.

func (CRD) XYZ

func (a CRD) XYZ() XYZ

XYZ returns the floating-point version of the vector.

func (CRD) XYZW

func (a CRD) XYZW(w float32) XYZW

XYZW returns the floating-point vector in 3D projective space, with the fourth dimensions set to w.

type Coordinates

type Coordinates interface {
	// Cartesian returns the cartesian coordinates of the vector.
	Cartesian() (x, y, z float32)
}

Coordinates represents any three-dimensional vector.

type RA

type RA struct {
	R float32 // Distance from origin (i.e. radius)
	A float32 // Angle (counter-clockwise from 3 b'clock)
}

RA represents polar coordinates.

Note: incomplete implementation.

func (RA) Cartesian

func (a RA) Cartesian() (x, y, z float32)

Cartesian returns the cartesian coordinates; z is alwyas 0.

func (RA) XY

func (a RA) XY() XY

XY returns the cartesian representation of the vector.

type RA64

type RA64 struct {
	D float64 // Distance from origin (i.e. radius)
	A float64 // Angle //TODO: what angle?
}

RA64 represents a two dimensional vector, defined by its polar coordinates.

func (RA64) Cartesian

func (a RA64) Cartesian() (x, y float32)

Cartesian returns the cartesian coordinates of the vector. This implements the Vector interface.

func (RA64) XY64

func (a RA64) XY64() XY64

XY64 returns the cartesian representation of the vector.

type RAB

type RAB struct {
	R float32 // Distance from origin (i.e. radius)
	A float32 // Angle (counter-clockwise from 3 b'clock)
	S float32 // Second angle
}

RAB represents a three dimensional vector, defined by its spherical coordinates.

Note: not yet implemented.

type RAZ

type RAZ struct {
	R float32 // Distance from origin (i.e. radius)
	A float32 // Angle (counter-clockwise from 3 b'clock)
	Z float32 // Depth
}

RAZ represents a three dimensional vector, defined by its cylindrical coordinates.

Note: not yet implemented.

type XY

type XY struct {
	X float32
	Y float32
}

XY represents 2D cartesian coordinates.

func XYof

func XYof(c Coordinates) XY

XYof converts the first two coordinates of c to XY.

func (XY) CR

func (a XY) CR() CR

CR return the integer coordinates of the vector.

func (XY) Cartesian

func (a XY) Cartesian() (x, y, z float32)

Cartesian returns the two coordinates; z is always 0.

func (XY) Distance

func (a XY) Distance(b XY) float32

Distance returns the distance with another vector.

func (XY) Distance2

func (a XY) Distance2(b XY) float32

Distance2 returns the square of the distance with another vector.

func (XY) Dot

func (a XY) Dot(b XY) float32

Dot returns the dot product with another vector.

func (XY) FlipX

func (a XY) FlipX() XY

FlipX returns the coordinates with the signe of X flipped.

func (XY) FlipY

func (a XY) FlipY() XY

FlipY returns the coordinates with the signe of Y flipped.

func (XY) IsAlmostEqual

func (a XY) IsAlmostEqual(b XY, ulps uint32) bool

IsAlmostEqual returns true if the difference between the two vectors is less than the specified ULPs (Unit in the Last Place).

Handle special cases: zero, infinites, denormals.

See also IsNearlyEqual and IsRoughlyEqual.

func (XY) IsNearlyEqual

func (a XY) IsNearlyEqual(b XY, epsilon float32) bool

IsNearlyEqual Returns true if the relative error between the two vectors is less than epsilon.

Handles special cases: zero, infinites, denormals.

See also IsAlmostEqual and IsRoughlyEqual.

func (XY) IsRoughlyEqual

func (a XY) IsRoughlyEqual(b XY, epsilon float32) bool

IsRoughlyEqual Returns true if the absolute error between the two vectors is less than epsilon.

See also IsNearlyEqual and IsAlmostEqual.

func (XY) Length

func (a XY) Length() float32

Length returns the euclidian length of the vector.

func (XY) Length2

func (a XY) Length2() float32

Length2 returns the square of the euclidian length of the vector.

func (XY) Minus

func (a XY) Minus(b XY) XY

Minus returns the difference with another vector.

func (XY) Minuss

func (a XY) Minuss(s float32) XY

Minuss returns the difference with a scalar.

func (XY) Mod

func (a XY) Mod(s float32) XY

Mod returns the remainder (modulus) of the division by a scalar (which must be non-zero).

func (XY) Modf

func (a XY) Modf() (intg, frac XY)

Modf returns the integer part and the fractional part of (each component of) the vector.

func (XY) Modxy

func (a XY) Modxy(b XY) XY

Modxy returns the remainders (modulus) of the component-wise division by another vector (of which both X and Y must be non-zero).

func (XY) Normalized

func (a XY) Normalized() XY

Normalized return the normalization of the vector (i.e. the vector divided by its length, which must be non-zero).

func (XY) Opposite

func (a XY) Opposite() XY

Opposite returns the opposite of the vector.

func (XY) PerpDot

func (a XY) PerpDot(b XY) float32

PerpDot returns the dot product with the perpendicular of v and another vector.

func (XY) Plus

func (a XY) Plus(b XY) XY

Plus returns the sum with another vector.

func (XY) Pluss

func (a XY) Pluss(s float32) XY

Pluss returns the sum with a scalar.

func (XY) RoundCR

func (a XY) RoundCR() CR

RoundCR return the rounded interger coordinates of the vector.

func (XY) Slash

func (a XY) Slash(s float32) XY

Slash returns the division by a scalar (which must be non-zero).

func (XY) Slashxy

func (a XY) Slashxy(b XY) XY

Slashxy returns the component-wise division by another vector (of which both X and Y must be non-zero).

func (XY) Times

func (a XY) Times(s float32) XY

Times returns the product with a scalar.

func (XY) Timesxy

func (a XY) Timesxy(b XY) XY

Timesxy returns the component-wise product with another vector.

func (XY) XY64

func (a XY) XY64() XY64

XY64 returns the 64-bit float representation of a.

func (XY) XYZ

func (a XY) XYZ(z float32) XYZ

XYZ returns the coordinates {X, Y, z}.

type XY64

type XY64 struct {
	X float64
	Y float64
}

XY64 represents 64-bit 2D cartesian coordinates.

func (XY64) Cartesian

func (a XY64) Cartesian() (x, y, z float32)

Cartesian returns the cartesian coordinates of the vector.

This function implements the Vector interface.

func (XY64) Distance

func (a XY64) Distance(b XY64) float64

Distance returns the distance with another vector.

func (XY64) Distance2

func (a XY64) Distance2(b XY64) float64

Distance2 returns the square of the distance with another vector.

func (XY64) Dot

func (a XY64) Dot(b XY64) float64

Dot returns the dot product with another vector.

func (XY64) Length

func (a XY64) Length() float64

Length returns the euclidian length of the vector.

func (XY64) Length2

func (a XY64) Length2() float64

Length2 returns the square of the euclidian length of the vector.

func (XY64) Minus

func (a XY64) Minus(b XY64) XY64

Minus returns the difference with another vector.

func (XY64) Minuss

func (a XY64) Minuss(s float64) XY64

Minuss returns the difference with a scalar.

func (XY64) Mod

func (a XY64) Mod(s float64) XY64

Mod returns the remainder (modulus) of the division by a scalar (which must be non-zero).

func (XY64) Modf

func (a XY64) Modf() (intg, frac XY64)

Modf returns the integer part and the fractional part of (each component of) the vector.

func (XY64) Modxy

func (a XY64) Modxy(b XY64) XY64

Modxy returns the remainders (modulus) of the component-wise division by another vector (of which both X and Y must be non-zero).

func (XY64) Normalized

func (a XY64) Normalized() XY64

Normalized return the normalization of the vector (i.e. the vector divided by its length, which must be non-zero).

func (XY64) Opposite

func (a XY64) Opposite() XY64

Opposite returns the opposite of the vector.

func (XY64) PerpDot

func (a XY64) PerpDot(b XY64) float64

PerpDot returns the dot product with the perpendicular of a and another vector.

func (XY64) Plus

func (a XY64) Plus(b XY64) XY64

Plus returns the sum with another vector.

func (XY64) Pluss

func (a XY64) Pluss(s float64) XY64

Pluss returns the sum with a scalar.

func (XY64) Slash

func (a XY64) Slash(s float64) XY64

Slash returns the division by a scalar (which must be non-zero).

func (XY64) Slashxy

func (a XY64) Slashxy(b XY64) XY64

Slashxy returns the component-wise division by another vector (of which both X and Y must be non-zero).

func (XY64) Times

func (a XY64) Times(s float64) XY64

Times returns the product with a scalar.

func (XY64) Timesxy

func (a XY64) Timesxy(b XY64) XY64

Timesxy returns the component-wise product with another vector.

func (XY64) XY

func (a XY64) XY() XY

XY returns the 32-bit float version of a.

func (XY64) XYZ64

func (a XY64) XYZ64() XYZ64

XYZ64 returns the homogenous coordinates of the vector, with Z set to 1.

type XYZ

type XYZ struct {
	X float32
	Y float32
	Z float32
}

XYZ represents either 3D cartesian coordinates, or 2D projective (homogeneous) coordinates.

func (XYZ) Cartesian

func (a XYZ) Cartesian() (x, y, z float32)

Cartesian returns the three coordinates.

func (XYZ) Cross

func (a XYZ) Cross(b XYZ) XYZ

Cross returns the cross product with another vector.

func (XYZ) Distance

func (a XYZ) Distance(b XYZ) float32

Distance returns the distance with another vector.

func (XYZ) Distance2

func (a XYZ) Distance2(b XYZ) float32

Distance2 returns the square of the distance with another vector.

func (XYZ) Dot

func (a XYZ) Dot(b XYZ) float32

Dot returns the dot product with another vector.

func (XYZ) IsAlmostEqual

func (a XYZ) IsAlmostEqual(b XYZ, ulps uint32) bool

IsAlmostEqual returns true if the difference between the two vectors is less than the specified ULPs (Unit in the Last Place).

Handle special cases: zero, infinites, denormals.

See also IsNearlyEqual and IsRoughlyEqual.

func (XYZ) IsNearlyEqual

func (a XYZ) IsNearlyEqual(b XYZ, epsilon float32) bool

IsNearlyEqual Returns true if the relative error between the two vectors is less than epsilon.

Handles special cases: zero, infinites, denormals.

See also IsAlmostEqual and IsRoughlyEqual.

func (XYZ) IsRoughlyEqual

func (a XYZ) IsRoughlyEqual(b XYZ, epsilon float32) bool

IsRoughlyEqual Returns true if the absolute error between the two vectors is less than epsilon.

See also IsNearlyEqual and IsAlmostEqual.

func (XYZ) Length

func (a XYZ) Length() float32

Length returns the euclidian length of the vector.

func (XYZ) Length2

func (a XYZ) Length2() float32

Length2 returns the square of the euclidian length of the vector.

func (XYZ) Minus

func (a XYZ) Minus(b XYZ) XYZ

Minus returns the difference with another vector.

func (XYZ) Mod

func (a XYZ) Mod(s float32) XYZ

Mod returns the remainder (modulus) of the division by a scalar (which must be non-zero).

func (XYZ) ModCW

func (a XYZ) ModCW(b XYZ) XYZ

ModCW returns the remainders (modulus) of the component-wise division by another vector (of which X, Y and Z must be non-zero).

func (XYZ) Modf

func (a XYZ) Modf() (intg, frac XYZ)

Modf returns the integer part and the fractional part of (each component of) the vector.

func (XYZ) Normalized

func (a XYZ) Normalized() XYZ

Normalized return the normalization of the vector (i.e. the vector divided by its length, which must be non-zero).

func (XYZ) Opposite

func (a XYZ) Opposite() XYZ

Opposite returns the opposite of the vector.

func (XYZ) Plus

func (a XYZ) Plus(b XYZ) XYZ

Plus returns the sum with another vector.

func (XYZ) Slash

func (a XYZ) Slash(s float32) XYZ

Slash returns the division by a scalar (which must be non-zero).

func (XYZ) SlashCW

func (a XYZ) SlashCW(b XYZ) XYZ

SlashCW returns the component-wise division by another vector (of which X, Y and Z must be non-zero).

func (XYZ) Times

func (a XYZ) Times(s float32) XYZ

Times returns the product with a scalar.

func (XYZ) TimesCW

func (a XYZ) TimesCW(b XYZ) XYZ

TimesCW returns the component-wise product with another vector.

func (XYZ) XY

func (a XYZ) XY() XY

XY returns the planar coordinates {X, Y}.

func (XYZ) XYZW

func (a XYZ) XYZW(w float32) XYZW

XYZW returns the homogenous coordinates of the vector, with W set to w.

func (XYZ) XYproj

func (a XYZ) XYproj() XY

XYproj returns the cartesian representation of the vector (i.e. the perspective divide of the homogeneous coordinates). Z must be non-zero.

func (XYZ) XZ

func (a XYZ) XZ() XY

XZ returns the planar coordinates {X, Z}.

func (XYZ) YX

func (a XYZ) YX() XY

YX returns the planar coordinates {Y, X}.

func (XYZ) YZ

func (a XYZ) YZ() XY

YZ returns the planar coordinates {Y, Z}.

func (XYZ) ZX

func (a XYZ) ZX() XY

ZX returns the planar coordinates {Z, X}.

func (XYZ) ZY

func (a XYZ) ZY() XY

ZY returns the planar coordinates {Z, Y}.

type XYZ64

type XYZ64 struct {
	X float64
	Y float64
	Z float64
}

XYZ64 represents a two-dimensional vector, defined by its homogeneous coordinates.

func (XYZ64) Cartesian

func (a XYZ64) Cartesian() (x, y float32)

Cartesian returns the cartesian coordinates of the vector (i.e. the perspective divide of the homogeneous coordinates). Z must be non-zero.

type XYZW

type XYZW struct {
	X float32
	Y float32
	Z float32
	W float32
}

XYZW represents 3D projective (homogeneous) coordinates.

func (XYZW) Cartesian

func (a XYZW) Cartesian() (x, y, z float32)

Cartesian returns the cartesian coordinates of the vector (i.e. the perspective divide of the homogeneous coordinates). W must be non-zero.

func (XYZW) XYZ

func (a XYZW) XYZ() XYZ

XYZ returns the cartesian representation of the vector (i.e. the perspective divide of the homogeneous coordinates). W must be non-zero.

type XYZW64

type XYZW64 struct {
	X float64
	Y float64
	Z float64
	W float64
}

XYZW64 represents a two-dimensional vector, defined by its homogeneous coordinates.

func (XYZW64) Cartesian

func (a XYZW64) Cartesian() (x, y, z float32)

Cartesian returns the cartesian coordinates of the vector (i.e. the perspective divide of the homogeneous coordinates).

Jump to

Keyboard shortcuts

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