| 1 |
package gfx |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
) |
| 7 |
|
| 8 |
// Circle is a 2D circle. It is defined by two properties: |
| 9 |
// - Center vector |
| 10 |
// - Radius float64 |
| 11 |
type Circle struct { |
| 12 |
Center Vec |
| 13 |
Radius float64 |
| 14 |
} |
| 15 |
|
| 16 |
// C returns a new Circle with the given radius and center coordinates. |
| 17 |
// |
| 18 |
// Note that a negative radius is valid. |
| 19 |
func C(center Vec, radius float64) Circle { |
| 20 |
return Circle{ |
| 21 |
Center: center, |
| 22 |
Radius: radius, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
// String returns the string representation of the Circle. |
| 27 |
func (c Circle) String() string { |
| 28 |
return fmt.Sprintf("gfx.C(%s, %.2f)", c.Center, c.Radius) |
| 29 |
} |
| 30 |
|
| 31 |
// Norm returns the Circle in normalized form - this sets the radius to its absolute value. |
| 32 |
func (c Circle) Norm() Circle { |
| 33 |
return Circle{ |
| 34 |
Center: c.Center, |
| 35 |
Radius: math.Abs(c.Radius), |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// Area returns the area of the Circle. |
| 40 |
func (c Circle) Area() float64 { |
| 41 |
return math.Pi * math.Pow(c.Radius, 2) |
| 42 |
} |
| 43 |
|
| 44 |
// Moved returns the Circle moved by the given vector delta. |
| 45 |
func (c Circle) Moved(delta Vec) Circle { |
| 46 |
return Circle{ |
| 47 |
Center: c.Center.Add(delta), |
| 48 |
Radius: c.Radius, |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
// Resized returns the Circle resized by the given delta. The Circles center is use as the anchor. |
| 53 |
func (c Circle) Resized(radiusDelta float64) Circle { |
| 54 |
return Circle{ |
| 55 |
Center: c.Center, |
| 56 |
Radius: c.Radius + radiusDelta, |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Contains checks whether a vector `u` is contained within this Circle (including it's perimeter). |
| 61 |
func (c Circle) Contains(u Vec) bool { |
| 62 |
toCenter := c.Center.To(u) |
| 63 |
return c.Radius >= toCenter.Len() |
| 64 |
} |
| 65 |
|
| 66 |
// Union returns the minimal Circle which covers both `c` and `d`. |
| 67 |
func (c Circle) Union(d Circle) Circle { |
| 68 |
biggerC := maxCircle(c.Norm(), d.Norm()) |
| 69 |
smallerC := minCircle(c.Norm(), d.Norm()) |
| 70 |
|
| 71 |
// Get distance between centers |
| 72 |
dist := c.Center.To(d.Center).Len() |
| 73 |
|
| 74 |
// If the bigger Circle encompasses the smaller one, we have the result |
| 75 |
if dist+smallerC.Radius <= biggerC.Radius { |
| 76 |
return biggerC |
| 77 |
} |
| 78 |
|
| 79 |
// Calculate radius for encompassing Circle |
| 80 |
r := (dist + biggerC.Radius + smallerC.Radius) / 2 |
| 81 |
|
| 82 |
// Calculate center for encompassing Circle |
| 83 |
theta := .5 + (biggerC.Radius-smallerC.Radius)/(2*dist) |
| 84 |
center := smallerC.Center.Lerp(biggerC.Center, theta) |
| 85 |
|
| 86 |
return Circle{ |
| 87 |
Center: center, |
| 88 |
Radius: r, |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Intersect returns the maximal Circle which is covered by both `c` and `d`. |
| 93 |
// |
| 94 |
// If `c` and `d` don't overlap, this function returns a zero-sized circle at the centerpoint between the two Circle's |
| 95 |
// centers. |
| 96 |
func (c Circle) Intersect(d Circle) Circle { |
| 97 |
// Check if one of the circles encompasses the other; if so, return that one |
| 98 |
biggerC := maxCircle(c.Norm(), d.Norm()) |
| 99 |
smallerC := minCircle(c.Norm(), d.Norm()) |
| 100 |
|
| 101 |
if biggerC.Radius >= biggerC.Center.To(smallerC.Center).Len()+smallerC.Radius { |
| 102 |
return biggerC |
| 103 |
} |
| 104 |
|
| 105 |
// Calculate the midpoint between the two radii |
| 106 |
// Distance between centers |
| 107 |
dist := c.Center.To(d.Center).Len() |
| 108 |
// Difference between radii |
| 109 |
diff := dist - (c.Radius + d.Radius) |
| 110 |
// Distance from c.Center to the weighted midpoint |
| 111 |
distToMidpoint := c.Radius + 0.5*diff |
| 112 |
// Weighted midpoint |
| 113 |
center := c.Center.Lerp(d.Center, distToMidpoint/dist) |
| 114 |
|
| 115 |
// No need to calculate radius if the circles do not overlap |
| 116 |
if c.Center.To(d.Center).Len() >= c.Radius+d.Radius { |
| 117 |
return C(center, 0) |
| 118 |
} |
| 119 |
|
| 120 |
radius := c.Center.To(d.Center).Len() - (c.Radius + d.Radius) |
| 121 |
|
| 122 |
return Circle{ |
| 123 |
Center: center, |
| 124 |
Radius: math.Abs(radius), |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// IntersectRect returns a minimal required Vector, such that moving the circle by that vector would stop the Circle |
| 129 |
// and the Rect intersecting. This function returns a zero-vector if the Circle and Rect do not overlap, and if only |
| 130 |
// the perimeters touch. |
| 131 |
// |
| 132 |
// This function will return a non-zero vector if: |
| 133 |
// - The Rect contains the Circle, partially or fully |
| 134 |
// - The Circle contains the Rect, partially of fully |
| 135 |
func (c Circle) IntersectRect(r Rect) Vec { |
| 136 |
// Checks if the c.Center is not in the diagonal quadrants of the rectangle |
| 137 |
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) { |
| 138 |
// 'grow' the Rect by c.Radius in each orthagonal |
| 139 |
grown := Rect{ |
| 140 |
Min: r.Min.Sub(V(c.Radius, c.Radius)), |
| 141 |
Max: r.Max.Add(V(c.Radius, c.Radius)), |
| 142 |
} |
| 143 |
|
| 144 |
if !grown.Contains(c.Center) { |
| 145 |
// c.Center not close enough to overlap, return zero-vector |
| 146 |
return ZV |
| 147 |
} |
| 148 |
|
| 149 |
// Get minimum distance to travel out of Rect |
| 150 |
rToC := r.Center().To(c.Center) |
| 151 |
h := c.Radius - math.Abs(rToC.X) + (r.W() / 2) |
| 152 |
v := c.Radius - math.Abs(rToC.Y) + (r.H() / 2) |
| 153 |
|
| 154 |
if rToC.X < 0 { |
| 155 |
h = -h |
| 156 |
} |
| 157 |
|
| 158 |
if rToC.Y < 0 { |
| 159 |
v = -v |
| 160 |
} |
| 161 |
|
| 162 |
// No intersect |
| 163 |
if h == 0 && v == 0 { |
| 164 |
return ZV |
| 165 |
} |
| 166 |
|
| 167 |
if math.Abs(h) > math.Abs(v) { |
| 168 |
// Vertical distance shorter |
| 169 |
return V(0, v) |
| 170 |
} |
| 171 |
|
| 172 |
return V(h, 0) |
| 173 |
} |
| 174 |
|
| 175 |
// The center is in the diagonal quadrants |
| 176 |
|
| 177 |
// Helper points to make code below easy to read. |
| 178 |
rectTopLeft := V(r.Min.X, r.Max.Y) |
| 179 |
rectBottomRight := V(r.Max.X, r.Min.Y) |
| 180 |
|
| 181 |
// Check for overlap. |
| 182 |
if !(c.Contains(r.Min) || c.Contains(r.Max) || c.Contains(rectTopLeft) || c.Contains(rectBottomRight)) { |
| 183 |
// No overlap. |
| 184 |
return ZV |
| 185 |
} |
| 186 |
|
| 187 |
var centerToCorner Vec |
| 188 |
|
| 189 |
if c.Center.To(r.Min).Len() <= c.Radius { |
| 190 |
// Closest to bottom-left |
| 191 |
centerToCorner = c.Center.To(r.Min) |
| 192 |
} |
| 193 |
|
| 194 |
if c.Center.To(r.Max).Len() <= c.Radius { |
| 195 |
// Closest to top-right |
| 196 |
centerToCorner = c.Center.To(r.Max) |
| 197 |
} |
| 198 |
|
| 199 |
if c.Center.To(rectTopLeft).Len() <= c.Radius { |
| 200 |
// Closest to top-left |
| 201 |
centerToCorner = c.Center.To(rectTopLeft) |
| 202 |
} |
| 203 |
|
| 204 |
if c.Center.To(rectBottomRight).Len() <= c.Radius { |
| 205 |
// Closest to bottom-right |
| 206 |
centerToCorner = c.Center.To(rectBottomRight) |
| 207 |
} |
| 208 |
|
| 209 |
cornerToCircumferenceLen := c.Radius - centerToCorner.Len() |
| 210 |
|
| 211 |
return centerToCorner.Unit().Scaled(cornerToCircumferenceLen) |
| 212 |
} |
| 213 |
|
| 214 |
// IntersectCircle returns a minimal required Vector, such that moving the circle by that vector would stop the Circle |
| 215 |
// and the Rect intersecting. This function returns a zero-vector if the Circle and Rect do not overlap, and if only |
| 216 |
// the perimeters touch. |
| 217 |
// |
| 218 |
// This function will return a non-zero vector if: |
| 219 |
// - The Rect contains the Circle, partially or fully |
| 220 |
// - The Circle contains the Rect, partially of fully |
| 221 |
func (r Rect) IntersectCircle(c Circle) Vec { |
| 222 |
return c.IntersectRect(r).Scaled(-1) |
| 223 |
} |
| 224 |
|
| 225 |
// maxCircle will return the larger circle based on the radius. |
| 226 |
func maxCircle(c, d Circle) Circle { |
| 227 |
if c.Radius < d.Radius { |
| 228 |
return d |
| 229 |
} |
| 230 |
return c |
| 231 |
} |
| 232 |
|
| 233 |
// minCircle will return the smaller circle based on the radius. |
| 234 |
func minCircle(c, d Circle) Circle { |
| 235 |
if c.Radius < d.Radius { |
| 236 |
return c |
| 237 |
} |
| 238 |
return d |
| 239 |
} |