Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4. #include <cstdlib>
  5.  
  6.  
  7. /*
  8.  * Developed by Penweng.
  9.  */
  10.  
  11. using namespace std;
  12.  
  13. int main(int argc, char **argv){
  14.     float coordinate[6] = {0}; //座標 x1 y1 x2 y2 x3 y3
  15.     for(int i = 0; i < 6; i++){
  16.         cin >> coordinate[i];
  17.     }
  18.     cout << "your coordinate";
  19.     for(int i = 0; i < 6; i = i+2){
  20.         cout << "(" << coordinate[i] << "," << coordinate[i+1] << ") ";
  21.     }
  22.  
  23.    
  24.     //檢查錯誤
  25.     if(coordinate[0]+coordinate[2] == 0 || coordinate[1]+coordinate[3] == 0){
  26.         // 位置 2(x2) 3(y2) 跟 4(x3) 5(y3) 對調
  27.     /*  float tempx2 = coordinate[2];
  28.         float tempy2 = coordinate[3];
  29.         coordinate[2] = coordinate[4];
  30.         coordinate[3] = coordinate[5];
  31.         coordinate[4] = tempx2;
  32.         coordinate[5] = tempy2; */ 
  33.     }
  34.    
  35.    
  36.    
  37.     //1-1 求x1y1 x2y2線段中垂線方程式
  38.     // x1y1 x2y2 中點
  39.     float centerPoint1[2] = {0};
  40.     centerPoint1[0] = ((coordinate[0]+coordinate[2])/2.0);
  41.     centerPoint1[1] = ((coordinate[1]+coordinate[3])/2.0);
  42.    
  43.    
  44.     // x1y1 x2y2 斜率 (又垂線斜率相乘=-1)
  45.     float slopeOrigin = ((coordinate[1]-coordinate[3])/(coordinate[0]-coordinate[2]));
  46.     if(slopeOrigin == 0.0){
  47.         //斜率為0 圓心在中垂線上
  48.         //令圓心O(t, 0) A(x1, y1) B(x3, y3)
  49.         // OA = OB
  50.         // t-x1^2 + y1^2 = t-x3^2 + y3^2
  51.         // t-x1^2 - t-x3^2 = y3^2 - y1^2
  52.        
  53.         float t = 2*coordinate[2]; // = pow(orrdinate[5],2) - pow(orrdinate[2],2);
  54.         float ans = ((pow(coordinate[5],2)) - (pow(coordinate[2],2)))/t;
  55.         cout << "圓心 (" << ans << " , 0 )";
  56.         return 1;
  57.     }
  58.     float verticalSlope1 = -1.0/slopeOrigin;
  59.     // y = mx + k 求k
  60.     float k1 =  -(verticalSlope1*centerPoint1[0])+centerPoint1[1];
  61.    
  62.  
  63.     //1-2 求x1y1 x3y3線段中垂線方程式
  64.     // x1y1 x3y3 中點
  65.     float centerPoint2[2] = {0};
  66.     centerPoint2[0] = ((coordinate[0]+coordinate[4])/2.0);
  67.     centerPoint2[1] = ((coordinate[1]+coordinate[5])/2.0);
  68.     // x1y1 x3y3 斜率 (又垂線斜率相乘=-1)
  69.     slopeOrigin = ((coordinate[1]-coordinate[5])/(coordinate[0]-coordinate[4]));
  70.  
  71.     if(slopeOrigin == 0.0){
  72.         //斜率為0 圓心在中垂線上
  73.         //令圓心O(t, 0) A(x1, y1) B(x3, y3)
  74.         // OA = OB
  75.         // t-x1^2 + y1^2 = t-x3^2 + y3^2
  76.         // t-x1^2 - t-x3^2 = y3^2 - y1^2
  77.        
  78.         float t = 2*coordinate[2]; // = pow(orrdinate[5],2) - pow(orrdinate[2],2);
  79.         float ans = ((pow(coordinate[5],2)) - (pow(coordinate[2],2)))/t;
  80.         cout << "圓心 (" << ans << " , 0 )";
  81.         return 1;
  82.     }
  83.     float verticalSlope2 = -1.0/slopeOrigin;
  84.     // y = mx + k 求k
  85.     float k2 =  -(verticalSlope2*centerPoint2[0])+centerPoint2[1];
  86.      
  87.      
  88.    
  89.     //解聯立求兩中垂線方程式交點
  90.     // equ1 => y1 = verticalSlope1*x1 + k1 ==> y1 - verticalSlope1*x1 = k1
  91.     // equ2 => y2 = verticalSlope2*x2 + k2 ==> y2 - verticalSlope2*x2 = k2
  92.    
  93.     // y1 x1 y2 x2 is unknown
  94.     // eg
  95.     // y = 6x + 9
  96.     // y = 3x + 4
  97.    
  98.    
  99.     // 3 5
  100.     float equAnsX = (k1-k2)/(verticalSlope1-verticalSlope2);
  101.     float equAnsY = equAnsX*verticalSlope1 - k1;
  102.    
  103.     cout << "圓心 (" << -equAnsX << "," << -equAnsY << ")";
  104.    
  105.    
  106.     //求r h-x1^2 + k-y1^2 then sqrt
  107.    
  108.     float destination =  sqrt(pow(-equAnsX-coordinate[0],2) + pow(-equAnsY-coordinate[1],2));
  109.     cout << "半徑 " << destination << " 也等於根號" << pow(-equAnsX-coordinate[0],2) + pow(-equAnsY-coordinate[1],2);  
  110.  
  111.    
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement