Skip to content

janczer/vector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status codecov

Vector

Library for work with vectors.

Methods you can use:

Simple usage

First get the package:

$ go get github.com/janczer/vector

Now you can add this library to import and use it:

package main

import (
	"fmt"
	"github.com/janczer/vector"
)

func main() {
	v := vector.New([]float64{1, 2, 3})
	v2 := vector.New([]float64{1, 2, 4})
	fmt.Println(v.Print()) //Vector: 1, 2, 3
	fmt.Println(v.Eq(v2))  //false
}

Eq

Method return true if vectors are equal:

	v := vector.New([]float64{1, 2, 3})
	v2 := vector.New([]float64{1, 2, 3})
	fmt.Println(v.Eq(v2))  //true

Add

This method add coordinates of two vectors, and return new Vector:

	v := vector.New([]float64{1, 2, 4})
	v2 := vector.New([]float64{1, 1, 3})
 	v3 := v.Add(v2) //1+1, 2+1, 4+3

Sub

Method subtract coordinates of two vectors, and return new Vector:

	v := vector.New([]float64{1, 2, 4})
	v2 := vector.New([]float64{1, 1, 3})
	v3 := v.Sub(v2) //1-1, 2-1, 4-3

Dot

Method multiply coordinates of two vectors, and return new Vector:

	v := vector.New([]float64{1, 2, 4})
	v2 := vector.New([]float64{1, 1, 3})
	v3 := v.Dot(v2) //1, 2, 12

Scalar

Method multiply each coordinate by number:

	v := vector.New([]float64{1, 2, 4})
	v2 := v.Scalar(3) //3, 6, 12

Magnitude

Method return magnitude of vector:

	v := vector.New([]float64{1, 2, 4})
	m := v.Magnitude() //4,5825..

Normalize

Method return new unit vector:

	v := vector.New([]float64{1, 2, 4})
	m := v.Normalize() // 1/4,5825, 2/4.5825, 4/4.5825

Angle

Method return angle between two vectors:

	v := vector.New([]float64{3, 0})
	v2 := vector.New([]float64{0, 3})
	a := v.Angle(v2, true) // 45

Paraller

Method return true if two vectors are paraller:

	v := vector.New([]float64{3, 0})
	v2 := vector.New([]float64{0, 3})
	a := v.Paraller(v2) // false

Orthogonal

Method return true if two vectors are orthogonal:

	v := vector.New([]float64{3, 0})
	v2 := vector.New([]float64{0, 3})
	a := v.Orthogonal(v2) // true

Releases

No releases published

Packages

No packages published

Languages