判断一个点与圆的关系

来源:互联网 发布:微信链接网络出错1003 编辑:程序博客网 时间:2024/04/29 18:11
//判断点和圆的关系。class Points{    private int x;    private int y;    Points(int x,int y)    {        this.x=x;        this.y=y;    }    public int getX()//获取x的值    {        return x;    }    public int getY()//获取y的值    {        return y;    }}class Circles{    private int r;    Circles(int r)    {        this.r=r;    }    //判断点到圆的什么地方,在圆内返回1,在圆外返回-1,在圆上返回0    int judge(Points p)    {        int xxyy=p.getX()*p.getX()+p.getY()*p.getY();        int rr = this.r*this.r;        if (xxyy>rr)        {            return -1;        }        else if (xxyy<rr)        {            return 1;        }        else        {            return 0;        }    }}class CircleDemo {    public static void main(String[] args)     {        //创建一个点对象        Points p=new Points(3,4);        //创建一个圆对象        Circles c=new Circles(5);        int ret = c.judge(p);//调用circle类中的判断        System.out.println(ret);    }}
0 0
原创粉丝点击