-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Circle.java
222 lines (194 loc) · 6.78 KB
/
Circle.java
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
******************************************************************************/
package com.badlogic.gdx.math;
import java.io.Serializable;
import com.badlogic.gdx.utils.NumberUtils;
/** A convenient 2D circle class.
* @author mzechner */
public class Circle implements Serializable, Shape2D {
public float x, y;
public float radius;
/** Constructs a new circle with all values set to zero */
public Circle () {
}
/** Constructs a new circle with the given X and Y coordinates and the given radius.
*
* @param x X coordinate
* @param y Y coordinate
* @param radius The radius of the circle */
public Circle (float x, float y, float radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
/** Constructs a new circle using a given {@link Vector2} that contains the desired X and Y coordinates, and a given radius.
*
* @param position The position {@link Vector2}.
* @param radius The radius */
public Circle (Vector2 position, float radius) {
this.x = position.x;
this.y = position.y;
this.radius = radius;
}
/** Copy constructor
*
* @param circle The circle to construct a copy of. */
public Circle (Circle circle) {
this.x = circle.x;
this.y = circle.y;
this.radius = circle.radius;
}
/** Creates a new {@link Circle} in terms of its center and a point on its edge.
*
* @param center The center of the new circle
* @param edge Any point on the edge of the given circle */
public Circle (Vector2 center, Vector2 edge) {
this.x = center.x;
this.y = center.y;
this.radius = Vector2.len(center.x - edge.x, center.y - edge.y);
}
/** Sets a new location and radius for this circle.
*
* @param x X coordinate
* @param y Y coordinate
* @param radius Circle radius */
public void set (float x, float y, float radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
/** Sets a new location and radius for this circle.
*
* @param position Position {@link Vector2} for this circle.
* @param radius Circle radius */
public void set (Vector2 position, float radius) {
this.x = position.x;
this.y = position.y;
this.radius = radius;
}
/** Sets a new location and radius for this circle, based upon another circle.
*
* @param circle The circle to copy the position and radius of. */
public void set (Circle circle) {
this.x = circle.x;
this.y = circle.y;
this.radius = circle.radius;
}
/** Sets this {@link Circle}'s values in terms of its center and a point on its edge.
*
* @param center The new center of the circle
* @param edge Any point on the edge of the given circle */
public void set (Vector2 center, Vector2 edge) {
this.x = center.x;
this.y = center.y;
this.radius = Vector2.len(center.x - edge.x, center.y - edge.y);
}
/** Sets the x and y-coordinates of circle center from vector
* @param position The position vector */
public void setPosition (Vector2 position) {
this.x = position.x;
this.y = position.y;
}
/** Sets the x and y-coordinates of circle center
* @param x The x-coordinate
* @param y The y-coordinate */
public void setPosition (float x, float y) {
this.x = x;
this.y = y;
}
/** Sets the x-coordinate of circle center
* @param x The x-coordinate */
public void setX (float x) {
this.x = x;
}
/** Sets the y-coordinate of circle center
* @param y The y-coordinate */
public void setY (float y) {
this.y = y;
}
/** Sets the radius of circle
* @param radius The radius */
public void setRadius (float radius) {
this.radius = radius;
}
/** Checks whether or not this circle contains a given point.
*
* @param x X coordinate
* @param y Y coordinate
*
* @return true if this circle contains the given point. */
public boolean contains (float x, float y) {
x = this.x - x;
y = this.y - y;
return x * x + y * y <= radius * radius;
}
/** Checks whether or not this circle contains a given point.
*
* @param point The {@link Vector2} that contains the point coordinates.
*
* @return true if this circle contains this point; false otherwise. */
public boolean contains (Vector2 point) {
float dx = x - point.x;
float dy = y - point.y;
return dx * dx + dy * dy <= radius * radius;
}
/** @param c the other {@link Circle}
* @return whether this circle contains the other circle. */
public boolean contains (Circle c) {
final float radiusDiff = radius - c.radius;
if (radiusDiff < 0f) return false; // Can't contain bigger circle
final float dx = x - c.x;
final float dy = y - c.y;
final float dst = dx * dx + dy * dy;
final float radiusSum = radius + c.radius;
return (!(radiusDiff * radiusDiff < dst) && (dst < radiusSum * radiusSum));
}
/** @param c the other {@link Circle}
* @return whether this circle overlaps the other circle. */
public boolean overlaps (Circle c) {
float dx = x - c.x;
float dy = y - c.y;
float distance = dx * dx + dy * dy;
float radiusSum = radius + c.radius;
return distance < radiusSum * radiusSum;
}
/** Returns a {@link String} representation of this {@link Circle} of the form {@code x,y,radius}. */
@Override
public String toString () {
return x + "," + y + "," + radius;
}
/** @return The circumference of this circle (as 2 * {@link MathUtils#PI2}) * {@code radius} */
public float circumference () {
return this.radius * MathUtils.PI2;
}
/** @return The area of this circle (as {@link MathUtils#PI} * radius * radius). */
public float area () {
return this.radius * this.radius * MathUtils.PI;
}
@Override
public boolean equals (Object o) {
if (o == this) return true;
if (o == null || o.getClass() != this.getClass()) return false;
Circle c = (Circle)o;
return this.x == c.x && this.y == c.y && this.radius == c.radius;
}
@Override
public int hashCode () {
final int prime = 41;
int result = 1;
result = prime * result + NumberUtils.floatToRawIntBits(radius);
result = prime * result + NumberUtils.floatToRawIntBits(x);
result = prime * result + NumberUtils.floatToRawIntBits(y);
return result;
}
}