Documentation
¶
Overview ¶
Package vec2d offers a two-dimensional vector implementation for Go.
Index ¶
- func GetAngleBetween(first, second *Vector) float64
- func GetDistance(first, second *Vector) float64
- type Vector
- func Collect(first, second *Vector) *Vector
- func CollectToFloat64(vector *Vector, value float64) *Vector
- func Div(first, second *Vector) *Vector
- func DivToFloat64(vector *Vector, value float64) *Vector
- func Mul(first, second *Vector) *Vector
- func MulToFloat64(vector *Vector, value float64) *Vector
- func New(x, y float64) *Vector
- func Rotated(vector *Vector, angle_degrees float64) *Vector
- func Sub(first, second *Vector) *Vector
- func SubToFloat64(vector *Vector, value float64) *Vector
- func (v *Vector) Angle() float64
- func (v *Vector) Collect(other *Vector)
- func (v *Vector) CollectToFloat64(value float64)
- func (v *Vector) Div(other *Vector)
- func (v *Vector) DivToFloat64(value float64)
- func (v *Vector) IsEqual(other *Vector) bool
- func (v *Vector) Length() float64
- func (v *Vector) Mul(other *Vector)
- func (v *Vector) MulToFloat64(value float64)
- func (v *Vector) Rotate(angle_degrees float64)
- func (v *Vector) SetAngle(angle_degrees float64)
- func (v *Vector) SetLength(value float64)
- func (v *Vector) Sub(other *Vector)
- func (v *Vector) SubToFloat64(value float64)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetAngleBetween ¶
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 ¶
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 ¶
Collect creates new Vector and sets its X and Y to the sum of these in first and second
func CollectToFloat64 ¶
CollectToFloat64 returns new Vector adding given value to first's X and Y
func DivToFloat64 ¶
DivToFloat64 returns a new Vector by dividing first's X and Y by given value
func MulToFloat64 ¶
MulToFloat64 returns a new Vector using multiplication of first's X and Y by given value
func SubToFloat64 ¶
SubToFloat64 returns a new Vector, taking given value from first's X and Y
func (*Vector) Collect ¶
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 ¶
CollectToFloat64 changes Vector's X and Y by collecting them with value
func (*Vector) DivToFloat64 ¶
DivToFloat64 changes Vector's X and Y by dividing them with value
func (*Vector) MulToFloat64 ¶
MulToFloat64 changes Vector's X and Y by multiplying them with value
func (*Vector) Rotate ¶
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) SetLength ¶
SetLength changes Vector's length, which obviously changes the values of Vector.X and Vector.Y
func (*Vector) SubToFloat64 ¶
SubToFloat64 changes Vector's X and Y by substracting them with value