-
Notifications
You must be signed in to change notification settings - Fork 47
/
circle.go
82 lines (67 loc) · 2.08 KB
/
circle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package resolv
// Circle represents a circle (naturally), and is essentially a point with a radius.
type Circle struct {
ShapeBase
radius float64
}
// NewCircle returns a new Circle, with its center at the X and Y position given, and with the defined radius.
func NewCircle(x, y, radius float64) *Circle {
circle := &Circle{
ShapeBase: newShapeBase(x, y),
radius: radius,
}
circle.ShapeBase.owner = circle
return circle
}
// Clone clones the Circle.
func (c *Circle) Clone() IShape {
newCircle := NewCircle(c.position.X, c.position.Y, c.radius)
newCircle.tags.Set(*c.tags)
newCircle.ShapeBase = c.ShapeBase
newCircle.id = globalShapeID
globalShapeID++
newCircle.ShapeBase.space = nil
newCircle.ShapeBase.touchingCells = []*Cell{}
newCircle.ShapeBase.owner = newCircle
return newCircle
}
// Bounds returns the top-left and bottom-right corners of the Circle.
func (c *Circle) Bounds() Bounds {
return Bounds{
Min: Vector{c.position.X - c.radius, c.position.Y - c.radius},
Max: Vector{c.position.X + c.radius, c.position.Y + c.radius},
space: c.space,
}
}
func (c *Circle) Project(axis Vector) Projection {
axis = axis.Unit()
projectedCenter := axis.Dot(c.position)
projectedRadius := axis.Magnitude() * c.radius
min := projectedCenter - projectedRadius
max := projectedCenter + projectedRadius
if min > max {
min, max = max, min
}
return Projection{min, max}
}
// Radius returns the radius of the Circle.
func (c *Circle) Radius() float64 {
return c.radius
}
// SetRadius sets the radius of the Circle, updating the scale multiplier to reflect this change.
func (c *Circle) SetRadius(radius float64) {
c.radius = radius
c.update()
}
// Intersection returns an IntersectionSet for the other Shape provided.
// If no intersection is detected, the IntersectionSet returned is empty.
func (c *Circle) Intersection(other IShape) IntersectionSet {
switch otherShape := other.(type) {
case *ConvexPolygon:
return circleConvexTest(c, otherShape)
case *Circle:
return circleCircleTest(c, otherShape)
}
// This should never happen
panic("Unimplemented intersection")
}