1. 程式人生 > >判斷一個點與圓的關係

判斷一個點與圓的關係

//判斷點和圓的關係。
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); } }