Corrected returned Vectors for corner overlaps
This commit is contained in:
parent
8563c28493
commit
680d16c17b
73
geometry.go
73
geometry.go
|
@ -484,9 +484,6 @@ func (c Circle) Intersect(d Circle) Circle {
|
||||||
// - The Rect contains the Circle, partially or fully
|
// - The Rect contains the Circle, partially or fully
|
||||||
// - The Circle contains the Rect, partially of fully
|
// - The Circle contains the Rect, partially of fully
|
||||||
func (c Circle) IntersectRect(r Rect) Vec {
|
func (c Circle) IntersectRect(r Rect) Vec {
|
||||||
// h and v will hold the minimum horizontal and vertical distances (respectively) to avoid overlapping
|
|
||||||
var h, v float64
|
|
||||||
|
|
||||||
// Checks if the c.Center is not in the diagonal quadrants of the rectangle
|
// 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) {
|
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) {
|
||||||
// 'grow' the Rect by c.Radius in each orthagonal
|
// 'grow' the Rect by c.Radius in each orthagonal
|
||||||
|
@ -498,8 +495,8 @@ func (c Circle) IntersectRect(r Rect) Vec {
|
||||||
|
|
||||||
// Get minimum distance to travel out of Rect
|
// Get minimum distance to travel out of Rect
|
||||||
rToC := r.Center().To(c.Center)
|
rToC := r.Center().To(c.Center)
|
||||||
h = c.Radius - math.Abs(rToC.X) + (r.W() / 2)
|
h := c.Radius - math.Abs(rToC.X) + (r.W() / 2)
|
||||||
v = c.Radius - math.Abs(rToC.Y) + (r.H() / 2)
|
v := c.Radius - math.Abs(rToC.Y) + (r.H() / 2)
|
||||||
|
|
||||||
if rToC.X < 0 {
|
if rToC.X < 0 {
|
||||||
h = -h
|
h = -h
|
||||||
|
@ -507,37 +504,6 @@ func (c Circle) IntersectRect(r Rect) Vec {
|
||||||
if rToC.Y < 0 {
|
if rToC.Y < 0 {
|
||||||
v = -v
|
v = -v
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// The center is in the diagonal quadrants
|
|
||||||
if c.Center.To(r.Min).Len() <= c.Radius {
|
|
||||||
// Closest to bottom-left
|
|
||||||
cornerToCenter := r.Min.To(c.Center)
|
|
||||||
// Get the horizontal and vertical overlaps
|
|
||||||
h = c.Radius - math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.Y, 2))
|
|
||||||
v = -1 * (c.Radius + math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.X, 2)))
|
|
||||||
}
|
|
||||||
if c.Center.To(r.Max).Len() <= c.Radius {
|
|
||||||
// Closest to top-right
|
|
||||||
cornerToCenter := r.Max.To(c.Center)
|
|
||||||
// Get the horizontal and vertical overlaps
|
|
||||||
h = c.Radius - math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.Y, 2))
|
|
||||||
v = c.Radius - math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.X, 2))
|
|
||||||
}
|
|
||||||
if c.Center.To(V(r.Min.X, r.Max.Y)).Len() <= c.Radius {
|
|
||||||
// Closest to top-left
|
|
||||||
cornerToCenter := V(r.Min.X, r.Max.Y).To(c.Center)
|
|
||||||
// Get the horizontal and vertical overlaps
|
|
||||||
h = -1 * (c.Radius + math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.Y, 2)))
|
|
||||||
v = c.Radius - math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.X, 2))
|
|
||||||
}
|
|
||||||
if c.Center.To(V(r.Max.X, r.Min.Y)).Len() <= c.Radius {
|
|
||||||
// Closest to bottom-right
|
|
||||||
cornerToCenter := V(r.Max.X, r.Min.Y).To(c.Center)
|
|
||||||
// Get the horizontal and vertical overlaps
|
|
||||||
h = -1 * (c.Radius + math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.Y, 2)))
|
|
||||||
v = -1 * (c.Radius + math.Sqrt(math.Pow(c.Radius, 2)-math.Pow(cornerToCenter.X, 2)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// No intersect
|
// No intersect
|
||||||
if h == 0 && v == 0 {
|
if h == 0 && v == 0 {
|
||||||
|
@ -549,6 +515,41 @@ func (c Circle) IntersectRect(r Rect) Vec {
|
||||||
return V(0, v)
|
return V(0, v)
|
||||||
}
|
}
|
||||||
return V(h, 0)
|
return V(h, 0)
|
||||||
|
} else {
|
||||||
|
// The center is in the diagonal quadrants
|
||||||
|
|
||||||
|
// 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
|
||||||
|
if c.Center.To(r.Min).Len() <= c.Radius {
|
||||||
|
// Closest to bottom-left
|
||||||
|
centerToCorner = c.Center.To(r.Min)
|
||||||
|
}
|
||||||
|
if c.Center.To(r.Max).Len() <= c.Radius {
|
||||||
|
// Closest to top-right
|
||||||
|
centerToCorner = c.Center.To(r.Max)
|
||||||
|
}
|
||||||
|
if c.Center.To(rectTopLeft).Len() <= c.Radius {
|
||||||
|
// Closest to top-left
|
||||||
|
centerToCorner = c.Center.To(rectTopLeft)
|
||||||
|
}
|
||||||
|
if c.Center.To(rectBottomRight).Len() <= c.Radius {
|
||||||
|
// Closest to bottom-right
|
||||||
|
centerToCorner = c.Center.To(rectBottomRight)
|
||||||
|
}
|
||||||
|
|
||||||
|
cornerToCircumferenceLen := c.Radius - centerToCorner.Len()
|
||||||
|
|
||||||
|
return centerToCorner.Unit().Scaled(cornerToCircumferenceLen)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Matrix is a 2x3 affine matrix that can be used for all kinds of spatial transforms, such
|
// Matrix is a 2x3 affine matrix that can be used for all kinds of spatial transforms, such
|
||||||
|
|
Loading…
Reference in New Issue