编写一个circle类的JAVA程序验证点在圆内
2个回答
展开全部
import java.awt.Point;
public class Circle{
private Point centerPoint;
private int r;
public Circle(){}
public Circle(Point p,int r){
this.r=r;
this.centerPoint=p;
}
public int getR(){return this.r;}
public void setR(int r){this.r=r;}
public Point getCenPoint(){return this.centerPoint;}
public void setcenterPoint(Point p){this.centerPoint=p;}
public boolean isInside(Point point){
double ar=Math.sqrt((point.x-centerPoint.x)*(point.x-centerPoint.x)+(point.y-centerPoint.y)*(point.y-centerPoint.y));
if(ar>=this.r){
return false;
}else{
return true;
}
}
}
public class test{
public static void main(){
Circle aCircle=new Circle(new Point(0,0),10);
Point p1=new Point(5,5);
Point p2=new Point(15,0);
if(aCircle.isInside(p1)){
System.out.println("p1在圆的内部");
}else{
System.out.println("p1不在圆的内部");
}
Point p2=new Point(15,0);
if(aCircle.isInside(p2)){
System.out.println("p2在圆的内部");
}else{
System.out.println("p2不在圆的内部");
}
}
}
如果你愿意,自己可以把圆的参数都改成float,或者double。这事是简单的一个例子
public class Circle{
private Point centerPoint;
private int r;
public Circle(){}
public Circle(Point p,int r){
this.r=r;
this.centerPoint=p;
}
public int getR(){return this.r;}
public void setR(int r){this.r=r;}
public Point getCenPoint(){return this.centerPoint;}
public void setcenterPoint(Point p){this.centerPoint=p;}
public boolean isInside(Point point){
double ar=Math.sqrt((point.x-centerPoint.x)*(point.x-centerPoint.x)+(point.y-centerPoint.y)*(point.y-centerPoint.y));
if(ar>=this.r){
return false;
}else{
return true;
}
}
}
public class test{
public static void main(){
Circle aCircle=new Circle(new Point(0,0),10);
Point p1=new Point(5,5);
Point p2=new Point(15,0);
if(aCircle.isInside(p1)){
System.out.println("p1在圆的内部");
}else{
System.out.println("p1不在圆的内部");
}
Point p2=new Point(15,0);
if(aCircle.isInside(p2)){
System.out.println("p2在圆的内部");
}else{
System.out.println("p2不在圆的内部");
}
}
}
如果你愿意,自己可以把圆的参数都改成float,或者double。这事是简单的一个例子
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询