rename Vec2 -> Vec
This commit is contained in:
parent
0974069426
commit
966f31a68e
38
vec.go
38
vec.go
|
@ -6,10 +6,10 @@ import (
|
||||||
"math/cmplx"
|
"math/cmplx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Vec2 is a 2d vector type. It is unusually implemented as complex128 for convenience. Since Go
|
// Vec is a 2d vector type. It is unusually implemented as complex128 for convenience. Since Go does
|
||||||
// does not allow operator overloading, implementing vector as a struct leads to a bunch of methods
|
// not allow operator overloading, implementing vector as a struct leads to a bunch of methods for
|
||||||
// for addition, subtraction and multiplication of vectors. With complex128, much of this
|
// addition, subtraction and multiplication of vectors. With complex128, much of this functionality
|
||||||
// functionality is given through operators.
|
// is given through operators.
|
||||||
//
|
//
|
||||||
// Create vectors with the V constructor:
|
// Create vectors with the V constructor:
|
||||||
//
|
//
|
||||||
|
@ -30,59 +30,59 @@ import (
|
||||||
// fmt.Println("this won't happend")
|
// fmt.Println("this won't happend")
|
||||||
// }
|
// }
|
||||||
// x := u.Unit().Dot(v.Unit())
|
// x := u.Unit().Dot(v.Unit())
|
||||||
type Vec2 complex128
|
type Vec complex128
|
||||||
|
|
||||||
// V returns a new 2d vector with the given coordinates.
|
// V returns a new 2d vector with the given coordinates.
|
||||||
func V(x, y float64) Vec2 {
|
func V(x, y float64) Vec {
|
||||||
return Vec2(complex(x, y))
|
return Vec(complex(x, y))
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns the string representation of a vector u as "Vec2(x, y)".
|
// String returns the string representation of a vector u as "Vec(x, y)".
|
||||||
func (u Vec2) String() string {
|
func (u Vec) String() string {
|
||||||
return fmt.Sprintf("Vec2(%v, %v)", u.X(), u.Y())
|
return fmt.Sprintf("Vec(%v, %v)", u.X(), u.Y())
|
||||||
}
|
}
|
||||||
|
|
||||||
// X returns the x coordinate of a vector u.
|
// X returns the x coordinate of a vector u.
|
||||||
func (u Vec2) X() float64 {
|
func (u Vec) X() float64 {
|
||||||
return real(u)
|
return real(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Y returns the y coordinate of a vector u.
|
// Y returns the y coordinate of a vector u.
|
||||||
func (u Vec2) Y() float64 {
|
func (u Vec) Y() float64 {
|
||||||
return imag(u)
|
return imag(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Len returns the length of a vector u.
|
// Len returns the length of a vector u.
|
||||||
func (u Vec2) Len() float64 {
|
func (u Vec) Len() float64 {
|
||||||
return cmplx.Abs(complex128(u))
|
return cmplx.Abs(complex128(u))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Angle returns the angle between a vector u and the x-axis. The result is in the range [-Pi, Pi].
|
// Angle returns the angle between a vector u and the x-axis. The result is in the range [-Pi, Pi].
|
||||||
func (u Vec2) Angle() float64 {
|
func (u Vec) Angle() float64 {
|
||||||
return cmplx.Phase(complex128(u))
|
return cmplx.Phase(complex128(u))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unit returns a vector of length 1 with the same angle as u.
|
// Unit returns a vector of length 1 with the same angle as u.
|
||||||
func (u Vec2) Unit() Vec2 {
|
func (u Vec) Unit() Vec {
|
||||||
return u / V(u.Len(), 0)
|
return u / V(u.Len(), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scaled returns a vector u multiplied by k.
|
// Scaled returns a vector u multiplied by k.
|
||||||
func (u Vec2) Scaled(k float64) Vec2 {
|
func (u Vec) Scaled(k float64) Vec {
|
||||||
return u * V(k, 0)
|
return u * V(k, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotated returns a vector u rotated by the given angle in radians.
|
// Rotated returns a vector u rotated by the given angle in radians.
|
||||||
func (u Vec2) Rotated(angle float64) Vec2 {
|
func (u Vec) Rotated(angle float64) Vec {
|
||||||
return u * V(math.Cos(angle), math.Sin(angle))
|
return u * V(math.Cos(angle), math.Sin(angle))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dot returns the dot product of vectors u and v.
|
// Dot returns the dot product of vectors u and v.
|
||||||
func (u Vec2) Dot(v Vec2) float64 {
|
func (u Vec) Dot(v Vec) float64 {
|
||||||
return u.X()*v.X() + u.Y()*v.Y()
|
return u.X()*v.X() + u.Y()*v.Y()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cross return the cross product of vectors u and v.
|
// Cross return the cross product of vectors u and v.
|
||||||
func (u Vec2) Cross(v Vec2) float64 {
|
func (u Vec) Cross(v Vec) float64 {
|
||||||
return u.X()*v.Y() - v.X()*u.Y()
|
return u.X()*v.Y() - v.X()*u.Y()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue