首页 > 代码库 > 1000. Circle and Rectangle

1000. Circle and Rectangle

#include<iostream>
#include<cmath>
using namespace std;

class Point{

private:

 double x,y;

public:

 Point();

 Point(double xv,double yv);

 Point(Point& pt);

 double getx();

 double gety();

};

Point::Point(){
 x=0;y=0;
}

Point::Point(double xv,double yv){
 x=xv;y=yv;
}

Point::Point(Point& pt){
 this->x = pt.x;
 this->y = pt.y;
}

double Point::getx(){
 return x;
}
double Point::gety(){
 return y;
}


class Circle : public Point{
 public:
  Circle(double x,double y,double r);
  Circle();
  Circle(Circle &);
     int position(Point &pr){
      double a = pow(pr.getx() - this->getx(),2) + pow(pr.gety() - this->gety(),2);
      if(a==r*r) return 0;
      if(a>r*r) return 1;
      if(a<r*r) return -1;
      }
    private:
     double r;
};

Circle::Circle(double x,double y,double r) : Point(x,y){
 this->r=r;
}

Circle::Circle() : Point(){
 r=0;
}

Circle::Circle(Circle &t) : Point(t.getx(),t.gety()){
 r = t.r;
}

class Rectangle : public Point{
 public:
  
     Rectangle(double x,double y,double len,double wid) : Point(x,y){
      this->len=len;
      this->wid=wid;
     }
     Rectangle();
  Rectangle(Rectangle &);
     int position(Point &pr){
      double a = pr.getx() - this->getx();
      double b = pr.gety() - this->gety();
//      cout<<a<<"KKKK"<<b<<endl;
//      if((a==0&&b<=wid&&b>0)||(a>0&&a<=len&&b==0)||(a==len&&b<=wid&&b>0)||(a>0&&a<=len&&b==wid)) return 0;
      if(a<len&&a>0&&b<wid&&b>0) return -1;
      else if(a>len||b>wid||a<0||b<0) return 1; //wai
      else return 0; //zai
      }
    private:
     double len;
     double wid;
};

Rectangle::Rectangle() : Point(){
 len=1;
 wid=1;
}
Rectangle::Rectangle(Rectangle &t): Point(t.getx(),t.gety()){
 len = t.len;
 wid = t.wid;
}

 

1000. Circle and Rectangle