Skip to content
geometry.go 3.82 KiB
Newer Older
faiface's avatar
faiface committed
package pixel

import (
	"fmt"
	"math"
	"math/cmplx"
)

faiface's avatar
faiface committed
// 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.
faiface's avatar
faiface committed
//
// 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
faiface's avatar
faiface committed
//   fmt.Println(w)     // Vec(9, -1)
//   fmt.Println(u - v) // Vec(-7, 5)
faiface's avatar
faiface committed
//
// Additional standard vector operations can be obtained with methods:
//
faiface's avatar
faiface committed
//   u := pixel.V(2, 3)
//   v := pixel.V(8, 1)
faiface's avatar
faiface committed
//   if u.X() < 0 {
//	     fmt.Println("this won't happen")
faiface's avatar
faiface committed
//   }
//   x := u.Unit().Dot(v.Unit())
faiface's avatar
faiface committed
type Vec complex128
faiface's avatar
faiface committed

// V returns a new 2d vector with the given coordinates.
faiface's avatar
faiface committed
func V(x, y float64) Vec {
	return Vec(complex(x, y))
faiface's avatar
faiface committed
}

faiface's avatar
faiface committed
// String returns the string representation of the vector u.
faiface's avatar
faiface committed
//
//   u := pixel.V(4.5, -1.3)
//   u.String()     // returns "Vec(4.5, -1.3)"
//   fmt.Println(u) // Vec(4.5, -1.3)
faiface's avatar
faiface committed
func (u Vec) String() string {
	return fmt.Sprintf("Vec(%v, %v)", u.X(), u.Y())
faiface's avatar
faiface committed
}

faiface's avatar
faiface committed
// X returns the x coordinate of the vector u.
faiface's avatar
faiface committed
func (u Vec) X() float64 {
faiface's avatar
faiface committed
	return real(u)
}

faiface's avatar
faiface committed
// Y returns the y coordinate of the vector u.
faiface's avatar
faiface committed
func (u Vec) Y() float64 {
faiface's avatar
faiface committed
	return imag(u)
}

faiface's avatar
faiface committed
// XY returns the components of the vector in two return values.
faiface's avatar
faiface committed
func (u Vec) XY() (x, y float64) {
	return real(u), imag(u)
}

faiface's avatar
faiface committed
// Len returns the length of the vector u.
faiface's avatar
faiface committed
func (u Vec) Len() float64 {
faiface's avatar
faiface committed
	return cmplx.Abs(complex128(u))
}

faiface's avatar
faiface committed
// Angle returns the angle between the vector u and the x-axis. The result is in the range [-Pi, Pi].
faiface's avatar
faiface committed
func (u Vec) Angle() float64 {
faiface's avatar
faiface committed
	return cmplx.Phase(complex128(u))
}

// Unit returns a vector of length 1 with the same angle as u.
faiface's avatar
faiface committed
func (u Vec) Unit() Vec {
faiface's avatar
faiface committed
	return u / V(u.Len(), 0)
}

faiface's avatar
faiface committed
// Scaled returns the vector u multiplied by c.
faiface's avatar
faiface committed
func (u Vec) Scaled(c float64) Vec {
	return u * V(c, 0)
faiface's avatar
faiface committed
}

faiface's avatar
faiface committed
// Rotated returns the vector u rotated by the given angle in radians.
faiface's avatar
faiface committed
func (u Vec) Rotated(angle float64) Vec {
faiface's avatar
faiface committed
	sin, cos := math.Sincos(angle)
	return u * V(cos, sin)
faiface's avatar
faiface committed
}

// Dot returns the dot product of vectors u and v.
faiface's avatar
faiface committed
func (u Vec) Dot(v Vec) float64 {
faiface's avatar
faiface committed
	return u.X()*v.X() + u.Y()*v.Y()
}

// Cross return the cross product of vectors u and v.
faiface's avatar
faiface committed
func (u Vec) Cross(v Vec) float64 {
faiface's avatar
faiface committed
	return u.X()*v.Y() - v.X()*u.Y()
}
faiface's avatar
faiface committed

faiface's avatar
faiface committed
// Rect is a 2D rectangle aligned with the axes of the coordinate system. It has a position
// and a size.
faiface's avatar
faiface committed
//
// You can manipulate the position and the size using the usual vector operations.
type Rect struct {
	Pos, Size Vec
}

faiface's avatar
faiface committed
// R returns a new Rect with given position (x, y) and size (w, h).
faiface's avatar
faiface committed
func R(x, y, w, h float64) Rect {
	return Rect{
		Pos:  V(x, y),
		Size: V(w, h),
	}
}

faiface's avatar
faiface committed
// String returns the string representation of the rectangle.
faiface's avatar
faiface committed
//
//   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())
}

faiface's avatar
faiface committed
// X returns the x coordinate of the position of the rectangle.
faiface's avatar
faiface committed
func (r Rect) X() float64 {
	return r.Pos.X()
}

faiface's avatar
faiface committed
// Y returns the y coordinate of the position of the rectangle
faiface's avatar
faiface committed
func (r Rect) Y() float64 {
	return r.Pos.Y()
}

faiface's avatar
faiface committed
// W returns the width of the rectangle.
faiface's avatar
faiface committed
func (r Rect) W() float64 {
	return r.Size.X()
}

faiface's avatar
faiface committed
// H returns the height of the rectangle.
faiface's avatar
faiface committed
func (r Rect) H() float64 {
	return r.Size.Y()
}

faiface's avatar
faiface committed
// XYWH returns all of the four components of the rectangle in four return values.
faiface's avatar
faiface committed
func (r Rect) XYWH() (x, y, w, h float64) {
	return r.X(), r.Y(), r.W(), r.H()
}

faiface's avatar
faiface committed
// Center returns the position of the center of the rectangle.
faiface's avatar
faiface committed
func (r Rect) Center() Vec {
	return r.Pos + r.Size.Scaled(0.5)
}