fork download
  1. #include<iostream>
  2. #include<math.h>
  3. #include<sstream>
  4. #include<time.h>
  5. #define ss stringstream
  6. using namespace std;
  7. int gcd(int m, int n) { // 最大公因數
  8. if(n == 0)
  9. return m;
  10. else
  11. return gcd(n, m % n);
  12. }
  13. bool Tangent_exist(float x, float y, float h, float k, float rsq){ //判斷切線存在
  14. if((float)(pow(x-h,2)+pow(y-k,2) - rsq) >= 0)
  15. return true;
  16. else
  17. return false;
  18. }
  19. void solve(float x, float y, float h, float k ,float rsq){ // 解方程式
  20. float OPx = x-h, OPy = y-k, PA = sqrt(pow(x-h,2) + pow(y-k,2) - rsq), r = sqrt(rsq);
  21. float OPxO = OPx + (PA/r) * -(OPy), OPyO = OPy + (PA/r) * OPx, OPxN = OPx - (PA/r) * -(OPy), OPyN = OPy - (PA/r) * OPx;
  22. OPxO *= 1e5; OPyO *= 1e5; OPxN *= 1e5; OPyN *= 1e5; //避免小數
  23. ss S_OPxO, S_OPyO, S_OPxN, S_OPyN;
  24. string operator1 = "",operator2= "";
  25. if(OPyO > 0)
  26. operator1 = "+";
  27. if(OPyN > 0)
  28. operator2 = "+";
  29. int factor1 = gcd(abs(OPxO),abs(OPyO)), factor2 = gcd(abs(OPxN), abs(OPyN));
  30. OPxO /= factor1; OPyO /= factor1; OPxN /= factor2; OPyN /= factor2;//約分
  31. string tempx1 = OPxO == 0? "": "x", tempx2 = OPxN == 0? "":"x", tempy1 = OPyO == 0? "":"y", tempy2 = OPyN == 0? "":"y";
  32. int ans1= OPxO*x + OPyO*y, ans2 = OPxN * x + OPyN * y;
  33. S_OPxO << OPxO; S_OPyO << OPyO; S_OPxN << OPxN; S_OPyN << OPyN;//int -> string 以方便修改運算子、係數
  34. if(OPxO == 0 || OPxO == 1)
  35. S_OPxO.str(""); S_OPxO.clear();
  36. if(OPyO == 0 || OPyO == 1)
  37. S_OPyO.str(""); S_OPyO.clear();
  38. if(OPxN == 0 || OPxN == 1)
  39. S_OPxN.str(""); S_OPxN.clear();
  40. if(OPyN == 0 || OPyN == 1)
  41. S_OPyN.str(""); S_OPyN.clear();
  42. if(OPxO == OPxN){
  43. cout << "1條" << endl;
  44. cout << S_OPxO.str() << tempx1 << operator1 << S_OPyO.str() << tempy1 << "=" << ans1 << endl;
  45. }
  46. else{
  47. cout << "2條" << endl;
  48. cout << S_OPxO.str() << tempx1 << operator1 << S_OPyO.str() << tempy1 << "=" << ans1 << endl;
  49. cout << S_OPxN.str() << tempx2 << operator2 << S_OPyN.str() << tempy2 << "=" << ans2 << endl;
  50. }
  51. }
  52. int main(){
  53. double START,END;
  54. START = clock();
  55. std::ios::sync_with_stdio(false);
  56. std::cin.tie(0);
  57. float type, h, k, rsq, d, e, f, x, y;
  58. cout << "請選擇圓方程式類式\n1.一般式 x^2+y^2+dx+ey+f=0\n2.標準式(x-h)^2+(y-k)^2=r^2" << endl;
  59. cin >> type;
  60. if(type == 1){
  61. cout << "請輸入 x^2+y^2+dx+ey+f=0 之 (d,e,f)" << endl;
  62. cout << "d : "; cin >> d;
  63. cout << "e : "; cin >> e;
  64. cout << "f : "; cin >> f;
  65. h = -(d/2);
  66. k = -(e/2);
  67. rsq = (pow(d,2) + pow(e,2) - 4 * f) / 4; // R^2 rsquare
  68. }
  69. else{
  70. cout << "請輸入 (x-h)^2+(y-k)^2=r^2 之 (h,k,r^2)" << endl;
  71. cout << "h : "; cin >> h;
  72. cout << "k : "; cin >> k;
  73. cout << "r^2 : "; cin >> rsq;
  74. }
  75. cout << "請輸入點座標(x,y)" << endl;
  76. cout << "x : "; cin >> x;
  77. cout << "y : "; cin >> y;
  78. if(Tangent_exist(x,y,h,k,rsq)){
  79. cout << "切線為:";
  80. solve(x,y,h,k,rsq);
  81. }
  82. else{
  83. cout << "切線不存在" << endl;
  84. }
  85. END = clock();
  86. cout <<"執行時間 : " << (END - START) / CLOCKS_PER_SEC << "s"<< endl;
  87. }
Success #stdin #stdout 0s 4312KB
stdin
1 -8 -6 20 1 2
stdout
請選擇圓方程式類式
1.一般式 x^2+y^2+dx+ey+f=0
2.標準式(x-h)^2+(y-k)^2=r^2
請輸入 x^2+y^2+dx+ey+f=0 之 (d,e,f)
d : e : f : 請輸入點座標(x,y)
x : y : 切線為:2條
-1x-2y=-5
-2x+y=0
執行時間 : 0.000119s