C++写的点线面之间的关系
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 class Point 6 { 7 public: 8 Point(){} 9 Point(int x, int y){pointX = x; pointY = y;} 10 //Point(Point &p){pointX = p.pointX; pointY = p.pointY;} 11 void setPointValue(); 12 float getX(){return pointX;} 13 float getY(){return pointY;} 14 private: 15 float pointX; 16 float pointY; 17 }; 18 19 void Point::setPointValue() 20 { 21 cout << "请输入点的坐标<x, y>: "; 22 cin >> pointX >> pointY; 23 } 24 25 class Line 26 { 27 public: 28 Line(){} 29 Line(Point p, float x):point(p), k(x){} 30 Point getPoint(){return point;} 31 float getK(){return k;} 32 void setLineValue(); 33 private: 34 Point point; 35 float k; 36 }; 37 38 void Line::setLineValue() 39 { 40 point.setPointValue(); 41 cout << "请输入直线的斜率k的值: "; 42 cin >> k; 43 } 44 45 46 class Circle 47 { 48 public: 49 Circle(){} 50 Circle(Point p, float r):point(p), Radius(r){} 51 Point getPoint(){return point;} 52 float getRadius(){return Radius;} 53 void setCircleValue(); 54 public: 55 Point point; 56 float Radius; 57 }; 58 59 void Circle::setCircleValue() 60 { 61 point.setPointValue(); 62 cout << "请输入圆的半径的值: "; 63 cin >> Radius; 64 } 65 66 class OptRelation 67 { 68 public: 69 OptRelation(){} 70 void setValue(); 71 void LinesRelation(); 72 void LineCircleRelation(); 73 void CirclesRelation(); 74 void PointCircleRelation(); 75 private: 76 Point pValue; 77 Line lValue; 78 Line lValue2; 79 Circle cValue; 80 Circle cValue2; 81 }; 82 83 void OptRelation::setValue() 84 { 85 cout << "请输入点的信息" << endl; 86 pValue.setPointValue(); 87 cout << "请输入第一条直线的信息" << endl; 88 lValue.setLineValue(); 89 cout << "请输入第二条直线的信息" << endl; 90 lValue2.setLineValue(); 91 cout << "请输入第一个圆的信息" << endl; 92 cValue.setCircleValue(); 93 cout << "请输入第二个圆的信息" << endl; 94 cValue2.setCircleValue(); 95 } 96 97 void OptRelation::LinesRelation() 98 { 99 if (lValue.getK() * lValue2.getK() == -1) 100 cout << "两条直线垂直" << endl; 101 else if (lValue.getK() == lValue2.getK()) 102 cout << "两条直线平行" << endl; 103 else 104 cout << "两条直线相交" << endl; 105 } 106 107 void OptRelation::LineCircleRelation() 108 { 109 float c = lValue.getPoint().getY() - lValue.getK() * lValue.getPoint().getX(); 110 float distance = abs( lValue.getK() * cValue.getPoint().getX() - cValue.getPoint().getY() + c) / (float)sqrt(lValue.getK() * lValue.getK() + 1); 111 112 if (distance == cValue.getRadius()) 113 cout << "直线与圆相切" << endl; 114 if (distance < cValue.getRadius()) 115 cout << "直线与圆相交" << endl; 116 else 117 cout << "直线与圆相离" << endl; 118 } 119 120 void OptRelation::CirclesRelation() 121 { 122 float yDistPow = (cValue2.getPoint().getY()- cValue.getPoint().getY()) * (cValue2.getPoint().getY() - cValue.getPoint().getY()); 123 float xDistPow = (cValue2.getPoint().getX() - cValue.getPoint().getX()) * (cValue2.getPoint().getX() - cValue.getPoint().getX()); 124 float pointDist = sqrt(yDistPow + xDistPow); 125 bool pointEqual = (cValue.getPoint().getX() == cValue2.getPoint().getX()) && (cValue.getPoint().getY() == cValue2.getPoint().getY() ); 126 if ( pointDist == (cValue.getRadius() + cValue2.getRadius()) ) 127 cout << "两圆相切" << endl; 128 else if( pointDist < (cValue.getRadius() + cValue2.getRadius()) ) 129 cout << "两圆相交" << endl; 130 else if ((pointDist == cValue.getRadius()) && ( pointEqual == true )) 131 cout << "两圆重叠" << endl; 132 else 133 cout << "两圆相离" << endl; 134 } 135 136 void OptRelation::PointCircleRelation() 137 { 138 float xDistPow = (pValue.getX() - cValue.getPoint().getX()) * (pValue.getX() - cValue.getPoint().getX()); 139 float yDistPow = (pValue.getY() - cValue.getPoint().getY()) * (pValue.getY() - cValue.getPoint().getY()); 140 float pointDist = sqrt(xDistPow + yDistPow); 141 142 if (pointDist == cValue.getRadius()) 143 cout << "点在圆上" << endl; 144 else if (pointDist < cValue.getRadius()) 145 cout << "点在圆内" << endl; 146 else 147 cout << "点在圆外" << endl; 148 } 149 150 151 152 int main(void) 153 { 154 OptRelation opt; 155 opt.setValue(); 156 opt.CirclesRelation(); 157 opt.LineCircleRelation(); 158 opt.LinesRelation(); 159 opt.PointCircleRelation(); 160 161 return 0; 162 }
posted on 2013-06-13 15:45 smileleeboo 阅读(764) 评论(0) 编辑 收藏 举报