第12週作業4

5115 ワード

// 

// main() 
#include <iostream>

#include <cmath>

using namespace std;

class Point
{
public:
	Point(float xx, float yy)
	{
		x = xx;
		y = yy;
	}
	void show()
	{
		cout << "(" << x << ", " << y << ")" << endl;
	}
	friend ostream & operator << (ostream & output, Point & p)
	{
		p.show();
		
		return output;
	}
	double distance(Point &p)    
    {  
        double dx = x - p.x;  
        double dy = y - p.y; 

        return sqrt(dx * dx + dy * dy);  
    }
	double getx(){return x;}
	double gety(){return y;}
	void setx(double x1){x = x1;}
	void sety(double y1){y = y1;}
	~Point(){}
private:
	double x;
	double y;
};

class Circle:public Point
{
public:
	Circle(float xx, float yy, float r):Point(xx, yy)
	{
		R = r;
	}
	double display()
	{
		Point::show();
		cout << " : ";

		return R;
	}
	friend ostream & operator << (ostream & output, Circle & c)
	{
		output << c.display() << endl; 
		
		return output;
	}

	friend int locate(Point &p, Circle &c)
	{
		
		double d = c.distance(p);
		if(d - c.R > 0)
			return 1;
		else if(d < c.R)
			return -1;
		else 
			return 0;
	}
	
	bool operator > (const Circle &);  
    bool operator < (const Circle &);  
    bool operator >= (const Circle &);  
    bool operator <= (const Circle &);  
    bool operator == (const Circle &);  
    bool operator != (const Circle &);

	friend void crossover_point(Point &p1,Circle &c1, Point &p4,Point &p5);
	
	~Circle(){}
private:
	double R;
};

bool Circle::operator > (const Circle &c)  
{  
	return (R > c.R);  
}
bool Circle::operator < (const Circle &c)  
{  
	return (R < c.R);  
}
bool Circle::operator >= (const Circle &c)  
{  
	return (R >= c.R);  
}
bool Circle::operator <= (const Circle &c)  
{  
	return (R <= c.R);  
}
bool Circle::operator == (const Circle &c)  
{  
	return (R == c.R);  
}
bool Circle::operator != (const Circle &c)  
{  
	return (R != c.R);  
}

void crossover_point(Point &p1,Circle &c1, Point &p4,Point &p5)
{
	double x1 = p1.getx() - c1.getx();
	double y1 = p1.gety() - c1.gety();
	p4.setx((c1.R / c1.distance(p1) * x1) + c1.getx());
	p4.sety((c1.R / c1.distance(p1) * y1) + c1.gety());
	p5.setx(c1.getx() * 2 - p4.getx());
	p5.sety(c1.gety() * 2 - p4.gety());
}


int main( )
{
	Circle c1(3,2,4),c2(4,5,5);      //c2 c1
	
	Point p1(1,1),p2(3,-2),p3(7,3);  // c1 、 、 

	cout <<" c1: " << c1;
	cout << " p1: " << p1;
	
	cout << " p1 c1 " << ((locate(p1, c1)>0)?" ":((locate(p1, c1)<0)?" ":" ")) << endl;
	cout << " p2: " << p2;
	cout << " p2 c1 " << ((locate(p2, c1)>0)?" ":((locate(p2, c1)<0)?" ":" ")) << endl;	
	cout << " p3: " << p3;
	cout << " p3 c1 " << ((locate(p3, c1)>0)?" ":((locate(p3, c1)<0)?" ":" ")) << endl;
	cout << endl; 

	cout << " c1: " << c1 << " " << endl;
	if(c1 > c2) cout << "  ";
	if(c1 < c2) cout << "  "; 
	if(c1 >= c2) cout << "  ";
	if(c1 <= c2) cout << "  "; 
	if(c1 == c2) cout << "  "; 
	if(c1 != c2) cout << "  ";
	cout << endl;
	cout << " c2: " << c2 << " " << endl;
	cout << endl; 

	Point p4(1, 1) ,p5(1, 1);
	crossover_point(p1,c1, p4, p5);

	cout <<" p1: " << p1;
	cout << " c1: " << c1;
	cout << " , , :" << endl;
	cout << " : " << p4;
	cout << " : " << p5;
	cout << endl; 

	system("pause");
	return 0;
}

円c 1:(3,2)半径:4点p 1:(1,1)点p 1円c 1内点p 2:(3,-2)点p 2円c 1上点p 3:(7,3)点p 3円c 1外
円c 1:(3,2)半径:4の面積が円c 2:(4,5)の半径以下:5の面積
点p 1:(1,1)円c 1:(3,2)半径:4の円心に接続され、円と2点に交差し、それぞれ交点:(-0.577709,0.211146)交点:(6.5771,3.788885)
任意のキーを押して続行してください.