第十三週--圓外一點與圓心相連的直線與圓的交點
阿新 • • 發佈:2019-02-15
/* *程式的版權和版本宣告部分: *Copyright(c)2013,煙臺大學計算機學院學生 *All rights reserved. *檔名稱: *作者:尚振偉 *完成日期:2014年5月19日 *版本號:v0.1 *對任務及求解方法的描述部分: *輸入描述:無 *問題描述:1)先建立一個Point(點)類,包含資料成員x,y(座標點); (2)以Point為基類,派生出一個Circle(圓)類,增加資料成員(半徑),基類的成員表示圓心; (3)編寫上述兩類中的構造、解構函式及必要運算子過載函式(本專案主要是輸入輸出); (4)定義友元函式計算圓外一點雨圓心的連線與圓的交點。 *程式輸入: *程式輸出: *問題分析: *演算法設計: *我的程式: */ #include <iostream> #include <Cmath> using namespace std; class Circle; class Point { protected: double x; double y; public: Point():x(0),y(0) {} Point(double xx,double yy):x(xx),y(yy) {} double get_x() { return x; } double get_y() { return y; } friend ostream & operator<<(ostream &,const Point &); friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 ); }; ostream & operator<<(ostream &output,const Point &p) { output<<"("<<p.x<<","<<p.y<<")"<<endl; return output; } class Circle:public Point { private: double r; public: Circle(double a,double b,double c):Point(a,b),r(c) {} double getr() { return r; } friend ostream &operator<<(ostream &output,const Circle &c); friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 ) ; }; ostream &operator<<(ostream &output,const 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( ) { Circle c1(5,2,8); //c2應該大於c1 Point p1(3,7),p2,p3; //分別位於c1內、上、外 cout<<"圓c1: "<<c1; cout<<"點p1: "<<p1; crossover_point(p1,c1, p2, p3); cout<<"的圓心相連,與圓交於兩點,分別是:"<<endl; cout<<"交點1: "<<p2; cout<<"交點2: "<<p3; return 0; }
結果展示:
心得體會:有點小麻煩。。。。