1 package org.newdawn.slick.geom;
2
3 import javax.annotation.Nonnull;
4
5
6
7
8
9
10 public strictfp class Circle extends Ellipse {
11
12
13
14 private static final long serialVersionUID = 1L;
15
16 private float radius;
17
18
19
20
21
22
23
24
25 public Circle(float centerPointX, float centerPointY, float radius) {
26 this(centerPointX, centerPointY, radius, DEFAULT_SEGMENT_COUNT);
27 }
28
29
30
31
32
33
34
35
36
37 private Circle(float centerPointX, float centerPointY, float radius, int segmentCount) {
38 super(centerPointX, centerPointY, radius, radius, segmentCount);
39 this.x = centerPointX - radius;
40 this.y = centerPointY - radius;
41 this.radius = radius;
42 boundingCircleRadius = radius;
43 }
44
45
46
47
48
49
50 float getCenterX() {
51 return getX() + radius;
52 }
53
54
55
56
57
58
59 float getCenterY() {
60 return getY() + radius;
61 }
62
63
64
65
66
67
68 public void setRadius(float radius) {
69 if (radius != this.radius) {
70 pointsDirty = true;
71 this.radius = radius;
72 setRadii(radius, radius);
73 }
74 }
75
76
77
78
79
80
81 float getRadius() {
82 return radius;
83 }
84
85
86
87
88
89
90
91 public boolean intersects(@Nonnull Shape shape) {
92 if(shape instanceof Circle) {
93 Circle other = (Circle)shape;
94 float totalRad2 = getRadius() + other.getRadius();
95
96 if (Math.abs(other.getCenterX() - getCenterX()) > totalRad2) {
97 return false;
98 }
99 if (Math.abs(other.getCenterY() - getCenterY()) > totalRad2) {
100 return false;
101 }
102
103 totalRad2 *= totalRad2;
104
105 float dx = Math.abs(other.getCenterX() - getCenterX());
106 float dy = Math.abs(other.getCenterY() - getCenterY());
107
108 return totalRad2 >= ((dx*dx) + (dy*dy));
109 }
110 else if(shape instanceof Rectangle) {
111 return intersects((Rectangle)shape);
112 }
113 else {
114 return super.intersects(shape);
115 }
116 }
117
118
119
120
121
122
123
124
125 public boolean contains(float x, float y)
126 {
127 return (x - getCenterX()) * (x - getCenterX()) + (y - getCenterY()) * (y - getCenterY()) < getRadius() * getRadius();
128 }
129
130
131
132
133 protected void findCenter() {
134 center = new float[2];
135 center[0] = x + radius;
136 center[1] = y + radius;
137 }
138
139
140
141
142 protected void calculateRadius() {
143 boundingCircleRadius = radius;
144 }
145
146
147
148
149
150
151
152 private boolean intersects(Rectangle other) {
153 Rectangle box = other;
154 Circle circle = this;
155
156 if (box.contains(x+radius,y+radius)) {
157 return true;
158 }
159
160 float x1 = box.getX();
161 float y1 = box.getY();
162 float x2 = box.getX() + box.getWidth();
163 float y2 = box.getY() + box.getHeight();
164
165 Line[] lines = new Line[4];
166 lines[0] = new Line(x1,y1,x2,y1);
167 lines[1] = new Line(x2,y1,x2,y2);
168 lines[2] = new Line(x2,y2,x1,y2);
169 lines[3] = new Line(x1,y2,x1,y1);
170
171 float r2 = circle.getRadius() * circle.getRadius();
172
173 Vector2f pos = new Vector2f(circle.getCenterX(), circle.getCenterY());
174
175 for (int i=0;i<4;i++) {
176 float dis = lines[i].distanceSquared(pos);
177 if (dis < r2) {
178 return true;
179 }
180 }
181
182 return false;
183 }
184 }