-
Notifications
You must be signed in to change notification settings - Fork 2
/
circle.go
137 lines (113 loc) · 3.07 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cirno
import "fmt"
// Circle represents a geometric euclidian circle.
type Circle struct {
center Vector
radius float64
tag
data
domain
}
// TypeName returns the name of the shape type.
func (c *Circle) TypeName() string {
return "Circle"
}
// Center returns the coordinates of the center
// of the circle.
func (c *Circle) Center() Vector {
return c.center
}
// Angle doesn't return any valuable
// data because there is no sense to
// rotate the circle.
//
// This method is added just to match the
// Shape interface.
func (c *Circle) Angle() float64 {
return 0
}
// AngleRadians doesn't return any valuable
// data because there is no sense to
// rotate the circle.
//
// This method is added just to match the
// Shape interface.
func (c *Circle) AngleRadians() float64 {
return 0
}
// Radius returns the radius og the circle.
func (c *Circle) Radius() float64 {
return c.radius
}
// Move moves the circle in the specified direction.
func (c *Circle) Move(direction Vector) Vector {
c.center = c.center.Add(direction)
return c.center
}
// Rotate does nothing to the circle because there
// is no sense to rotate it.
//
// This method is added just to match the
// Shape interface.
func (c *Circle) Rotate(angle float64) float64 {
return 0
}
// RotateRadians does nothing to the circle because there
// is no sense to rotate it.
//
// This method is added just to match the
// Shape interface.
func (c *Circle) RotateRadians(angle float64) float64 {
return 0
}
// RotateAround rotates the circle around the specified point
// changing the circle's position.
func (c *Circle) RotateAround(angle float64, base Vector) Vector {
c.center = c.center.RotateAround(angle, base)
return c.center
}
// RotateAroundRadians rotates the circle around the base point at the
// specified angle in radians.
func (c *Circle) RotateAroundRadians(angle float64, base Vector) Vector {
c.center = c.center.RotateAroundRadians(angle, base)
return c.center
}
// SetPosition sets the circle position to the given
// coordinates.
func (c *Circle) SetPosition(pos Vector) Vector {
c.center = pos
return c.center
}
// SetAngle does nothing to the circle because there is
// no sense to rotate it.
//
// This method is added just to match the
// Shape interface.
func (c *Circle) SetAngle(angle float64) float64 {
return 0
}
// SetAngleRadians does nothing to the circle because there is
// no sense to rotate it.
//
// This method is added just to match the
// Shape interface.
func (c *Circle) SetAngleRadians(angle float64) float64 {
return 0
}
// ContainsPoint detects if the given point is inside the circle.
func (c *Circle) ContainsPoint(point Vector) bool {
d := c.center.Subtract(point)
return d.SquaredMagnitude() <= c.radius*c.radius
}
// NewCircle create a new circle with the given parameters.
func NewCircle(position Vector, radius float64) (*Circle, error) {
if radius <= 0 {
return nil, fmt.Errorf(
"circle radius must be positive, but got %f", radius)
}
circle := &Circle{}
circle.center = position
circle.radius = radius
circle.treeNodes = []*quadTreeNode{}
return circle, nil
}