go-opengl-pixel/geometry.go

169 lines
4.3 KiB
Go
Raw Normal View History

2016-11-19 06:08:42 -06:00
package pixel
import (
"fmt"
"math"
"math/cmplx"
)
2017-01-25 11:55:17 -06:00
// Vec is a 2D vector type. It is unusually implemented as complex128 for convenience. Since
// Go does not allow operator overloading, implementing vector as a struct leads to a bunch of
// methods for addition, subtraction and multiplication of vectors. With complex128, much of
// this functionality is given through operators.
2016-11-19 06:08:42 -06:00
//
// Create vectors with the V constructor:
//
// u := pixel.V(1, 2)
// v := pixel.V(8, -3)
//
// Add and subtract them using the standard + and - operators:
//
// w := u + v
2016-11-19 07:13:36 -06:00
// fmt.Println(w) // Vec(9, -1)
// fmt.Println(u - v) // Vec(-7, 5)
2016-11-19 06:08:42 -06:00
//
// Additional standard vector operations can be obtained with methods:
//
2016-11-19 07:13:36 -06:00
// u := pixel.V(2, 3)
// v := pixel.V(8, 1)
2016-11-19 06:08:42 -06:00
// if u.X() < 0 {
// fmt.Println("this won't happen")
2016-11-19 06:08:42 -06:00
// }
// x := u.Unit().Dot(v.Unit())
2016-11-19 07:06:52 -06:00
type Vec complex128
2016-11-19 06:08:42 -06:00
// V returns a new 2d vector with the given coordinates.
2016-11-19 07:06:52 -06:00
func V(x, y float64) Vec {
return Vec(complex(x, y))
2016-11-19 06:08:42 -06:00
}
2017-01-25 11:55:17 -06:00
// String returns the string representation of the vector u.
2016-11-19 07:34:37 -06:00
//
// u := pixel.V(4.5, -1.3)
// u.String() // returns "Vec(4.5, -1.3)"
// fmt.Println(u) // Vec(4.5, -1.3)
2016-11-19 07:06:52 -06:00
func (u Vec) String() string {
return fmt.Sprintf("Vec(%v, %v)", u.X(), u.Y())
2016-11-19 06:08:42 -06:00
}
2017-01-25 11:55:17 -06:00
// X returns the x coordinate of the vector u.
2016-11-19 07:06:52 -06:00
func (u Vec) X() float64 {
2016-11-19 06:08:42 -06:00
return real(u)
}
2017-01-25 11:55:17 -06:00
// Y returns the y coordinate of the vector u.
2016-11-19 07:06:52 -06:00
func (u Vec) Y() float64 {
2016-11-19 06:08:42 -06:00
return imag(u)
}
2017-01-25 11:55:17 -06:00
// XY returns the components of the vector in two return values.
2016-11-30 18:01:36 -06:00
func (u Vec) XY() (x, y float64) {
return real(u), imag(u)
}
2017-01-25 11:55:17 -06:00
// Len returns the length of the vector u.
2016-11-19 07:06:52 -06:00
func (u Vec) Len() float64 {
2016-11-19 06:08:42 -06:00
return cmplx.Abs(complex128(u))
}
2017-01-25 11:55:17 -06:00
// Angle returns the angle between the vector u and the x-axis. The result is in the range [-Pi, Pi].
2016-11-19 07:06:52 -06:00
func (u Vec) Angle() float64 {
2016-11-19 06:08:42 -06:00
return cmplx.Phase(complex128(u))
}
// Unit returns a vector of length 1 with the same angle as u.
2016-11-19 07:06:52 -06:00
func (u Vec) Unit() Vec {
2016-11-19 06:08:42 -06:00
return u / V(u.Len(), 0)
}
2017-01-25 11:55:17 -06:00
// Scaled returns the vector u multiplied by c.
2016-11-19 07:21:43 -06:00
func (u Vec) Scaled(c float64) Vec {
return u * V(c, 0)
2016-11-19 06:08:42 -06:00
}
2017-01-25 11:55:17 -06:00
// Rotated returns the vector u rotated by the given angle in radians.
2016-11-19 07:06:52 -06:00
func (u Vec) Rotated(angle float64) Vec {
2016-11-20 12:21:20 -06:00
sin, cos := math.Sincos(angle)
return u * V(cos, sin)
2016-11-19 06:08:42 -06:00
}
// Dot returns the dot product of vectors u and v.
2016-11-19 07:06:52 -06:00
func (u Vec) Dot(v Vec) float64 {
2016-11-19 06:08:42 -06:00
return u.X()*v.X() + u.Y()*v.Y()
}
// Cross return the cross product of vectors u and v.
2016-11-19 07:06:52 -06:00
func (u Vec) Cross(v Vec) float64 {
2016-11-19 06:08:42 -06:00
return u.X()*v.Y() - v.X()*u.Y()
}
2016-12-05 16:19:31 -06:00
2017-03-01 16:12:48 -06:00
// Apply applies the function f to both x and y components of the vector u and returns the modified
// vector.
func (u Vec) Apply(f func(float64) float64) Vec {
return V(
f(u.X()),
f(u.Y()),
)
}
2017-01-25 11:55:17 -06:00
// Rect is a 2D rectangle aligned with the axes of the coordinate system. It has a position
// and a size.
2016-12-05 16:19:31 -06:00
//
// You can manipulate the position and the size using the usual vector operations.
type Rect struct {
Pos, Size Vec
}
2017-01-25 11:55:17 -06:00
// R returns a new Rect with given position (x, y) and size (w, h).
2016-12-05 16:19:31 -06:00
func R(x, y, w, h float64) Rect {
return Rect{
Pos: V(x, y),
Size: V(w, h),
}
}
2017-01-25 11:55:17 -06:00
// String returns the string representation of the rectangle.
2016-12-05 16:19:31 -06:00
//
// r := pixel.R(100, 50, 200, 300)
// r.String() // returns "Rect(100, 50, 200, 300)"
// fmt.Println(r) // Rect(100, 50, 200, 300)
func (r Rect) String() string {
return fmt.Sprintf("Rect(%v, %v, %v, %v)", r.X(), r.Y(), r.W(), r.H())
}
2017-01-25 11:55:17 -06:00
// X returns the x coordinate of the position of the rectangle.
2016-12-05 16:19:31 -06:00
func (r Rect) X() float64 {
return r.Pos.X()
}
2017-01-25 11:55:17 -06:00
// Y returns the y coordinate of the position of the rectangle
2016-12-05 16:19:31 -06:00
func (r Rect) Y() float64 {
return r.Pos.Y()
}
2017-01-25 11:55:17 -06:00
// W returns the width of the rectangle.
2016-12-05 16:19:31 -06:00
func (r Rect) W() float64 {
return r.Size.X()
}
2017-01-25 11:55:17 -06:00
// H returns the height of the rectangle.
2016-12-05 16:19:31 -06:00
func (r Rect) H() float64 {
return r.Size.Y()
}
2017-01-25 11:55:17 -06:00
// XYWH returns all of the four components of the rectangle in four return values.
2016-12-05 16:19:31 -06:00
func (r Rect) XYWH() (x, y, w, h float64) {
return r.X(), r.Y(), r.W(), r.H()
}
2017-01-25 11:55:17 -06:00
// Center returns the position of the center of the rectangle.
2016-12-05 16:19:31 -06:00
func (r Rect) Center() Vec {
return r.Pos + r.Size.Scaled(0.5)
}
2017-02-25 11:38:22 -06:00
// Contains checks whether a vector u is contained within this Rect (including it's borders).
func (r Rect) Contains(u Vec) bool {
min, max := r.Pos, r.Pos+r.Size
return min.X() <= u.X() && u.X() <= max.X() && min.Y() <= u.Y() && u.Y() <= max.Y()
}