vector

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

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 4 Imported by: 0

README

Vector

A 3D and 2D Vector Library, In Golang

go get git.red-green.com/david/vector

Quickstart

package main

import (
    "fmt"
    vec "git.red-green.com/david/vector"
)

func main() {
    // 3D
    var v vec.Vec = vec.NewVec3()
    fmt.Printf("Hello v, %s\r\n", v)
    // Hello v, (0, 0, 0)

    // 2D
    var v2 vec.Vec = vec.NewVec2(2, -2)
    fmt.Printf("Oh, Hi v2, %s\r\n", v2)
    // Oh, Hi v2, (2, -2)

    // Or auto detect
    // 3D
    var v3 vec.Vec = vec.NewVec()
    fmt.Printf("v3 = %s\r\n", v3)
    // v3 = (0, 0, 0)
    // 2D
    var v4 vec.Vec = vec.NewVec(2, -2)
    fmt.Printf("v4 = %s\r\n", v4)
    // v4 = (2, -2)
}

Library Info

Types
  • vector.Vec interface/generic of *vector.Vec2 or *vector.Vec3
  • vector.Vec2 struct { X int64, Y int64 }
  • vector.Vec3 struct { X int64, Y int64, Z int64 }

Many methods will return the generic vector.Vec instead of the direct Vec2 or Vec3 to satisfy the Vec interface (Use Is2D or Is3D to determine Vec2 or Vec3 and convert to *Vec2 or *Vec3 from there, i.e. var v1 *vec.Vec2 = v.(*vec.Vec2) or var v1 *vec.Vec3 = v.(*vec.Vec3))

Constructor (Generic/Auto)
  • vector.NewVec(...int64) vector.Vec
    • 0 to 1 values will result in nil (Use vector.NewVec2 or vector.NewVec3 to obtain a zeroed Vec)
    • 2 values will result in a *vector.Vec2 with X and Y
    • 3+ values will result in a *vector.Vec3 with X, Y and Z
    • 4 or more values are not used
  • vector.VecFrom(vector.Vec) vector.Vec
    • A nil will result in nil (vector.Vec2From or vector.Vec3From will make zeroed copies instead)
    • A *vector.Vec2 will result in a *vector.Vec2 with X and Y copied
    • A *vector.Vec3 will result in a *vector.Vec3 with X, Y and Z copied
  • vector.VecString(string) (Vec, error)
    • Given a string of a vector (You can use Vec2.String() or Vec3.String() to make this quickly)
    • string requirements:
      • Must start with a ( and end with a ) (missing parentheses)
      • Must contain from 1 to 2 , (invalid comma count: expected 1-2 got #)
        • 1 comma for a 2D vector.Vec2 (X, Y)
        • 2 commas for a 3D vector.Vec3 (X, Y, Z)
      • vector.Vec2String(string) (Vec, error) will expect the string to make a valid Vec2 rather than a Vec3 (A Vec3 will be converted)
      • vector.Vec3String(string) (Vec, error) will expect the string to make a valid Vec3 rather than a Vec2 (A Vec2 will be converted)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingParentheses error = errors.New("missing parentheses")
	ErrInvalidCommaCount  error = errors.New("invalid comma count")
)

Functions

This section is empty.

Types

type Vec

type Vec interface {
	X(...int64) int64
	Y(...int64) int64
	Z(...int64) int64

	Is2D() bool
	Is3D() bool

	Clone() Vec
	CloneDown() Vec

	IsZero() bool
	Zero() Vec

	Abs() Vec
	Negate() Vec

	IsPositive() bool
	IsNegative() bool

	String() string

	Set(...int64) Vec
	Move(...int64) Vec

	SetTo(Vec) Vec
	MoveBy(Vec) Vec

	Eq(...int64) bool
	EqTo(Vec) bool

	Less(...int64) bool
	Greater(...int64) bool
	LessThan(Vec) bool
	GreaterThan(Vec) bool

	Distance(...int64) int64
	DistanceTo(Vec) int64

	Within(Vec, Vec) bool
}

func NewVec

func NewVec(axis ...int64) Vec

func NewVec2

func NewVec2(axis ...int64) Vec

func NewVec3

func NewVec3(axis ...int64) Vec

func Vec2From

func Vec2From(o Vec) Vec

func Vec2String

func Vec2String(s string) (Vec, error)

func Vec3From

func Vec3From(o Vec) Vec

func Vec3String

func Vec3String(s string) (Vec, error)

func VecFrom

func VecFrom(o Vec) Vec

func VecString

func VecString(s string) (Vec, error)

type Vec2

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

A Point or Velocity in Space

func (*Vec2) Abs

func (v *Vec2) Abs() Vec

func (*Vec2) Clone

func (v *Vec2) Clone() Vec

func (*Vec2) CloneDown

func (v *Vec2) CloneDown() Vec

func (*Vec2) Distance

func (v *Vec2) Distance(axis ...int64) int64

func (*Vec2) DistanceTo

func (v *Vec2) DistanceTo(o Vec) int64

func (*Vec2) Eq

func (v *Vec2) Eq(axis ...int64) bool

func (*Vec2) EqTo

func (v *Vec2) EqTo(o Vec) bool

func (*Vec2) Greater

func (v *Vec2) Greater(axis ...int64) bool

func (*Vec2) GreaterThan

func (v *Vec2) GreaterThan(o Vec) bool

func (*Vec2) Is2D

func (v *Vec2) Is2D() bool

func (*Vec2) Is3D

func (v *Vec2) Is3D() bool

func (*Vec2) IsNegative

func (v *Vec2) IsNegative() bool

func (*Vec2) IsPositive

func (v *Vec2) IsPositive() bool

func (*Vec2) IsZero

func (v *Vec2) IsZero() bool

func (*Vec2) Less

func (v *Vec2) Less(axis ...int64) bool

func (*Vec2) LessThan

func (v *Vec2) LessThan(o Vec) bool

func (*Vec2) Move

func (v *Vec2) Move(axis ...int64) Vec

func (*Vec2) MoveBy

func (v *Vec2) MoveBy(o Vec) Vec

func (*Vec2) Negate

func (v *Vec2) Negate() Vec

func (*Vec2) Set

func (v *Vec2) Set(axis ...int64) Vec

func (*Vec2) SetTo

func (v *Vec2) SetTo(o Vec) Vec

func (*Vec2) String

func (v *Vec2) String() string

func (*Vec2) Within

func (v *Vec2) Within(corner1, corner2 Vec) bool

func (*Vec2) X

func (v *Vec2) X(x ...int64) int64

func (*Vec2) Y

func (v *Vec2) Y(y ...int64) int64

func (*Vec2) Z

func (v *Vec2) Z(z ...int64) int64

func (*Vec2) Zero

func (v *Vec2) Zero() Vec

type Vec3

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

A Point or Velocity in Space

func (*Vec3) Abs

func (v *Vec3) Abs() Vec

func (*Vec3) Clone

func (v *Vec3) Clone() Vec

func (*Vec3) CloneDown

func (v *Vec3) CloneDown() Vec

func (*Vec3) Distance

func (v *Vec3) Distance(axis ...int64) int64

func (*Vec3) DistanceTo

func (v *Vec3) DistanceTo(o Vec) int64

func (*Vec3) Eq

func (v *Vec3) Eq(axis ...int64) bool

func (*Vec3) EqTo

func (v *Vec3) EqTo(o Vec) bool

func (*Vec3) Greater

func (v *Vec3) Greater(axis ...int64) bool

func (*Vec3) GreaterThan

func (v *Vec3) GreaterThan(o Vec) bool

func (*Vec3) Is2D

func (v *Vec3) Is2D() bool

func (*Vec3) Is3D

func (v *Vec3) Is3D() bool

func (*Vec3) IsNegative

func (v *Vec3) IsNegative() bool

func (*Vec3) IsPositive

func (v *Vec3) IsPositive() bool

func (*Vec3) IsZero

func (v *Vec3) IsZero() bool

func (*Vec3) Less

func (v *Vec3) Less(axis ...int64) bool

func (*Vec3) LessThan

func (v *Vec3) LessThan(o Vec) bool

func (*Vec3) Move

func (v *Vec3) Move(axis ...int64) Vec

func (*Vec3) MoveBy

func (v *Vec3) MoveBy(o Vec) Vec

func (*Vec3) Negate

func (v *Vec3) Negate() Vec

func (*Vec3) Set

func (v *Vec3) Set(axis ...int64) Vec

func (*Vec3) SetTo

func (v *Vec3) SetTo(o Vec) Vec

func (*Vec3) String

func (v *Vec3) String() string

func (*Vec3) Within

func (v *Vec3) Within(corner1, corner2 Vec) bool

func (*Vec3) X

func (v *Vec3) X(x ...int64) int64

func (*Vec3) Y

func (v *Vec3) Y(y ...int64) int64

func (*Vec3) Z

func (v *Vec3) Z(z ...int64) int64

func (*Vec3) Zero

func (v *Vec3) Zero() Vec

Jump to

Keyboard shortcuts

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