go-opengl-pixel/geometry.go

345 lines
9.4 KiB
Go
Raw Normal View History

2016-11-19 06:08:42 -06:00
package pixel
import (
"fmt"
"math"
)
2017-05-21 12:25:06 -05:00
// Vec is a 2D vector type with X and Y coordinates.
2016-11-19 06:08:42 -06:00
//
// Create vectors with the V constructor:
//
// u := pixel.V(1, 2)
// v := pixel.V(8, -3)
//
2017-05-21 12:25:06 -05:00
// Use various methods to manipulate them:
2016-11-19 06:08:42 -06:00
//
2017-05-21 12:25:06 -05:00
// w := u.Add(v)
// fmt.Println(w) // Vec(9, -1)
// fmt.Println(u.Sub(v)) // Vec(-7, 5)
// u = pixel.V(2, 3)
// v = pixel.V(8, 1)
// if u.X < 0 {
// fmt.Println("this won't happen")
2016-11-19 06:08:42 -06:00
// }
// x := u.Unit().Dot(v.Unit())
2017-05-21 12:25:06 -05:00
type Vec struct {
X, Y float64
2016-11-19 06:08:42 -06:00
}
2017-05-21 12:25:06 -05:00
// ZV is a zero vector.
var ZV = Vec{0, 0}
2017-03-14 07:27:18 -05:00
2017-05-21 12:25:06 -05:00
// V returns a new 2D vector with the given coordinates.
func V(x, y float64) Vec {
return Vec{x, y}
2017-03-14 07:27:18 -05: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 {
2017-05-21 12:25:06 -05:00
return fmt.Sprintf("Vec(%v, %v)", u.X, u.Y)
2016-11-19 06:08:42 -06:00
}
2017-05-21 12:25:06 -05:00
// XY returns the components of the vector in two return values.
func (u Vec) XY() (x, y float64) {
return u.X, u.Y
2016-11-19 06:08:42 -06:00
}
2017-05-21 12:25:06 -05:00
// Add returns the sum of vectors u and v.
func (u Vec) Add(v Vec) Vec {
return Vec{
u.X + v.X,
u.Y + v.Y,
}
2016-11-19 06:08:42 -06:00
}
2017-05-21 12:25:06 -05:00
// Sub returns the difference betweeen vectors u and v.
func (u Vec) Sub(v Vec) Vec {
return Vec{
u.X - v.X,
u.Y - v.Y,
}
}
// Scaled returns the vector u multiplied by c.
func (u Vec) Scaled(c float64) Vec {
return Vec{u.X * c, u.Y * c}
}
// ScaledXY returns the vector u multiplied by the vector v component-wise.
func (u Vec) ScaledXY(v Vec) Vec {
return Vec{u.X * v.X, u.Y * v.Y}
2016-11-30 18:01:36 -06:00
}
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 {
2017-05-21 12:25:06 -05:00
return math.Hypot(u.X, u.Y)
2016-11-19 06:08:42 -06:00
}
2017-04-13 08:18:13 -05:00
// Angle returns the angle between the vector u and the x-axis. The result is in range [-Pi, Pi].
2016-11-19 07:06:52 -06:00
func (u Vec) Angle() float64 {
2017-05-21 12:25:06 -05:00
return math.Atan2(u.Y, u.X)
2016-11-19 06:08:42 -06:00
}
2017-03-15 13:40:39 -05:00
// Unit returns a vector of length 1 facing the direction of u (has the same angle).
2016-11-19 07:06:52 -06:00
func (u Vec) Unit() Vec {
2017-05-21 12:25:06 -05:00
if u.X == 0 && u.Y == 0 {
return Vec{1, 0}
2017-03-23 13:15:06 -05:00
}
2017-05-21 12:25:06 -05:00
return u.Scaled(1 / u.Len())
}
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)
2017-05-21 12:25:06 -05:00
return Vec{
u.X*cos - u.Y*sin,
u.X*sin + u.Y*cos,
}
}
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 {
2017-05-21 12:25:06 -05:00
return u.X*v.X + u.Y*v.Y
2016-11-19 06:08:42 -06:00
}
// 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 {
2017-05-21 12:25:06 -05:00
return u.X*v.Y - v.X*u.Y
2016-11-19 06:08:42 -06:00
}
2016-12-05 16:19:31 -06:00
2017-03-01 16:13:14 -06:00
// Map applies the function f to both x and y components of the vector u and returns the modified
2017-03-01 16:12:48 -06:00
// vector.
2017-03-15 13:40:39 -05:00
//
// u := pixel.V(10.5, -1.5)
// v := u.Map(math.Floor) // v is Vec(10, -2), both components of u floored
2017-03-01 16:13:14 -06:00
func (u Vec) Map(f func(float64) float64) Vec {
2017-05-21 12:25:06 -05:00
return Vec{
f(u.X),
f(u.Y),
}
2017-03-01 16:12:48 -06:00
}
2017-03-05 04:47:18 -06:00
// Lerp returns a linear interpolation between vectors a and b.
//
2017-03-15 13:40:39 -05:00
// This function basically returns a point along the line between a and b and t chooses which one.
2017-03-05 04:47:18 -06:00
// If t is 0, then a will be returned, if t is 1, b will be returned. Anything between 0 and 1 will
// return the appropriate point between a and b and so on.
func Lerp(a, b Vec, t float64) Vec {
2017-05-21 12:25:06 -05:00
return a.Scaled(1 - t).Add(b.Scaled(t))
2017-03-05 04:47:18 -06:00
}
// Rect is a 2D rectangle aligned with the axes of the coordinate system. It is defined by two
// points, Min and Max.
2016-12-05 16:19:31 -06:00
//
// The invariant should hold, that Max's components are greater or equal than Min's components
// respectively.
2016-12-05 16:19:31 -06:00
type Rect struct {
Min, Max Vec
2016-12-05 16:19:31 -06:00
}
// R returns a new Rect with given the Min and Max coordinates.
2017-05-21 12:38:21 -05:00
//
// Note that the returned rectangle is not automatically normalized.
func R(minX, minY, maxX, maxY float64) Rect {
2016-12-05 16:19:31 -06:00
return Rect{
Min: V(minX, minY),
Max: V(maxX, maxY),
}
}
2017-04-04 07:02:39 -05:00
// String returns the string representation of the Rect.
//
// 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 {
2017-05-21 12:25:06 -05:00
return fmt.Sprintf("Rect(%v, %v, %v, %v)", r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)
2017-04-04 07:02:39 -05:00
}
// Norm returns the Rect in normal form, such that Max is component-wise greater or equal than Min.
func (r Rect) Norm() Rect {
return Rect{
2017-05-21 12:25:06 -05:00
Min: Vec{
math.Min(r.Min.X, r.Max.X),
math.Min(r.Min.Y, r.Max.Y),
},
Max: Vec{
math.Max(r.Min.X, r.Max.X),
math.Max(r.Min.Y, r.Max.Y),
},
2016-12-05 16:19:31 -06:00
}
}
2017-03-15 13:40:39 -05:00
// W returns the width of the Rect.
2016-12-05 16:19:31 -06:00
func (r Rect) W() float64 {
2017-05-21 12:25:06 -05:00
return r.Max.X - r.Min.X
2016-12-05 16:19:31 -06:00
}
2017-03-15 13:40:39 -05:00
// H returns the height of the Rect.
2016-12-05 16:19:31 -06:00
func (r Rect) H() float64 {
2017-05-21 12:25:06 -05:00
return r.Max.Y - r.Min.Y
2016-12-05 16:19:31 -06:00
}
2017-03-15 13:40:39 -05:00
// Size returns the vector of width and height of the Rect.
2017-03-14 12:25:39 -05:00
func (r Rect) Size() Vec {
return V(r.W(), r.H())
}
2017-03-15 13:40:39 -05:00
// Center returns the position of the center of the Rect.
2016-12-05 16:19:31 -06:00
func (r Rect) Center() Vec {
2017-05-21 12:25:06 -05:00
return Lerp(r.Min, r.Max, 0.5)
}
// Moved returns the Rect moved (both Min and Max) by the given vector delta.
func (r Rect) Moved(delta Vec) Rect {
return Rect{
2017-05-21 12:25:06 -05:00
Min: r.Min.Add(delta),
Max: r.Max.Add(delta),
}
}
2017-03-15 13:40:39 -05:00
// Resized returns the Rect resized to the given size while keeping the position of the given
// anchor.
2017-03-14 07:20:26 -05:00
//
// r.Resized(r.Min, size) // resizes while keeping the position of the lower-left corner
// r.Resized(r.Max, size) // same with the top-right corner
// r.Resized(r.Center(), size) // resizes around the center
2017-03-14 07:20:26 -05:00
//
// This function does not make sense for resizing a rectangle of zero area and will panic. Use
// ResizedMin in the case of zero area.
func (r Rect) Resized(anchor, size Vec) Rect {
if r.W()*r.H() == 0 {
panic(fmt.Errorf("(%T).Resize: zero area", r))
}
2017-05-21 12:25:06 -05:00
fraction := Vec{size.X / r.W(), size.Y / r.H()}
return Rect{
2017-05-21 12:25:06 -05:00
Min: anchor.Add(r.Min.Sub(anchor)).ScaledXY(fraction),
Max: anchor.Add(r.Max.Sub(anchor)).ScaledXY(fraction),
}
}
// ResizedMin returns the Rect resized to the given size while keeping the position of the Rect's
// Min.
//
// Sizes of zero area are safe here.
func (r Rect) ResizedMin(size Vec) Rect {
return Rect{
Min: r.Min,
2017-05-21 12:25:06 -05:00
Max: r.Min.Add(size),
}
2016-12-05 16:19:31 -06:00
}
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 {
2017-05-21 12:25:06 -05:00
return r.Min.X <= u.X && u.X <= r.Max.X && r.Min.Y <= u.Y && u.Y <= r.Max.Y
2017-02-25 11:38:22 -06:00
}
2017-05-05 08:43:24 -05:00
// Union returns a minimal Rect which covers both r and s. Rects r and s should be normalized.
func (r Rect) Union(s Rect) Rect {
return R(
2017-05-21 12:25:06 -05:00
math.Min(r.Min.X, s.Min.X),
math.Min(r.Min.Y, s.Min.Y),
math.Max(r.Max.X, s.Max.X),
math.Max(r.Max.Y, s.Max.Y),
2017-05-05 08:43:24 -05:00
)
}
// Matrix is a 3x2 affine matrix that can be used for all kinds of spatial transforms, such
// as movement, scaling and rotations.
//
// Matrix has a handful of useful methods, each of which adds a transformation to the matrix. For
// example:
//
2017-05-21 12:30:29 -05:00
// pixel.IM.Moved(pixel.V(100, 200)).Rotated(pixel.ZV, math.Pi/2)
//
2017-03-15 18:47:50 -05:00
// This code creates a Matrix that first moves everything by 100 units horizontally and 200 units
// vertically and then rotates everything by 90 degrees around the origin.
//
// Layout is:
// [0] [2] [4]
// [1] [3] [5]
2017-06-09 11:13:05 -05:00
// 0 0 1 (implicit row)
type Matrix [6]float64
2017-03-14 10:19:58 -05:00
// IM stands for identity matrix. Does nothing, no transformation.
var IM = Matrix{1, 0, 0, 1, 0, 0}
2017-04-04 07:08:37 -05:00
// String returns a string representation of the Matrix.
//
// m := pixel.IM
// fmt.Println(m) // Matrix(1 0 0 | 0 1 0)
2017-04-04 07:08:37 -05:00
func (m Matrix) String() string {
return fmt.Sprintf(
"Matrix(%v %v %v | %v %v %v)",
m[0], m[2], m[4],
m[1], m[3], m[5],
2017-04-04 07:08:37 -05:00
)
}
// Moved moves everything by the delta vector.
func (m Matrix) Moved(delta Vec) Matrix {
m[4], m[5] = m[4]+delta.X, m[5]+delta.Y
return m
}
// ScaledXY scales everything around a given point by the scale factor in each axis respectively.
func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix {
m[4], m[5] = m[4]-around.X, m[5]-around.Y
m[0], m[2], m[4] = m[0]*scale.X, m[2]*scale.X, m[4]*scale.X
m[1], m[3], m[5] = m[1]*scale.Y, m[3]*scale.Y, m[5]*scale.Y
m[4], m[5] = m[4]+around.X, m[5]+around.Y
return m
}
// Scaled scales everything around a given point by the scale factor.
func (m Matrix) Scaled(around Vec, scale float64) Matrix {
return m.ScaledXY(around, V(scale, scale))
}
// Rotated rotates everything around a given point by the given angle in radians.
func (m Matrix) Rotated(around Vec, angle float64) Matrix {
sint, cost := math.Sincos(angle)
m[4], m[5] = m[4]-around.X, m[5]-around.Y
m = m.Chained(Matrix{cost, sint, -sint, cost, 0, 0})
m[4], m[5] = m[4]+around.X, m[5]+around.Y
return m
}
2017-03-18 18:08:46 -05:00
// Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied
// after the transformations of this Matrix.
func (m Matrix) Chained(next Matrix) Matrix {
return Matrix{
m[0]*next[0] + m[2]*next[1],
m[1]*next[0] + m[3]*next[1],
m[0]*next[2] + m[2]*next[3],
m[1]*next[2] + m[3]*next[3],
m[0]*next[4] + m[2]*next[5] + m[4],
m[1]*next[4] + m[3]*next[5] + m[5],
}
2017-03-18 18:08:46 -05:00
}
// Project applies all transformations added to the Matrix to a vector u and returns the result.
2017-03-14 18:15:02 -05:00
//
// Time complexity is O(1).
func (m Matrix) Project(u Vec) Vec {
return Vec{X: m[0]*u.X + m[2]*u.Y + m[4], Y: m[1]*u.X + m[3]*u.Y + m[5]}
}
// Unproject does the inverse operation to Project.
2017-03-14 18:15:02 -05:00
//
2017-06-09 11:13:05 -05:00
// It turns out that multiplying a vector by the inverse matrix of m can be nearly-accomplished by
// subtracting the translate part of the matrix and multplying by the inverse of the top-left 2x2
// matrix, and the inverse of a 2x2 matrix is simple enough to just be inlined in the computation.
//
2017-03-14 18:15:02 -05:00
// Time complexity is O(1).
func (m Matrix) Unproject(u Vec) Vec {
d := (m[0] * m[3]) - (m[1] * m[2])
u.X, u.Y = (u.X-m[4])/d, (u.Y-m[5])/d
return Vec{u.X*m[3] - u.Y*m[1], u.Y*m[0] - u.X*m[2]}
}