-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ellipse.java
80 lines (69 loc) · 1.84 KB
/
Ellipse.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
import java.awt.Color;
import java.awt.Graphics;
/**
* An ellipse-shaped Shape
* Defined by an upper-left corner (x1,y1) and a lower-right corner (x2,y2)
* with x1<=x2 and y1<=y2
*
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Fall 2012
* @author CBK, Fall 2016, constructors and corner setter
*/
public class Ellipse implements Shape {
private int x1, y1, x2, y2; // upper left and lower right
private Color color;
/**
* An "empty" ellipse, with only one point set so far
*/
public Ellipse(int x1, int y1, Color color) {
this.x1 = x1; this.x2 = x1;
this.y1 = y1; this.y2 = y1;
this.color = color;
}
/**
* An ellipse defined by two corners
*/
public Ellipse(int x1, int y1, int x2, int y2, Color color) {
setCorners(x1, y1, x2, y2);
this.color = color;
}
/**
* Redefines the ellipse based on new corners
*/
public void setCorners(int x1, int y1, int x2, int y2) {
// Ensure correct upper left and lower right
this.x1 = Math.min(x1, x2);
this.y1 = Math.min(y1, y2);
this.x2 = Math.max(x1, x2);
this.y2 = Math.max(y1, y2);
}
@Override
public boolean contains(int x, int y) {
double a = (x2-x1)/2.0, b = (y2-y1)/2.0;
double dx = x - (x1 + a); // horizontal distance from center
double dy = y - (y1 + b); // vertical distance from center
// Apply the standard geometry formula. (See CRC, 29th edition, p. 178.)
return Math.pow(dx / a, 2) + Math.pow(dy / b, 2) <= 1;
}
@Override
public void moveBy(int dx, int dy) {
x1 += dx; y1 += dy;
x2 += dx; y2 += dy;
}
@Override
public Color getColor() {
return color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillOval(x1, y1, x2-x1, y2-y1);
}
@Override
public String toString() {
return "ellipse "+x1+" "+y1+" "+x2+" "+y2+" "+color.getRGB();
}
}