Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Circle.java
1461 views
1
package org.newdawn.slick.geom;
2
3
/**
4
* A simple Circle geometry
5
*
6
* @author Kevin Glass
7
*/
8
public strictfp class Circle extends Ellipse {
9
/** The radius of the circle */
10
public float radius;
11
12
/**
13
* Create a new circle based on its radius
14
*
15
* @param centerPointX The x location of the center of the circle
16
* @param centerPointY The y location of the center of the circle
17
* @param radius The radius of the circle
18
*/
19
public Circle(float centerPointX, float centerPointY, float radius) {
20
this(centerPointX, centerPointY, radius, DEFAULT_SEGMENT_COUNT);
21
}
22
23
/**
24
* Create a new circle based on its radius
25
*
26
* @param centerPointX The x location of the center of the circle
27
* @param centerPointY The y location of the center of the circle
28
* @param radius The radius of the circle
29
* @param segmentCount The number of segments to build the circle out of
30
*/
31
public Circle(float centerPointX, float centerPointY, float radius, int segmentCount) {
32
super(centerPointX, centerPointY, radius, radius, segmentCount);
33
this.x = centerPointX - radius;
34
this.y = centerPointY - radius;
35
this.radius = radius;
36
boundingCircleRadius = radius;
37
}
38
39
/**
40
* Get the x coordinate of the centre of the circle
41
*
42
* @return The x coordinate of the centre of the circle
43
*/
44
public float getCenterX() {
45
return getX() + radius;
46
}
47
48
/**
49
* Get the y coordinate of the centre of the circle
50
*
51
* @return The y coordinate of the centre of the circle
52
*/
53
public float getCenterY() {
54
return getY() + radius;
55
}
56
57
/**
58
* Set the radius of this circle
59
*
60
* @param radius The radius of this circle
61
*/
62
public void setRadius(float radius) {
63
if (radius != this.radius) {
64
pointsDirty = true;
65
this.radius = radius;
66
setRadii(radius, radius);
67
}
68
}
69
70
/**
71
* Get the radius of the circle
72
*
73
* @return The radius of the circle
74
*/
75
public float getRadius() {
76
return radius;
77
}
78
79
/**
80
* Check if this circle touches another
81
*
82
* @param shape The other circle
83
* @return True if they touch
84
*/
85
public boolean intersects(Shape shape) {
86
if(shape instanceof Circle) {
87
Circle other = (Circle)shape;
88
float totalRad2 = getRadius() + other.getRadius();
89
90
if (Math.abs(other.getCenterX() - getCenterX()) > totalRad2) {
91
return false;
92
}
93
if (Math.abs(other.getCenterY() - getCenterY()) > totalRad2) {
94
return false;
95
}
96
97
totalRad2 *= totalRad2;
98
99
float dx = Math.abs(other.getCenterX() - getCenterX());
100
float dy = Math.abs(other.getCenterY() - getCenterY());
101
102
return totalRad2 >= ((dx*dx) + (dy*dy));
103
}
104
else if(shape instanceof Rectangle) {
105
return intersects((Rectangle)shape);
106
}
107
else {
108
return super.intersects(shape);
109
}
110
}
111
112
/**
113
* Check if a point is contained by this circle
114
*
115
* @param x The x coordinate of the point to check
116
* @param y The y coorindate of the point to check
117
* @return True if the point is contained by this circle
118
*/
119
public boolean contains(float x, float y) {
120
return intersects(new Circle(x,y,0));
121
}
122
123
/**
124
* @see org.newdawn.slick.geom.Ellipse#findCenter()
125
*/
126
protected void findCenter() {
127
center = new float[2];
128
center[0] = x + radius;
129
center[1] = y + radius;
130
}
131
132
/**
133
* @see org.newdawn.slick.geom.Ellipse#calculateRadius()
134
*/
135
protected void calculateRadius() {
136
boundingCircleRadius = radius;
137
}
138
139
/**
140
* Check if this circle touches a rectangle
141
*
142
* @param other The rectangle to check against
143
* @return True if they touch
144
*/
145
private boolean intersects(Rectangle other) {
146
Rectangle box = other;
147
Circle circle = this;
148
149
if (box.contains(x,y)) {
150
return true;
151
}
152
153
float x1 = box.getX();
154
float y1 = box.getY();
155
float x2 = box.getX() + box.getWidth();
156
float y2 = box.getY() + box.getHeight();
157
158
Line[] lines = new Line[4];
159
lines[0] = new Line(x1,y1,x2,y1);
160
lines[1] = new Line(x2,y1,x2,y2);
161
lines[2] = new Line(x2,y2,x1,y2);
162
lines[3] = new Line(x1,y2,x1,y1);
163
164
float r2 = circle.getRadius() * circle.getRadius();
165
166
Vector2f pos = new Vector2f(circle.getCenterX(), circle.getCenterY());
167
168
for (int i=0;i<4;i++) {
169
float dis = lines[i].distanceSquared(pos);
170
if (dis < r2) {
171
return true;
172
}
173
}
174
175
return false;
176
}
177
178
}
179
180