vec2d

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2019 License: Apache-2.0 Imports: 1 Imported by: 0

README

Vec2d

Package vec2d offers a two-dimensional vector implementation for Go.

API reference is available on godoc.org.

Installation

go get github.com/vladimiroff/vec2d

TODO

  • Normalization
  • Projection
  • Interpolation

License

Copyright 2013 Kiril Vladimiroff

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Documentation

Overview

Package vec2d offers a two-dimensional vector implementation for Go.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAngleBetween

func GetAngleBetween(first, second *Vector) float64

GetAngleBetween returns the angle between two Vectors in float64

Example
package main

import (
	"fmt"

	"github.com/vladimiroff/vec2d"
)

func main() {
	vector := vec2d.New(2.0, 4.0)
	other := vec2d.New(5.0, 12.0)
	angle_between := vec2d.GetAngleBetween(vector, other)

	fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y)
	fmt.Printf("Vector{X: %v, Y: %v}\n", other.X, other.Y)
	fmt.Printf("Angle between: %v\n", angle_between)
}
Output:
Vector{X: 2, Y: 4}
Vector{X: 5, Y: 12}
Angle between: 3.945186229037563

func GetDistance

func GetDistance(first, second *Vector) float64

GetDistance returns the distance between two Vectors in float64

Example
package main

import (
	"fmt"

	"github.com/vladimiroff/vec2d"
)

func main() {
	vector := vec2d.New(2.0, 4.0)
	other := vec2d.New(5.0, 12.0)
	distance := vec2d.GetDistance(vector, other)

	fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y)
	fmt.Printf("Vector{X: %v, Y: %v}\n", other.X, other.Y)
	fmt.Printf("Distance between: %v\n", distance)
}
Output:
Vector{X: 2, Y: 4}
Vector{X: 5, Y: 12}
Distance between: 8.54400374531753

Types

type Vector

type Vector struct {
	X, Y float64
}

Vector type defines vector using exported float64 values: X and Y

Example (Create)

In this example we create new vector with X=2 and Y=4, then we get the length and angle of it.

package main

import (
	"fmt"

	"github.com/vladimiroff/vec2d"
)

func main() {
	vector := vec2d.New(2.0, 4.0)
	length := vector.Length()
	angle := vector.Angle()

	fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y)
	fmt.Printf("Length: %v\n", length)
	fmt.Printf("Angle: %v\n", angle)
}
Output:
Vector{X: 2, Y: 4}
Length: 4.47213595499958
Angle: 63.434948822922
Example (Edit)

We're able to edit our Vector by changing its X, Y, angle or length. Obviously, if we change X or Y, then the angle and length most definately will be changes, also.

package main

import (
	"fmt"

	"github.com/vladimiroff/vec2d"
)

func main() {
	vector := vec2d.New(2.0, 4.0)
	vector.X = 4
	length := vector.Length()
	angle := vector.Angle()

	fmt.Printf("Vector{X: %v, Y: %v}\n", vector.X, vector.Y)
	fmt.Printf("Length: %v\n", length)
	fmt.Printf("Angle: %v\n", angle)
}
Output:
Vector{X: 4, Y: 4}
Length: 5.656854249492381
Angle: 45

func Collect

func Collect(first, second *Vector) *Vector

Collect creates new Vector and sets its X and Y to the sum of these in first and second

func CollectToFloat64

func CollectToFloat64(vector *Vector, value float64) *Vector

CollectToFloat64 returns new Vector adding given value to first's X and Y

func Div

func Div(first, second *Vector) *Vector

Div returns a new Vector by dividing first and second

func DivToFloat64

func DivToFloat64(vector *Vector, value float64) *Vector

DivToFloat64 returns a new Vector by dividing first's X and Y by given value

func Mul

func Mul(first, second *Vector) *Vector

Mul returns a new Vector by multiplication of first and second

func MulToFloat64

func MulToFloat64(vector *Vector, value float64) *Vector

MulToFloat64 returns a new Vector using multiplication of first's X and Y by given value

func New

func New(x, y float64) *Vector

New returns a new vector

func Rotated

func Rotated(vector *Vector, angle_degrees float64) *Vector

Rotated rotates a Vector by given angle degrees in float64 and returns a new one

func Sub

func Sub(first, second *Vector) *Vector

Sub creates a new Vector and sets its X and Y to the difference first-second

func SubToFloat64

func SubToFloat64(vector *Vector, value float64) *Vector

SubToFloat64 returns a new Vector, taking given value from first's X and Y

func (*Vector) Angle

func (v *Vector) Angle() float64

Angle returns the Vector's angle in float64

func (*Vector) Collect

func (v *Vector) Collect(other *Vector)

Collect changes Vector's X and Y by collecting them with other's

Example

Note that operations like Collect, Sub, Mul and Div changes the Vector if they are called as its methods. If you want to create a new instance you have to use the functions instead

package main

import (
	"fmt"

	"github.com/vladimiroff/vec2d"
)

func main() {
	vector := vec2d.New(2.0, 4.0)
	other := vec2d.New(3.0, 5.0)
	vector.Collect(other)
	result := vec2d.Collect(vector, other)

	fmt.Printf("Vector.Collect(Other) has changed Vector's values to X: %v, Y: %v\n", vector.X, vector.Y)
	fmt.Printf("vec2d.Collect(Vector, Other) gave a new vector with X: %v, Y: %v\n", result.X, result.Y)
}
Output:
Vector.Collect(Other) has changed Vector's values to X: 5, Y: 9
vec2d.Collect(Vector, Other) gave a new vector with X: 8, Y: 14

func (*Vector) CollectToFloat64

func (v *Vector) CollectToFloat64(value float64)

CollectToFloat64 changes Vector's X and Y by collecting them with value

func (*Vector) Div

func (v *Vector) Div(other *Vector)

Div changes Vector's X and Y by dividing them with other's

func (*Vector) DivToFloat64

func (v *Vector) DivToFloat64(value float64)

DivToFloat64 changes Vector's X and Y by dividing them with value

func (*Vector) IsEqual

func (v *Vector) IsEqual(other *Vector) bool

IsEqual compares а Vector with another and returns true if they're equal

func (*Vector) Length

func (v *Vector) Length() float64

Length returns... well the Vector's length

func (*Vector) Mul

func (v *Vector) Mul(other *Vector)

Mul changes Vector's X and Y by multiplying them with other's

func (*Vector) MulToFloat64

func (v *Vector) MulToFloat64(value float64)

MulToFloat64 changes Vector's X and Y by multiplying them with value

func (*Vector) Rotate

func (v *Vector) Rotate(angle_degrees float64)

Rotate Vector by given angle degrees in float64

Example

In this example we rotate our vector by 2 degrees, and this will change X and Y of the current instance, without creating a new one.

Note: If you don't want to change the current instance take a look at Vector.Rotated

package main

import (
	"fmt"

	"github.com/vladimiroff/vec2d"
)

func main() {
	vector := vec2d.New(2.0, 4.0)
	vector.Rotate(2.0)

	fmt.Printf("Vector.Rotate(2.0) changed X to %v and Y to %v\n", vector.X, vector.Y)
}
Output:
Vector.Rotate(2.0) changed X to 1.8591836672281876 and Y to 4.067362301481385

func (*Vector) SetAngle

func (v *Vector) SetAngle(angle_degrees float64)

SetAngle changes Vector's angle using vector rotation

func (*Vector) SetLength

func (v *Vector) SetLength(value float64)

SetLength changes Vector's length, which obviously changes the values of Vector.X and Vector.Y

func (*Vector) Sub

func (v *Vector) Sub(other *Vector)

Sub changes Vector's X and Y by substracting them with other's

func (*Vector) SubToFloat64

func (v *Vector) SubToFloat64(value float64)

SubToFloat64 changes Vector's X and Y by substracting them with value

Jump to

Keyboard shortcuts

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