Skip to content

Commit

Permalink
3d vector package
Browse files Browse the repository at this point in the history
  • Loading branch information
vaibhav11s committed Jun 18, 2022
1 parent 457c240 commit 63159b9
Show file tree
Hide file tree
Showing 9 changed files with 1,636 additions and 297 deletions.
5 changes: 5 additions & 0 deletions vector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# vector

Package vector provides a simple 3D vector class.

For documentation, see [pkg.go.dev](https://pkg.go.dev/github.com/vaibhav11s/gopkgs/vector)
338 changes: 338 additions & 0 deletions vector/doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,338 @@
<!-- Code generated by gomarkdoc. DO NOT EDIT -->

# vector

```go
import "github.com/vaibhav11s/gopkgs/vector"
```

Package vector provides a simple 3D vector class.

## Index

- [type Vector](#type-vector)
- [func Add(v1, v2 *Vector) *Vector](#func-add)
- [func Copy(v *Vector) *Vector](#func-copy)
- [func Cross(v1, v2 *Vector) *Vector](#func-cross)
- [func FromAngles(thetha, phi float32, length ...float32) \*Vector](#func-fromangles)
- [func Lerp(v1, v2 *Vector, t float32) *Vector](#func-lerp)
- [func Lerp2(v1, v2 *Vector, n, i int) *Vector](#func-lerp2)
- [func New(x, y, z float32) \*Vector](#func-new)
- [func Random(length ...float32) \*Vector](#func-random)
- [func ReflectThroughPlane(v, normal *Vector) *Vector](#func-reflectthroughplane)
- [func RotateAlongAxis(v, axis *Vector, angle float32) *Vector](#func-rotatealongaxis)
- [func Sub(v1, v2 *Vector) *Vector](#func-sub)
- [func Unit(v *Vector) *Vector](#func-unit)
- [func Angle(v1, v2 \*Vector) float32](#func-angle)
- [func Dist(v1, v2 \*Vector) float32](#func-dist)
- [func Dot(v1, v2 \*Vector) float32](#func-dot)
- [func (v *Vector) Add(v2 *Vector) \*Vector](#func-vector-add)
- [func (v *Vector) Angle(v2 *Vector) float32](#func-vector-angle)
- [func (v1 *Vector) Assign(v2 *Vector) \*Vector](#func-vector-assign)
- [func (v *Vector) Component(axis *Vector) (parallel, perpendicular \*Vector)](#func-vector-component)
- [func (v *Vector) Copy() *Vector](#func-vector-copy)
- [func (v *Vector) Cross(v2 *Vector) \*Vector](#func-vector-cross)
- [func (v *Vector) Dist(v2 *Vector) float32](#func-vector-dist)
- [func (v *Vector) Dot(v2 *Vector) float32](#func-vector-dot)
- [func (v *Vector) Equal(v2 *Vector, tolerance ...float32) bool](#func-vector-equal)
- [func (v \*Vector) Heading() (theta, phi float32)](#func-vector-heading)
- [func (v \*Vector) Mag() float32](#func-vector-mag)
- [func (v \*Vector) MagSq() float32](#func-vector-magsq)
- [func (v *Vector) Mult(scalar float32) *Vector](#func-vector-mult)
- [func (v *Vector) Normalize() *Vector](#func-vector-normalize)
- [func (v *Vector) ReflectThroughPlane(normal *Vector) \*Vector](#func-vector-reflectthroughplane)
- [func (v *Vector) Resize(mag float32) *Vector](#func-vector-resize)
- [func (v *Vector) RotateAlongAxis(axis *Vector, angle float32) \*Vector](#func-vector-rotatealongaxis)
- [func (v *Vector) SetHeading(thetha, phi float32) *Vector](#func-vector-setheading)
- [func (v \*Vector) String() string](#func-vector-string)
- [func (v *Vector) Sub(v2 *Vector) \*Vector](#func-vector-sub)

## type Vector

```go
type Vector struct {
X, Y, Z float32
}
```

### func Add

```go
func Add(v1, v2 *Vector) *Vector
```

returns the sum of two vectors

### func Copy

```go
func Copy(v *Vector) *Vector
```

Gets a copy of the vector

### func Cross

```go
func Cross(v1, v2 *Vector) *Vector
```

Calculates the cross product of two vectors

### func FromAngles

```go
func FromAngles(thetha, phi float32, length ...float32) *Vector
```

Make a new 3D vector from a pair of azimuth and zenith angles\. https://en.wikipedia.org/wiki/Spherical_coordinate_system

### func Lerp

```go
func Lerp(v1, v2 *Vector, t float32) *Vector
```

Linear interpolate the vector to another vector

### func Lerp2

```go
func Lerp2(v1, v2 *Vector, n, i int) *Vector
```

Linear interpolate the vector to another vector\. i/n = t

### func New

```go
func New(x, y, z float32) *Vector
```

Creates a new 3D vector\. Three dimensional Euclidean vector\.

### func Random

```go
func Random(length ...float32) *Vector
```

Makes a random 3D vector of given lenght \(default 1\)

### func ReflectThroughPlane

```go
func ReflectThroughPlane(v, normal *Vector) *Vector
```

Gives the reflection of vector from the given plane\(normal vector\)

### func RotateAlongAxis

```go
func RotateAlongAxis(v, axis *Vector, angle float32) *Vector
```

Rotates the given vector around the axis by given angle

### func Sub

```go
func Sub(v1, v2 *Vector) *Vector
```

returns the difference of two vectors

### func Unit

```go
func Unit(v *Vector) *Vector
```

Gives a unit vector in dirction of the vector

### func Angle

```go
func Angle(v1, v2 *Vector) float32
```

Calculates and returns the angle between two vectors\. Returns NaN if any vector is a zero vector

### func Dist

```go
func Dist(v1, v2 *Vector) float32
```

Calculates the Euclidean distance between two points \(considering a point as a vector object\)

### func Dot

```go
func Dot(v1, v2 *Vector) float32
```

### func \(\*Vector\) Add

```go
func (v *Vector) Add(v2 *Vector) *Vector
```

add a vector to the current vector\. Modify \+ Returns self

### func \(\*Vector\) Angle

```go
func (v *Vector) Angle(v2 *Vector) float32
```

Calculates and returns the angle with another vector Returns NaN if any vector is a zero vector

### func \(\*Vector\) Assign

```go
func (v1 *Vector) Assign(v2 *Vector) *Vector
```

Assigns the values of given vector to the vector\. Similar to copy\, but no new vector is create

### func \(\*Vector\) Component

```go
func (v *Vector) Component(axis *Vector) (parallel, perpendicular *Vector)
```

Give the component of the given vector parallel and perpendicular to the axis

### func \(\*Vector\) Copy

```go
func (v *Vector) Copy() *Vector
```

Gets a copy of the vector

### func \(\*Vector\) Cross

```go
func (v *Vector) Cross(v2 *Vector) *Vector
```

Calculates the cross product with another vector

### func \(\*Vector\) Dist

```go
func (v *Vector) Dist(v2 *Vector) float32
```

Calculates the Euclidean distance between two points \(considering a point as a vector object\)

### func \(\*Vector\) Dot

```go
func (v *Vector) Dot(v2 *Vector) float32
```

Calculates the dot product with another vector

### func \(\*Vector\) Equal

```go
func (v *Vector) Equal(v2 *Vector, tolerance ...float32) bool
```

Checks whether two vectors are equal\. optional tolerence value can be passed as a parameter to check for equality within a tolerance\. abs\(v\.x \- v2\.x\) \< tolerance && abs\(v\.y \- v2\.y\) \< tolerance && abs\(v\.z \- v2\.z\) \< tolerance

### func \(\*Vector\) Heading

```go
func (v *Vector) Heading() (theta, phi float32)
```

Calculate the azimuth and zenith angles\. https://en.wikipedia.org/wiki/Spherical_coordinate_system

### func \(\*Vector\) Mag

```go
func (v *Vector) Mag() float32
```

Calculates the magnitude \(length\) of the vector and returns the result as a float this is simply the equation sqrt\(x\*x \+ y\*y \+ z\*z\)

### func \(\*Vector\) MagSq

```go
func (v *Vector) MagSq() float32
```

Calculates the squared magnitude of the vector and returns the result as a float this is simply the equation \(x\*x \+ y\*y \+ z\*z\)

### func \(\*Vector\) Mult

```go
func (v *Vector) Mult(scalar float32) *Vector
```

Multiplies the vector by a scalar\. Modify \+ Returns self

### func \(\*Vector\) Normalize

```go
func (v *Vector) Normalize() *Vector
```

Normalize the vector to length 1 \(make it a unit vector\)\. Modify \+ Returns self

### func \(\*Vector\) ReflectThroughPlane

```go
func (v *Vector) ReflectThroughPlane(normal *Vector) *Vector
```

Gives the reflection of vector from the given plane\(normal vector\)

### func \(\*Vector\) Resize

```go
func (v *Vector) Resize(mag float32) *Vector
```

Set the magnitude of the vector to the given value\. Modify \+ Returns self

### func \(\*Vector\) RotateAlongAxis

```go
func (v *Vector) RotateAlongAxis(axis *Vector, angle float32) *Vector
```

Rotates the given vector around the axis by given angle https://math.stackexchange.com/questions/511370/how-to-rotate-one-vector-about-another

### func \(\*Vector\) SetHeading

```go
func (v *Vector) SetHeading(thetha, phi float32) *Vector
```

Rotate the vector to a specific angle\. magnitude remains the same\. Modify \+ Returns self https://en.wikipedia.org/wiki/Spherical_coordinate_system

### func \(\*Vector\) String

```go
func (v *Vector) String() string
```

String representation of vector

### func \(\*Vector\) Sub

```go
func (v *Vector) Sub(v2 *Vector) *Vector
```

subtract a vector from the current vector\. Modify \+ Returns self

Calculates the dot product of two vectors

Generated by [gomarkdoc](https://github.com/princjef/gomarkdoc)
Loading

0 comments on commit 63159b9

Please sign in to comment.