-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEllipse.java
More file actions
71 lines (59 loc) · 2.09 KB
/
Ellipse.java
File metadata and controls
71 lines (59 loc) · 2.09 KB
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
import javafx.scene.paint.Color;
import javafx.scene.canvas.GraphicsContext;
public class Ellipse extends Shape {
private double radiusx;
private double radiusy;
public Ellipse(double iwidth, double iheight, Color ioutlineColor, Color ifillColor, double ioutlineWidth, double iposx, double iposy) {
this.radiusx = iwidth;
this.radiusy = iheight;
this.outlineColor = ioutlineColor;
this.fillColor = ifillColor;
this.outlineWidth = ioutlineWidth;
this.posx = iposx;
this.posy = iposy;
}
public void setRadius(double iradiusx, double iradiusy) {
this.radiusx = iradiusx;
this.radiusy = iradiusy;
}
public double getRadiusX() {
return radiusx;
}
public double getRadiusY() {
return radiusy;
}
public double area() {
return Math.PI * radiusx * radiusy;
}
public double perimeter() {
// An approximation of ellipse perimeter
return Math.PI * (3 * (radiusx + radiusy) - Math.sqrt((3 * radiusx + radiusy) * (radiusx + 3 * radiusy)));
}
public boolean equals(Ellipse e) {
return this.radiusx == e.radiusx && this.radiusy == e.radiusy;
}
@Override
public boolean contains(double x, double y) {
// Check if the point (x, y) is within the bounds of the ellipse
double centerX = posx + radiusx;
double centerY = posy + radiusy;
double normalizedX = (x - centerX) / radiusx;
double normalizedY = (y - centerY) / radiusy;
return (normalizedX * normalizedX + normalizedY * normalizedY) <= 1;
}
@Override
public void draw(GraphicsContext gc) {
if (rightselected) {
gc.setStroke(Color.RED);
gc.setLineWidth(outlineWidth + 2);
} else {
gc.setStroke(outlineColor);
gc.setLineWidth(outlineWidth);
}
gc.setFill(fillColor);
// Draw the filled ellipse
gc.fillOval(posx, posy, radiusx * 2, radiusy * 2);
// Draw the outline
gc.strokeOval(posx, posy, radiusx * 2, radiusy * 2);
}
}