/*
02.*烟台大学计算机学院学生
03.*All right reserved.
04.*文件名称*烟台大学计算机学院学生
05.*All right reserved.
06.*文件名称:与圆心相连的直线
07.*作者:王洪海
08.*完成日期:2013年5月25日
09.*版本号:v1.0
10.*对任务及求解方法的描述部分:与圆心相连的直线
11.*我的程序:
12.*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Circle; //由于在Point中声明友元函数crossover_point中参数中用了Circle,需要提前声明
class Point
{
protected:
double x;
double y;
public:
Point(double x0=0,double y0=0):x(x0),y(y0){}
~Point(){}
double getx()
{
return x;
}
double gety()
{
return y;
}
friend ostream& operator <<(ostream &,Point &);
friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 );
};
ostream& operator <<(ostream &output,Point &p)
{
output<<"("<<p.x<<","<<p.y<<")"<<endl;
return output;
}
class Circle:public Point
{
protected:
double r;
public:
Circle(double x0=0,double y0=0,double r0=0):
Point(x0,y0),r(r0){}
~Circle(){}
friend ostream& operator <<(ostream &,Circle &);
friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 );
};
ostream& operator <<(ostream &output,Circle &c)
{
output<<"圆心为:("<<c.x<<","<<c.y<<") 圆的半径为:"<<c.r<<endl;
return output;
}
void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 )
{
p1.x = (c.x + sqrt(c.r*c.r/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
p2.x = (c.x - sqrt(c.r*c.r/(1+((c.y-p.y)/(c.x-p.x))*((c.y-p.y)/(c.x-p.x)))));
p1.y = (p.y + (p1.x -p.x)*(c.y-p.y)/(c.x-p.x));
p2.y = (p.y + (p2.x -p.x)*(c.y-p.y)/(c.x-p.x));
}
int main( )
{
Point p(1,1),p1,p2;
Circle c(3,2,4);
crossover_point(p,c,p1,p2);
cout<<"点p: "<<p<<endl;
cout<<"与圆c: "<<c<<endl;
cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;
cout<<"交点1: "<<p1<<endl;
cout<<"交点2: "<<p2<<endl;
return 0;
}
运行结果,如下图: