import java.util.ArrayList
import java.util.List
import java.util.Randompublic class Circle{ private int r private Point p//以下为setter和getter,省略 //以下为在圆内随机得到 N 个点 public List<Point>getRandomPoint(int n){
List<Point>list = new ArrayList<Point>()
for (int i = 0i <ni++) {
Random random = new Random()
int x = random.nextInt(2 * r)
x += p.x - r
int k = (int) Math.sqrt(r * r - ((x - p.x) * (x - p.x)))
int y = random.nextInt(2 * k)
y += p.y - r
Point pp = new Point()
pp.x = x
pp.y = y
}
return list
} public Point getP() {
return p
} public void setP(Point p) {
this.p = p
} public int getR() {
return r
} public void setR(int r) {
this.r = r
}
}
public class Circle {double x,y,r
//无参构造方法
public Circle() {}
//三个参数的构造方法
public Circle(double x, double y, double r) {
this.x = x
this.y = y
this.r = r
}
//求面积的方法,返回面积值
public double getArea() {
//求圆的面积
double s = 3.14*r*r
return s
}
//求周长的方法
public double getPerimeter() {
double l = 3.14*r*2
return l
}
//判断是否相交
public String getStatus(Circle c) {
//判断
//当圆心距等于两圆半径之差时 两圆内切
//当圆心距小于两圆半径之和 大于半径之差时 两圆相交
//当圆心距等于两圆半径之和时 两圆外切
//当圆心距大于两圆半径之和时 两圆外离
String result = ""
double l = Math.sqrt((x-c.x)*(x-c.x)+(y-c.y)*(y-c.y))
if(l > r+c.r) {
result = "相离"
}else if(l == r+c.r) {
result = "外切"
}else if(l < r+c.r && l > r+c.r) {
result = "相交"
}else if(l == Math.abs(r-c.r)) {
result = "内切"
}else {
result = "内含"
}
return result
}
}
public class TestCircle {
public static void main(String[] args) {
Circle c1 = new Circle(5, 5, 5)
Circle c2 = new Circle(1, 2, 3)
//面积
System.out.println(c1.getArea())
//周长
System.out.println(c2.getPerimeter())
//状态
System.out.println(c1.getStatus(c2))
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)