v2d

package module
v0.0.0-...-ddf5eb1 Latest Latest
Warning

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

Go to latest
Published: May 10, 2020 License: MIT Imports: 1 Imported by: 3

README

v2d

2D vector math library in golang.

Test

There exist several ones, but could not find one fitting my requirements of:

  • MIT license
  • All operations on values, not pointers
  • float32
  • 2D optimized

Inspired by https://github.com/joonazan/vec2 which however does not specify any license.

Documentation

Overview

Pacakge v2d implements 2D float32 vector math

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rect

type Rect struct {
	Min, Max Vec
}

A Rect represents a bounding rectangle in the (x, y) plane that is always aligned with the x and y axis.

func R

func R(c1, c2 Vec) (tr Rect)

R is a convenience method to create a rectangle

func (Rect) C0

func (r Rect) C0() Vec

C0 returns the first corner of the rectangle.

func (Rect) C1

func (r Rect) C1() Vec

C1 returns the second corner of the rectangle.

func (Rect) C2

func (r Rect) C2() Vec

C2 returns the third corner of the rectangle.

func (Rect) C3

func (r Rect) C3() Vec

C3 returns the forth corner of the rectangle.

func (Rect) Eq

func (r Rect) Eq(q Rect, tol float32) bool

Eq returns true if r and q are equal withing tol.

func (Rect) H

func (r Rect) H() float32

H returns the height (y dimension) of the rectangle.

func (Rect) W

func (r Rect) W() float32

W returns the width (x dimension) of the rectangle.

type Transform

type Transform interface {
	// TransVec applies this Transform to a vector
	TransVec(Vec) Vec
	// TransRect applies this transform to a rectangle. Note that since rectangles always are axis
	// aligned the transformed rectangle will fully enclose the original rectangle.
	TransRect(Rect) Rect
}

type TransformRotateMove

type TransformRotateMove struct {
	Sin, Cos float32
	Around   Vec
	Offset   Vec
}

func RotateMove

func RotateMove(theta float32, around, offset Vec) (t TransformRotateMove)

Rotate move creates a Transform that rotates with an angle theta about around and subsequently adds offset.

func (TransformRotateMove) TransRect

func (t TransformRotateMove) TransRect(r Rect) (tr Rect)

TransRect applies this transform to a rectangle. Note that since rectangles always are axis aligned the transformed rectangle will fully enclose the original rectangle.

func (TransformRotateMove) TransVec

func (t TransformRotateMove) TransVec(v Vec) (w Vec)

TransVec applies this Transform to a vector

type Vec

type Vec struct {
	X, Y float32
}

Vec is a 2D vector (x, y)

func CrossZ

func CrossZ(z float32, v Vec) Vec

CrossZ returns the cross product of (0, 0, z) and (x, y, 0).

func V

func V(x, y float32) Vec

V is a convenience function to create a Vec.

func (Vec) Add

func (v Vec) Add(w Vec) Vec

Add adds the two vectors.

func (Vec) Angle

func (v Vec) Angle() float32

Angle returns the angle of the vector (from x axis)

func (Vec) Cross

func (v Vec) Cross(w Vec) float32

Cross assumes that v, w are (x,y,0) vectors and returns the z component of 3D cross product only.

func (Vec) CrossZ

func (v Vec) CrossZ(z float32) Vec

CrossZ returns the cross product of (x, y, 0) and (0, 0, z).

func (Vec) Dot

func (v Vec) Dot(w Vec) float32

Dot takes the dot-product of v and w.

func (Vec) Eq

func (v Vec) Eq(w Vec, tol float32) bool

Eq returns true if v and w are equal withing tol.

func (Vec) IsZero

func (v Vec) IsZero() bool

IsZero checks if the vector is (0,0)

func (Vec) Length

func (v Vec) Length() float32

Length returns the length of the vector.

func (Vec) LengthSquared

func (v Vec) LengthSquared() float32

LengthSquared returns the length of the vector squared.

func (Vec) Neg

func (v Vec) Neg() Vec

Neg returns -v.

func (Vec) Orth

func (v Vec) Orth() (w Vec)

Orth returns a vector w of same length as v that is orthogonal to v such that (v, w) are "right handed".

func (Vec) Scale

func (v Vec) Scale(s float32) Vec

Scale scales the vector with s.

func (Vec) Split

func (v Vec) Split() (float32, float32)

Split is a convenience function to get the components of a Vec.

func (Vec) Sub

func (v Vec) Sub(w Vec) Vec

Sub subtracts w from v.

func (Vec) Unit

func (v Vec) Unit() Vec

Unit returns a unit vector in the same direction.

Directories

Path Synopsis
Pacakge v2d implements 2D float64 vector math
Pacakge v2d implements 2D float64 vector math

Jump to

Keyboard shortcuts

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