2016-11-19 06:08:42 -06:00
|
|
|
package pixel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2017-10-15 12:42:13 -05:00
|
|
|
// Clamp returns x clamped to the interval [min, max].
|
|
|
|
//
|
|
|
|
// If x is less than min, min is returned. If x is more than max, max is returned. Otherwise, x is
|
|
|
|
// returned.
|
|
|
|
func Clamp(x, min, max float64) float64 {
|
|
|
|
if x < min {
|
|
|
|
return min
|
|
|
|
}
|
|
|
|
if x > max {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
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 {
|
2016-12-30 10:43:26 -06:00
|
|
|
// 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-10-15 12:43:12 -05:00
|
|
|
// Unit returns a vector of length 1 facing the given angle.
|
|
|
|
func Unit(angle float64) Vec {
|
|
|
|
return Vec{1, 0}.Rotated(angle)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 16:13:51 -06:00
|
|
|
// Floor converts x and y to their integer equivalents.
|
2019-01-20 09:00:35 -06:00
|
|
|
func (u Vec) Floor() Vec {
|
2019-01-20 08:56:30 -06:00
|
|
|
return Vec{
|
|
|
|
math.Floor(u.X),
|
|
|
|
math.Floor(u.Y),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 18:17:37 -05:00
|
|
|
// To returns the vector from u to v. Equivalent to v.Sub(u).
|
2017-06-09 21:42:20 -05:00
|
|
|
func (u Vec) To(v Vec) Vec {
|
|
|
|
return Vec{
|
|
|
|
v.X - u.X,
|
|
|
|
v.Y - u.Y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 12:25:06 -05:00
|
|
|
// 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-03-13 17:52:34 -05: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)
|
2017-05-21 12:25:06 -05:00
|
|
|
return Vec{
|
|
|
|
u.X*cos - u.Y*sin,
|
|
|
|
u.X*sin + u.Y*cos,
|
|
|
|
}
|
2017-03-23 13:27:39 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 18:18:23 -05:00
|
|
|
// Normal returns a vector normal to u. Equivalent to u.Rotated(math.Pi / 2), but faster.
|
2017-06-10 18:17:37 -05:00
|
|
|
func (u Vec) Normal() Vec {
|
2017-08-29 13:23:32 -05:00
|
|
|
return Vec{-u.Y, u.X}
|
2017-06-10 18:17:37 -05:00
|
|
|
}
|
|
|
|
|
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-10-15 12:50:41 -05:00
|
|
|
// Project returns a projection (or component) of vector u in the direction of vector v.
|
|
|
|
//
|
|
|
|
// Behaviour is undefined if v is a zero vector.
|
|
|
|
func (u Vec) Project(v Vec) Vec {
|
|
|
|
len := u.Dot(v) / v.Len()
|
|
|
|
return v.Unit().Scaled(len)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-01 09:31:20 -05:00
|
|
|
// Line is a 2D line segment, between points `A` and `B`.
|
|
|
|
type Line struct {
|
|
|
|
A, B Vec
|
|
|
|
}
|
|
|
|
|
2019-04-01 09:37:23 -05:00
|
|
|
// L creates and returns a new Line object.
|
|
|
|
func L(from, to Vec) Line {
|
|
|
|
return Line{
|
|
|
|
A: from,
|
|
|
|
B: to,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 09:31:20 -05:00
|
|
|
// Bounds returns the lines bounding box. This is in the form of a normalized `Rect`.
|
|
|
|
func (l Line) Bounds() Rect {
|
|
|
|
return R(l.A.X, l.A.Y, l.B.X, l.B.Y).Norm()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Center will return the point at center of the line; that is, the point equidistant from either end.
|
|
|
|
func (l Line) Center() Vec {
|
|
|
|
return l.A.Add(l.A.To(l.B).Scaled(0.5))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closest will return the point on the line which is closest to the `Vec` provided.
|
|
|
|
func (l Line) Closest(v Vec) Vec {
|
2019-04-03 06:12:27 -05:00
|
|
|
// between is a helper function which determines whether 'x' is greater than min(a, b) and less than max(a, b)
|
|
|
|
between := func(a, b, x float64) bool {
|
|
|
|
min := math.Min(a, b)
|
|
|
|
max := math.Max(a, b)
|
|
|
|
return min < x && x < max
|
|
|
|
}
|
|
|
|
|
|
|
|
// Closest point will be on a line which perpendicular to this line.
|
|
|
|
// If and only if the infinite perpendicular line intersects the segment.
|
2019-04-03 05:09:14 -05:00
|
|
|
m, b := l.Formula()
|
|
|
|
|
|
|
|
// Account for horizontal lines
|
|
|
|
if m == 0 {
|
|
|
|
x := v.X
|
|
|
|
y := l.A.Y
|
2019-04-03 06:12:27 -05:00
|
|
|
|
|
|
|
// check if the X coordinate of v is on the line
|
|
|
|
if between(l.A.X, l.B.X, v.X) {
|
|
|
|
return V(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise get the closest endpoint
|
|
|
|
if l.A.To(v).Len() < l.B.To(v).Len() {
|
|
|
|
return l.A
|
|
|
|
}
|
|
|
|
return l.B
|
2019-04-03 05:09:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Account for vertical lines
|
|
|
|
if math.IsInf(math.Abs(m), 1) {
|
|
|
|
x := l.A.X
|
|
|
|
y := v.Y
|
2019-04-03 06:12:27 -05:00
|
|
|
|
|
|
|
// check if the Y coordinate of v is on the line
|
|
|
|
if between(l.A.Y, l.B.Y, v.Y) {
|
|
|
|
return V(x, y)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise get the closest endpoint
|
|
|
|
if l.A.To(v).Len() < l.B.To(v).Len() {
|
|
|
|
return l.A
|
|
|
|
}
|
2019-04-03 05:09:14 -05:00
|
|
|
return V(x, y)
|
|
|
|
}
|
|
|
|
|
2019-04-03 05:47:36 -05:00
|
|
|
perpendicularM := -1 / m
|
|
|
|
perpendicularB := v.Y - (perpendicularM * v.X)
|
|
|
|
|
|
|
|
// Coordinates of intersect (of infinite lines)
|
|
|
|
x := (perpendicularB - b) / (m - perpendicularM)
|
|
|
|
y := m*x + b
|
|
|
|
|
|
|
|
// Check if the point lies between the x and y bounds of the segment
|
|
|
|
if !between(l.A.X, l.B.X, x) && !between(l.A.Y, l.B.Y, y) {
|
2019-04-02 10:27:54 -05:00
|
|
|
// Not within bounding box
|
|
|
|
toStart := v.To(l.A)
|
|
|
|
toEnd := v.To(l.B)
|
|
|
|
|
|
|
|
if toStart.Len() < toEnd.Len() {
|
|
|
|
return l.A
|
|
|
|
}
|
|
|
|
return l.B
|
2019-04-01 09:31:20 -05:00
|
|
|
}
|
|
|
|
|
2019-04-02 10:27:54 -05:00
|
|
|
return V(x, y)
|
2019-04-01 09:31:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contains returns whether the provided `Vec` lies on the line
|
|
|
|
func (l Line) Contains(v Vec) bool {
|
|
|
|
return l.Closest(v) == v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Formula will return the values that represent the line in the formula: y = mx + b
|
|
|
|
func (l Line) Formula() (m, b float64) {
|
2019-04-03 05:09:14 -05:00
|
|
|
// Account for horizontal lines
|
|
|
|
if l.B.Y == l.A.Y {
|
|
|
|
return 0, l.A.Y
|
|
|
|
}
|
|
|
|
|
2019-04-01 09:31:20 -05:00
|
|
|
m = (l.B.Y - l.A.Y) / (l.B.X - l.A.X)
|
|
|
|
b = l.A.Y - (m * l.A.X)
|
|
|
|
|
|
|
|
return m, b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect will return the point of intersection for the two line segments. If the line segments do not intersect,
|
|
|
|
// this function will return the zero-vector and `false`.
|
|
|
|
func (l Line) Intersect(k Line) (Vec, bool) {
|
|
|
|
// Check if the lines are parallel
|
|
|
|
lDir := l.A.To(l.B)
|
|
|
|
kDir := k.A.To(k.B)
|
2019-04-02 11:01:18 -05:00
|
|
|
if lDir.X == kDir.X && lDir.Y == kDir.Y {
|
2019-04-01 09:31:20 -05:00
|
|
|
return ZV, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// The lines intersect - but potentially not within the line segments.
|
|
|
|
// Get the intersection point for the lines if they were infinitely long, check if the point exists on both of the
|
|
|
|
// segments
|
|
|
|
lm, lb := l.Formula()
|
2019-04-02 11:01:18 -05:00
|
|
|
km, kb := k.Formula()
|
2019-04-03 05:09:14 -05:00
|
|
|
|
|
|
|
// Account for vertical lines
|
|
|
|
if math.IsInf(math.Abs(lm), 1) && math.IsInf(math.Abs(km), 1) {
|
|
|
|
// Both vertical, therefore parallel
|
|
|
|
return ZV, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if math.IsInf(math.Abs(lm), 1) || math.IsInf(math.Abs(km), 1) {
|
|
|
|
// One line is vertical
|
|
|
|
intersectM := lm
|
|
|
|
intersectB := lb
|
|
|
|
verticalLine := k
|
|
|
|
|
|
|
|
if math.IsInf(math.Abs(lm), 1) {
|
|
|
|
intersectM = km
|
|
|
|
intersectB = kb
|
|
|
|
verticalLine = l
|
|
|
|
}
|
|
|
|
|
|
|
|
maxVerticalY := verticalLine.A.Y
|
|
|
|
minVerticalY := verticalLine.B.Y
|
|
|
|
if verticalLine.B.Y > maxVerticalY {
|
|
|
|
maxVerticalY = verticalLine.B.Y
|
|
|
|
minVerticalY = verticalLine.A.Y
|
|
|
|
}
|
|
|
|
|
|
|
|
y := intersectM*l.A.X + intersectB
|
|
|
|
if y > maxVerticalY || y < minVerticalY {
|
|
|
|
// Point is not on the horizontal line
|
|
|
|
return ZV, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return V(l.A.X, y), true
|
|
|
|
}
|
2019-04-01 09:31:20 -05:00
|
|
|
|
|
|
|
// Coordinates of intersect
|
|
|
|
x := (kb - lb) / (lm - km)
|
|
|
|
y := lm*x + lb
|
|
|
|
|
|
|
|
if l.Contains(V(x, y)) && k.Contains(V(x, y)) {
|
|
|
|
// The intersect point is on both line segments, they intersect.
|
|
|
|
return V(x, y), true
|
|
|
|
}
|
|
|
|
|
|
|
|
return ZV, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntersectCircle will return the shortest `Vec` such that the Line and Circle no longer intesect. If they do not
|
|
|
|
// intersect at all, this function will return a zero-vector.
|
|
|
|
func (l Line) IntersectCircle(c Circle) Vec {
|
|
|
|
// Get the point on the line closest to the center of the circle.
|
|
|
|
closest := l.Closest(c.Center)
|
|
|
|
cirToClosest := c.Center.To(closest)
|
|
|
|
|
|
|
|
if cirToClosest.Len() >= c.Radius {
|
|
|
|
return ZV
|
|
|
|
}
|
|
|
|
|
|
|
|
return cirToClosest.Scaled(cirToClosest.Len() - c.Radius)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntersectRect will return the shortest `Vec` such that the Line and Rect no longer intesect. If they do not
|
|
|
|
// intersect at all, this function will return a zero-vector.
|
|
|
|
func (l Line) IntersectRect(r Rect) Vec {
|
|
|
|
// Check if either end of the line segment are within the rectangle
|
|
|
|
if r.Contains(l.A) || r.Contains(l.B) {
|
|
|
|
// Use the `Rect.Intersect` to get minimal return value
|
|
|
|
rIntersect := l.Bounds().Intersect(r)
|
|
|
|
if rIntersect.H() > rIntersect.W() {
|
|
|
|
// Go vertical
|
|
|
|
return V(0, rIntersect.H())
|
|
|
|
}
|
|
|
|
return V(rIntersect.W(), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of the rectangles' edges intersect with this line.
|
|
|
|
for _, edge := range r.Edges() {
|
|
|
|
if _, ok := l.Intersect(edge); ok {
|
|
|
|
// Get the closest points on the line to each corner, where:
|
|
|
|
// - the point is contained by the rectangle
|
|
|
|
// - the point is not the corner itself
|
|
|
|
corners := r.Vertices()
|
|
|
|
closest := ZV
|
|
|
|
closestCorner := corners[0]
|
|
|
|
for _, c := range corners {
|
|
|
|
cc := l.Closest(c)
|
2019-04-03 05:47:36 -05:00
|
|
|
if closest == ZV || (closest.Len() > cc.Len() && r.Contains(cc)) {
|
2019-04-01 09:31:20 -05:00
|
|
|
closest = cc
|
|
|
|
closestCorner = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest.To(closestCorner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No intersect
|
|
|
|
return ZV
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the length of the line segment.
|
|
|
|
func (l Line) Len() float64 {
|
|
|
|
return l.A.Sub(l.B).Len()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Moved will return a line moved by the delta `Vec` provided.
|
|
|
|
func (l Line) Moved(delta Vec) Line {
|
|
|
|
return Line{
|
|
|
|
A: l.A.Add(delta),
|
|
|
|
B: l.B.Add(delta),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotated will rotate the line around the provided `Vec`.
|
|
|
|
func (l Line) Rotated(around Vec, angle float64) Line {
|
|
|
|
// Move the line so we can use `Vec.Rotated`
|
|
|
|
lineShifted := l.Moved(around.Scaled(-1))
|
2019-04-03 05:52:42 -05:00
|
|
|
|
2019-04-01 09:31:20 -05:00
|
|
|
lineRotated := Line{
|
|
|
|
A: lineShifted.A.Rotated(angle),
|
|
|
|
B: lineShifted.B.Rotated(angle),
|
|
|
|
}
|
|
|
|
|
|
|
|
return lineRotated.Moved(around)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scaled will return the line scaled around the center point.
|
|
|
|
func (l Line) Scaled(scale float64) Line {
|
|
|
|
return l.ScaledXY(l.Center(), scale)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScaledXY will return the line scaled around the `Vec` provided.
|
|
|
|
func (l Line) ScaledXY(around Vec, scale float64) Line {
|
|
|
|
toA := around.To(l.A).Scaled(scale)
|
|
|
|
toB := around.To(l.B).Scaled(scale)
|
|
|
|
|
|
|
|
return Line{
|
|
|
|
A: around.Add(toA),
|
|
|
|
B: around.Add(toB),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Line) String() string {
|
|
|
|
return fmt.Sprintf("Line(%v, %v)", l.A, l.B)
|
|
|
|
}
|
|
|
|
|
2017-03-13 17:52:34 -05: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
|
|
|
//
|
2017-03-13 17:52:34 -05: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 {
|
2017-03-13 17:52:34 -05:00
|
|
|
Min, Max Vec
|
2016-12-05 16:19:31 -06:00
|
|
|
}
|
|
|
|
|
2017-03-13 17:52:34 -05: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.
|
2017-03-13 17:52:34 -05:00
|
|
|
func R(minX, minY, maxX, maxY float64) Rect {
|
2016-12-05 16:19:31 -06:00
|
|
|
return Rect{
|
2017-07-05 12:51:54 -05:00
|
|
|
Min: Vec{minX, minY},
|
|
|
|
Max: Vec{maxX, maxY},
|
2017-03-25 13:45:11 -05:00
|
|
|
}
|
2017-03-13 17:52:34 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-13 17:52:34 -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-07-05 12:35:11 -05:00
|
|
|
// Area returns the area of r. If r is not normalized, area may be negative.
|
|
|
|
func (r Rect) Area() float64 {
|
|
|
|
return r.W() * r.H()
|
|
|
|
}
|
|
|
|
|
2019-04-01 09:31:20 -05:00
|
|
|
// Edges will return the four lines which make up the edges of the rectangle.
|
|
|
|
func (r Rect) Edges() [4]Line {
|
|
|
|
corners := r.Vertices()
|
|
|
|
|
|
|
|
return [4]Line{
|
|
|
|
{A: corners[0], B: corners[1]},
|
|
|
|
{A: corners[1], B: corners[2]},
|
|
|
|
{A: corners[2], B: corners[3]},
|
|
|
|
{A: corners[3], B: corners[0]},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2017-03-13 17:52:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-23 13:27:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2017-03-13 17:52:34 -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
|
|
|
//
|
2017-05-14 18:15:16 -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.
|
2017-03-13 17:52:34 -05:00
|
|
|
func (r Rect) Resized(anchor, size Vec) Rect {
|
2017-05-14 18:15:16 -05:00
|
|
|
if r.W()*r.H() == 0 {
|
2017-03-13 17:52:34 -05:00
|
|
|
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()}
|
2017-03-13 17:52:34 -05:00
|
|
|
return Rect{
|
2017-07-28 07:35:00 -05:00
|
|
|
Min: anchor.Add(r.Min.Sub(anchor).ScaledXY(fraction)),
|
|
|
|
Max: anchor.Add(r.Max.Sub(anchor).ScaledXY(fraction)),
|
2017-03-13 17:52:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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),
|
2017-03-13 17:52:34 -05:00
|
|
|
}
|
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-03-06 12:58:24 -06:00
|
|
|
|
2017-07-05 12:51:54 -05:00
|
|
|
// Union returns the minimal Rect which covers both r and s. Rects r and s must be normalized.
|
2017-05-05 08:43:24 -05:00
|
|
|
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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-07-05 12:51:54 -05:00
|
|
|
// Intersect returns the maximal Rect which is covered by both r and s. Rects r and s must be normalized.
|
2017-07-05 12:58:09 -05:00
|
|
|
//
|
|
|
|
// If r and s don't overlap, this function returns R(0, 0, 0, 0).
|
2017-07-05 12:51:54 -05:00
|
|
|
func (r Rect) Intersect(s Rect) Rect {
|
|
|
|
t := R(
|
|
|
|
math.Max(r.Min.X, s.Min.X),
|
|
|
|
math.Max(r.Min.Y, s.Min.Y),
|
2017-07-05 12:51:54 -05:00
|
|
|
math.Min(r.Max.X, s.Max.X),
|
|
|
|
math.Min(r.Max.Y, s.Max.Y),
|
2017-07-05 12:51:54 -05:00
|
|
|
)
|
|
|
|
if t.Min.X >= t.Max.X || t.Min.Y >= t.Max.Y {
|
|
|
|
return Rect{}
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2019-02-14 08:12:05 -06:00
|
|
|
// IntersectCircle returns a minimal required Vector, such that moving the circle by that vector would stop the Circle
|
2019-01-31 02:22:59 -06:00
|
|
|
// and the Rect intersecting. This function returns a zero-vector if the Circle and Rect do not overlap, and if only
|
|
|
|
// the perimeters touch.
|
2019-01-29 05:20:21 -06:00
|
|
|
//
|
2019-02-14 08:15:26 -06:00
|
|
|
// This function will return a non-zero vector if:
|
2019-01-29 05:20:21 -06:00
|
|
|
// - The Rect contains the Circle, partially or fully
|
|
|
|
// - The Circle contains the Rect, partially of fully
|
2019-02-14 08:12:05 -06:00
|
|
|
func (r Rect) IntersectCircle(c Circle) Vec {
|
|
|
|
return c.IntersectRect(r).Scaled(-1)
|
2019-01-29 05:20:21 -06:00
|
|
|
}
|
|
|
|
|
2019-04-01 09:31:20 -05:00
|
|
|
// IntersectLine will return the shortest `Vec` such that if the Rect is moved by the Vec returned, the Line and Rect no
|
|
|
|
// longer intersect.
|
|
|
|
func (r Rect) IntersectLine(l Line) Vec {
|
|
|
|
return l.IntersectRect(r).Scaled(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertices returns a slice of the four corners which make up the rectangle.
|
|
|
|
func (r Rect) Vertices() [4]Vec {
|
|
|
|
return [4]Vec{
|
|
|
|
r.Min,
|
|
|
|
V(r.Min.X, r.Max.Y),
|
2019-04-01 09:42:20 -05:00
|
|
|
r.Max,
|
2019-04-01 09:31:20 -05:00
|
|
|
V(r.Max.X, r.Min.Y),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 03:00:24 -06:00
|
|
|
// Circle is a 2D circle. It is defined by two properties:
|
|
|
|
// - Center vector
|
2019-01-30 02:37:27 -06:00
|
|
|
// - Radius float64
|
2019-01-28 03:00:24 -06:00
|
|
|
type Circle struct {
|
|
|
|
Center Vec
|
2019-01-30 02:37:27 -06:00
|
|
|
Radius float64
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// C returns a new Circle with the given radius and center coordinates.
|
|
|
|
//
|
|
|
|
// Note that a negative radius is valid.
|
2019-01-30 02:37:27 -06:00
|
|
|
func C(center Vec, radius float64) Circle {
|
2019-01-28 03:00:24 -06:00
|
|
|
return Circle{
|
|
|
|
Center: center,
|
2019-01-30 02:37:27 -06:00
|
|
|
Radius: radius,
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string representation of the Circle.
|
|
|
|
//
|
|
|
|
// c := pixel.C(10.1234, pixel.ZV)
|
|
|
|
// c.String() // returns "Circle(10.12, Vec(0, 0))"
|
|
|
|
// fmt.Println(c) // Circle(10.12, Vec(0, 0))
|
|
|
|
func (c Circle) String() string {
|
2019-01-30 02:37:27 -06:00
|
|
|
return fmt.Sprintf("Circle(%s, %.2f)", c.Center, c.Radius)
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
|
2019-01-29 03:33:20 -06:00
|
|
|
// Norm returns the Circle in normalized form - this sets the radius to its absolute value.
|
2019-01-28 03:00:24 -06:00
|
|
|
//
|
|
|
|
// c := pixel.C(-10, pixel.ZV)
|
2019-01-30 02:37:27 -06:00
|
|
|
// c.Norm() // returns pixel.Circle{pixel.Vec{0, 0}, 10}
|
2019-01-28 03:00:24 -06:00
|
|
|
func (c Circle) Norm() Circle {
|
|
|
|
return Circle{
|
|
|
|
Center: c.Center,
|
2019-01-30 02:37:27 -06:00
|
|
|
Radius: math.Abs(c.Radius),
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Area returns the area of the Circle.
|
|
|
|
func (c Circle) Area() float64 {
|
2019-01-30 02:38:51 -06:00
|
|
|
return math.Pi * math.Pow(c.Radius, 2)
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Moved returns the Circle moved by the given vector delta.
|
|
|
|
func (c Circle) Moved(delta Vec) Circle {
|
|
|
|
return Circle{
|
|
|
|
Center: c.Center.Add(delta),
|
2019-01-30 02:37:27 -06:00
|
|
|
Radius: c.Radius,
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 03:33:20 -06:00
|
|
|
// Resized returns the Circle resized by the given delta. The Circles center is use as the anchor.
|
2019-01-28 03:00:24 -06:00
|
|
|
//
|
2019-01-30 02:37:27 -06:00
|
|
|
// c := pixel.C(pixel.ZV, 10)
|
|
|
|
// c.Resized(-5) // returns pixel.Circle{pixel.Vec{0, 0}, 5}
|
|
|
|
// c.Resized(25) // returns pixel.Circle{pixel.Vec{0, 0}, 35}
|
2019-01-28 03:00:24 -06:00
|
|
|
func (c Circle) Resized(radiusDelta float64) Circle {
|
|
|
|
return Circle{
|
|
|
|
Center: c.Center,
|
2019-01-30 02:37:27 -06:00
|
|
|
Radius: c.Radius + radiusDelta,
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains checks whether a vector `u` is contained within this Circle (including it's perimeter).
|
|
|
|
func (c Circle) Contains(u Vec) bool {
|
|
|
|
toCenter := c.Center.To(u)
|
|
|
|
return c.Radius >= toCenter.Len()
|
|
|
|
}
|
|
|
|
|
2019-01-29 03:38:24 -06:00
|
|
|
// maxCircle will return the larger circle based on the radius.
|
|
|
|
func maxCircle(c, d Circle) Circle {
|
2019-01-29 03:33:20 -06:00
|
|
|
if c.Radius < d.Radius {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-01-29 03:38:24 -06:00
|
|
|
// minCircle will return the smaller circle based on the radius.
|
|
|
|
func minCircle(c, d Circle) Circle {
|
2019-01-28 03:00:24 -06:00
|
|
|
if c.Radius < d.Radius {
|
2019-01-29 03:33:20 -06:00
|
|
|
return c
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
2019-01-29 03:33:20 -06:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Union returns the minimal Circle which covers both `c` and `d`.
|
|
|
|
func (c Circle) Union(d Circle) Circle {
|
2019-01-29 05:33:12 -06:00
|
|
|
biggerC := maxCircle(c.Norm(), d.Norm())
|
|
|
|
smallerC := minCircle(c.Norm(), d.Norm())
|
2019-01-28 03:00:24 -06:00
|
|
|
|
|
|
|
// Get distance between centers
|
|
|
|
dist := c.Center.To(d.Center).Len()
|
|
|
|
|
|
|
|
// If the bigger Circle encompasses the smaller one, we have the result
|
|
|
|
if dist+smallerC.Radius <= biggerC.Radius {
|
|
|
|
return biggerC
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate radius for encompassing Circle
|
|
|
|
r := (dist + biggerC.Radius + smallerC.Radius) / 2
|
|
|
|
|
|
|
|
// Calculate center for encompassing Circle
|
|
|
|
theta := .5 + (biggerC.Radius-smallerC.Radius)/(2*dist)
|
2019-01-29 05:45:00 -06:00
|
|
|
center := Lerp(smallerC.Center, biggerC.Center, theta)
|
2019-01-28 03:00:24 -06:00
|
|
|
|
|
|
|
return Circle{
|
|
|
|
Center: center,
|
2019-01-30 02:37:27 -06:00
|
|
|
Radius: r,
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intersect returns the maximal Circle which is covered by both `c` and `d`.
|
|
|
|
//
|
|
|
|
// If `c` and `d` don't overlap, this function returns a zero-sized circle at the centerpoint between the two Circle's
|
|
|
|
// centers.
|
|
|
|
func (c Circle) Intersect(d Circle) Circle {
|
2019-01-29 03:33:20 -06:00
|
|
|
// Check if one of the circles encompasses the other; if so, return that one
|
2019-01-29 05:33:12 -06:00
|
|
|
biggerC := maxCircle(c.Norm(), d.Norm())
|
|
|
|
smallerC := minCircle(c.Norm(), d.Norm())
|
2019-01-29 03:33:20 -06:00
|
|
|
|
|
|
|
if biggerC.Radius >= biggerC.Center.To(smallerC.Center).Len()+smallerC.Radius {
|
|
|
|
return biggerC
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the midpoint between the two radii
|
|
|
|
// Distance between centers
|
|
|
|
dist := c.Center.To(d.Center).Len()
|
|
|
|
// Difference between radii
|
|
|
|
diff := dist - (c.Radius + d.Radius)
|
|
|
|
// Distance from c.Center to the weighted midpoint
|
|
|
|
distToMidpoint := c.Radius + 0.5*diff
|
|
|
|
// Weighted midpoint
|
|
|
|
center := Lerp(c.Center, d.Center, distToMidpoint/dist)
|
|
|
|
|
|
|
|
// No need to calculate radius if the circles do not overlap
|
|
|
|
if c.Center.To(d.Center).Len() >= c.Radius+d.Radius {
|
2019-01-30 02:37:27 -06:00
|
|
|
return C(center, 0)
|
2019-01-29 03:33:20 -06:00
|
|
|
}
|
2019-01-28 03:00:24 -06:00
|
|
|
|
2019-01-29 06:27:38 -06:00
|
|
|
radius := c.Center.To(d.Center).Len() - (c.Radius + d.Radius)
|
2019-01-28 03:00:24 -06:00
|
|
|
|
|
|
|
return Circle{
|
|
|
|
Center: center,
|
2019-01-30 02:37:27 -06:00
|
|
|
Radius: math.Abs(radius),
|
2019-01-28 03:00:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 09:31:20 -05:00
|
|
|
// IntersectLine will return the shortest `Vec` such that if the Rect is moved by the Vec returned, the Line and Rect no
|
|
|
|
// longer intersect.
|
|
|
|
func (c Circle) IntersectLine(l Line) Vec {
|
|
|
|
return l.IntersectCircle(c).Scaled(-1)
|
|
|
|
}
|
|
|
|
|
2019-02-14 08:12:05 -06:00
|
|
|
// IntersectRect returns a minimal required Vector, such that moving the circle by that vector would stop the Circle
|
2019-01-31 02:22:59 -06:00
|
|
|
// and the Rect intersecting. This function returns a zero-vector if the Circle and Rect do not overlap, and if only
|
|
|
|
// the perimeters touch.
|
2019-01-29 05:20:21 -06:00
|
|
|
//
|
2019-02-14 08:15:26 -06:00
|
|
|
// This function will return a non-zero vector if:
|
2019-01-29 05:20:21 -06:00
|
|
|
// - The Rect contains the Circle, partially or fully
|
|
|
|
// - The Circle contains the Rect, partially of fully
|
2019-02-14 08:12:05 -06:00
|
|
|
func (c Circle) IntersectRect(r Rect) Vec {
|
2019-01-29 05:20:21 -06:00
|
|
|
// Checks if the c.Center is not in the diagonal quadrants of the rectangle
|
|
|
|
if (r.Min.X <= c.Center.X && c.Center.X <= r.Max.X) || (r.Min.Y <= c.Center.Y && c.Center.Y <= r.Max.Y) {
|
2019-01-29 05:47:49 -06:00
|
|
|
// 'grow' the Rect by c.Radius in each orthagonal
|
2019-01-31 02:22:59 -06:00
|
|
|
grown := Rect{Min: r.Min.Sub(V(c.Radius, c.Radius)), Max: r.Max.Add(V(c.Radius, c.Radius))}
|
|
|
|
if !grown.Contains(c.Center) {
|
|
|
|
// c.Center not close enough to overlap, return zero-vector
|
|
|
|
return ZV
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get minimum distance to travel out of Rect
|
|
|
|
rToC := r.Center().To(c.Center)
|
2019-02-18 06:03:54 -06:00
|
|
|
h := c.Radius - math.Abs(rToC.X) + (r.W() / 2)
|
|
|
|
v := c.Radius - math.Abs(rToC.Y) + (r.H() / 2)
|
2019-01-31 02:22:59 -06:00
|
|
|
|
|
|
|
if rToC.X < 0 {
|
|
|
|
h = -h
|
|
|
|
}
|
|
|
|
if rToC.Y < 0 {
|
|
|
|
v = -v
|
|
|
|
}
|
2019-02-18 06:03:54 -06:00
|
|
|
|
|
|
|
// No intersect
|
|
|
|
if h == 0 && v == 0 {
|
|
|
|
return ZV
|
|
|
|
}
|
|
|
|
|
|
|
|
if math.Abs(h) > math.Abs(v) {
|
|
|
|
// Vertical distance shorter
|
|
|
|
return V(0, v)
|
|
|
|
}
|
|
|
|
return V(h, 0)
|
2019-01-31 02:22:59 -06:00
|
|
|
} else {
|
|
|
|
// The center is in the diagonal quadrants
|
2019-02-18 06:03:54 -06:00
|
|
|
|
|
|
|
// Helper points to make code below easy to read.
|
|
|
|
rectTopLeft := V(r.Min.X, r.Max.Y)
|
|
|
|
rectBottomRight := V(r.Max.X, r.Min.Y)
|
|
|
|
|
|
|
|
// Check for overlap.
|
|
|
|
if !(c.Contains(r.Min) || c.Contains(r.Max) || c.Contains(rectTopLeft) || c.Contains(rectBottomRight)) {
|
|
|
|
// No overlap.
|
|
|
|
return ZV
|
|
|
|
}
|
|
|
|
|
|
|
|
var centerToCorner Vec
|
2019-01-31 02:22:59 -06:00
|
|
|
if c.Center.To(r.Min).Len() <= c.Radius {
|
|
|
|
// Closest to bottom-left
|
2019-02-18 06:03:54 -06:00
|
|
|
centerToCorner = c.Center.To(r.Min)
|
2019-01-31 02:22:59 -06:00
|
|
|
}
|
|
|
|
if c.Center.To(r.Max).Len() <= c.Radius {
|
|
|
|
// Closest to top-right
|
2019-02-18 06:03:54 -06:00
|
|
|
centerToCorner = c.Center.To(r.Max)
|
2019-01-31 02:22:59 -06:00
|
|
|
}
|
2019-02-18 06:03:54 -06:00
|
|
|
if c.Center.To(rectTopLeft).Len() <= c.Radius {
|
2019-01-31 02:22:59 -06:00
|
|
|
// Closest to top-left
|
2019-02-18 06:03:54 -06:00
|
|
|
centerToCorner = c.Center.To(rectTopLeft)
|
2019-01-31 02:22:59 -06:00
|
|
|
}
|
2019-02-18 06:03:54 -06:00
|
|
|
if c.Center.To(rectBottomRight).Len() <= c.Radius {
|
2019-01-31 02:22:59 -06:00
|
|
|
// Closest to bottom-right
|
2019-02-18 06:03:54 -06:00
|
|
|
centerToCorner = c.Center.To(rectBottomRight)
|
2019-01-31 02:22:59 -06:00
|
|
|
}
|
|
|
|
|
2019-02-18 06:03:54 -06:00
|
|
|
cornerToCircumferenceLen := c.Radius - centerToCorner.Len()
|
2019-01-31 02:22:59 -06:00
|
|
|
|
2019-02-18 06:03:54 -06:00
|
|
|
return centerToCorner.Unit().Scaled(cornerToCircumferenceLen)
|
2019-01-29 05:20:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 22:53:25 -05:00
|
|
|
// Matrix is a 2x3 affine matrix that can be used for all kinds of spatial transforms, such
|
2017-03-06 12:58:24 -06:00
|
|
|
// 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-06 12:58:24 -06:00
|
|
|
//
|
2017-03-15 18:47:50 -05:00
|
|
|
// This code creates a Matrix that first moves everything by 100 units horizontally and 200 units
|
2017-03-06 12:58:24 -06:00
|
|
|
// vertically and then rotates everything by 90 degrees around the origin.
|
2017-06-05 18:54:53 -05:00
|
|
|
//
|
|
|
|
// Layout is:
|
|
|
|
// [0] [2] [4]
|
|
|
|
// [1] [3] [5]
|
2017-06-09 11:13:05 -05:00
|
|
|
// 0 0 1 (implicit row)
|
2017-06-05 18:54:53 -05:00
|
|
|
type Matrix [6]float64
|
2017-03-06 12:58:24 -06:00
|
|
|
|
2017-03-14 10:19:58 -05:00
|
|
|
// IM stands for identity matrix. Does nothing, no transformation.
|
2017-06-05 18:54:53 -05:00
|
|
|
var IM = Matrix{1, 0, 0, 1, 0, 0}
|
2017-03-06 12:58:24 -06:00
|
|
|
|
2017-04-04 07:08:37 -05:00
|
|
|
// String returns a string representation of the Matrix.
|
|
|
|
//
|
|
|
|
// m := pixel.IM
|
2017-06-05 18:54:53 -05:00
|
|
|
// 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(
|
2017-06-05 18:54:53 -05:00
|
|
|
"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
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:35:24 -05:00
|
|
|
// Moved moves everything by the delta vector.
|
|
|
|
func (m Matrix) Moved(delta Vec) Matrix {
|
2017-06-05 18:54:53 -05:00
|
|
|
m[4], m[5] = m[4]+delta.X, m[5]+delta.Y
|
|
|
|
return m
|
2017-03-06 12:58:24 -06:00
|
|
|
}
|
|
|
|
|
2017-03-13 17:52:34 -05:00
|
|
|
// ScaledXY scales everything around a given point by the scale factor in each axis respectively.
|
|
|
|
func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix {
|
2017-06-05 18:54:53 -05:00
|
|
|
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
|
2017-03-06 12:58:24 -06:00
|
|
|
}
|
|
|
|
|
2017-03-13 17:52:34 -05:00
|
|
|
// 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))
|
2017-03-06 12:58:24 -06:00
|
|
|
}
|
|
|
|
|
2017-03-13 17:52:34 -05:00
|
|
|
// Rotated rotates everything around a given point by the given angle in radians.
|
|
|
|
func (m Matrix) Rotated(around Vec, angle float64) Matrix {
|
2017-06-05 18:54:53 -05:00
|
|
|
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-06 12:58:24 -06:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-06-05 18:54:53 -05:00
|
|
|
return Matrix{
|
2017-09-03 17:40:12 -05:00
|
|
|
next[0]*m[0] + next[2]*m[1],
|
|
|
|
next[1]*m[0] + next[3]*m[1],
|
|
|
|
next[0]*m[2] + next[2]*m[3],
|
|
|
|
next[1]*m[2] + next[3]*m[3],
|
|
|
|
next[0]*m[4] + next[2]*m[5] + next[4],
|
|
|
|
next[1]*m[4] + next[3]*m[5] + next[5],
|
2017-06-05 18:54:53 -05:00
|
|
|
}
|
2017-03-18 18:08:46 -05:00
|
|
|
}
|
|
|
|
|
2017-03-06 12:58:24 -06: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).
|
2017-03-06 12:58:24 -06:00
|
|
|
func (m Matrix) Project(u Vec) Vec {
|
2017-06-11 07:14:02 -05:00
|
|
|
return Vec{m[0]*u.X + m[2]*u.Y + m[4], m[1]*u.X + m[3]*u.Y + m[5]}
|
2017-03-06 12:58:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unproject does the inverse operation to Project.
|
2017-03-14 18:15:02 -05:00
|
|
|
//
|
|
|
|
// Time complexity is O(1).
|
2017-03-06 12:58:24 -06:00
|
|
|
func (m Matrix) Unproject(u Vec) Vec {
|
2019-02-12 07:57:03 -06:00
|
|
|
det := m[0]*m[3] - m[2]*m[1]
|
|
|
|
return Vec{
|
2019-02-12 14:06:37 -06:00
|
|
|
(m[3]*(u.X-m[4]) - m[2]*(u.Y-m[5])) / det,
|
|
|
|
(-m[1]*(u.X-m[4]) + m[0]*(u.Y-m[5])) / det,
|
2019-02-12 07:57:03 -06:00
|
|
|
}
|
2017-03-06 12:58:24 -06:00
|
|
|
}
|