public class Exercise10_11{
public static void main(String[] args){
Circle2D c1 = new Circle2D(2,2,5.5);
System.out.println(“面积为: ” + c1.getArea() + ” \n周长为: ” + c1.getPerimeter());
System.out.println(“点在圆内 ? ” + c1.contains(3,3) + ” 两圆关系为内含 ?” +
c1.contains(new Circle2D (4,5,10.5)) + ” 两圆相交 ?” + c1.overlaps(new Circle2D(3,5,2.3)));
}
}
class Circle2D{
private double x;
private double y;
private double radius;
public Circle2D(){
x = y = 0;
radius = 1;
}
public Circle2D(double x,double y,double radius){
this.x = x;
this.y = y;
this.radius = radius;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public double getRadius(){
return radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
public double getPerimeter(){
return 2 * Math.PI * radius;
}
public double d(double x,double y){
return Math.sqrt((this.x - x)*(this.x -x) + (this.y - y)*(this.y -y));
}
public boolean contains(double x,double y){
if(d(x,y) < radius)
return true;
return false;
}
public boolean contains(Circle2D circle){
double x1 = circle.getX();
double y1 = circle.getY();
double radius1 = circle.getRadius();
if(d(x1,y1) < Math.abs(radius - radius1))
return true;
return false;
}
public boolean overlaps(Circle2D circle){
double x1 = circle.getX();
double y1 = circle.getY();
double radius1 = circle.getRadius();
if(Math.abs(radius - radius1) < d(x1,y1) && d(x1,y1) < (radius + radius1))
return true;
return false;
}
}
重点内容