Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/geom/Ellipse.java
1461 views
1
package org.newdawn.slick.geom;
2
3
import java.util.ArrayList;
4
5
import org.newdawn.slick.util.FastTrig;
6
7
/**
8
* An ellipse meeting the <code>Shape</code> contract. The ellipse is actually an approximation using
9
* a series of points generated around the contour of the ellipse.
10
*
11
* @author Mark
12
*/
13
public class Ellipse extends Shape {
14
/**
15
* Default number of segments to draw this ellipse with
16
*/
17
protected static final int DEFAULT_SEGMENT_COUNT = 50;
18
19
/**
20
* The number of segments for graphical representation.
21
*/
22
private int segmentCount;
23
/**
24
* horizontal radius
25
*/
26
private float radius1;
27
/**
28
* vertical radius
29
*/
30
private float radius2;
31
32
/**
33
* Creates a new Ellipse object.
34
*
35
* @param centerPointX x coordinate of the center of the ellipse
36
* @param centerPointY y coordinate of the center of the ellipse
37
* @param radius1 horizontal radius
38
* @param radius2 vertical radius
39
*/
40
public Ellipse(float centerPointX, float centerPointY, float radius1, float radius2) {
41
this(centerPointX, centerPointY, radius1, radius2, DEFAULT_SEGMENT_COUNT);
42
}
43
44
/**
45
* Creates a new Ellipse object.
46
*
47
* @param centerPointX x coordinate of the center of the ellipse
48
* @param centerPointY y coordinate of the center of the ellipse
49
* @param radius1 horizontal radius
50
* @param radius2 vertical radius
51
* @param segmentCount how fine to make the ellipse.
52
*/
53
public Ellipse(float centerPointX, float centerPointY, float radius1, float radius2, int segmentCount) {
54
this.x = centerPointX - radius1;
55
this.y = centerPointY - radius2;
56
this.radius1 = radius1;
57
this.radius2 = radius2;
58
this.segmentCount = segmentCount;
59
checkPoints();
60
}
61
62
/**
63
* Change the shape of this Ellipse
64
*
65
* @param radius1 horizontal radius
66
* @param radius2 vertical radius
67
*/
68
public void setRadii(float radius1, float radius2) {
69
setRadius1(radius1);
70
setRadius2(radius2);
71
}
72
73
/**
74
* Get the horizontal radius of the ellipse
75
*
76
* @return The horizontal radius of the ellipse
77
*/
78
public float getRadius1() {
79
return radius1;
80
}
81
82
/**
83
* Set the horizontal radius of the ellipse
84
*
85
* @param radius1 The horizontal radius to set
86
*/
87
public void setRadius1(float radius1) {
88
if (radius1 != this.radius1) {
89
this.radius1 = radius1;
90
pointsDirty = true;
91
}
92
}
93
94
/**
95
* Get the vertical radius of the ellipse
96
*
97
* @return The vertical radius of the ellipse
98
*/
99
public float getRadius2() {
100
return radius2;
101
}
102
103
/**
104
* Set the vertical radius of the ellipse
105
*
106
* @param radius2 The vertical radius to set
107
*/
108
public void setRadius2(float radius2) {
109
if (radius2 != this.radius2) {
110
this.radius2 = radius2;
111
pointsDirty = true;
112
}
113
}
114
115
/**
116
* Generate the points to outline this ellipse.
117
*
118
*/
119
protected void createPoints() {
120
ArrayList tempPoints = new ArrayList();
121
122
maxX = -Float.MIN_VALUE;
123
maxY = -Float.MIN_VALUE;
124
minX = Float.MAX_VALUE;
125
minY = Float.MAX_VALUE;
126
127
float start = 0;
128
float end = 359;
129
130
float cx = x + radius1;
131
float cy = y + radius2;
132
133
int step = 360 / segmentCount;
134
135
for (float a=start;a<=end+step;a+=step) {
136
float ang = a;
137
if (ang > end) {
138
ang = end;
139
}
140
float newX = (float) (cx + (FastTrig.cos(Math.toRadians(ang)) * radius1));
141
float newY = (float) (cy + (FastTrig.sin(Math.toRadians(ang)) * radius2));
142
143
if(newX > maxX) {
144
maxX = newX;
145
}
146
if(newY > maxY) {
147
maxY = newY;
148
}
149
if(newX < minX) {
150
minX = newX;
151
}
152
if(newY < minY) {
153
minY = newY;
154
}
155
156
tempPoints.add(new Float(newX));
157
tempPoints.add(new Float(newY));
158
}
159
points = new float[tempPoints.size()];
160
for(int i=0;i<points.length;i++) {
161
points[i] = ((Float)tempPoints.get(i)).floatValue();
162
}
163
}
164
165
/**
166
* @see org.newdawn.slick.geom.Shape#transform(org.newdawn.slick.geom.Transform)
167
*/
168
public Shape transform(Transform transform) {
169
checkPoints();
170
171
Polygon resultPolygon = new Polygon();
172
173
float result[] = new float[points.length];
174
transform.transform(points, 0, result, 0, points.length / 2);
175
resultPolygon.points = result;
176
resultPolygon.checkPoints();
177
178
return resultPolygon;
179
}
180
181
/**
182
* @see org.newdawn.slick.geom.Shape#findCenter()
183
*/
184
protected void findCenter() {
185
center = new float[2];
186
center[0] = x + radius1;
187
center[1] = y + radius2;
188
}
189
190
/**
191
* @see org.newdawn.slick.geom.Shape#calculateRadius()
192
*/
193
protected void calculateRadius() {
194
boundingCircleRadius = (radius1 > radius2) ? radius1 : radius2;
195
}
196
}
197
198