Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- #include <cstdlib>
- /*
- * Developed by Penweng.
- */
- using namespace std;
- int main(int argc, char **argv){
- float coordinate[6] = {0}; //座標 x1 y1 x2 y2 x3 y3
- for(int i = 0; i < 6; i++){
- cin >> coordinate[i];
- }
- cout << "your coordinate";
- for(int i = 0; i < 6; i = i+2){
- cout << "(" << coordinate[i] << "," << coordinate[i+1] << ") ";
- }
- //檢查錯誤
- if(coordinate[0]+coordinate[2] == 0 || coordinate[1]+coordinate[3] == 0){
- // 位置 2(x2) 3(y2) 跟 4(x3) 5(y3) 對調
- /* float tempx2 = coordinate[2];
- float tempy2 = coordinate[3];
- coordinate[2] = coordinate[4];
- coordinate[3] = coordinate[5];
- coordinate[4] = tempx2;
- coordinate[5] = tempy2; */
- }
- //1-1 求x1y1 x2y2線段中垂線方程式
- // x1y1 x2y2 中點
- float centerPoint1[2] = {0};
- centerPoint1[0] = ((coordinate[0]+coordinate[2])/2.0);
- centerPoint1[1] = ((coordinate[1]+coordinate[3])/2.0);
- // x1y1 x2y2 斜率 (又垂線斜率相乘=-1)
- float slopeOrigin = ((coordinate[1]-coordinate[3])/(coordinate[0]-coordinate[2]));
- if(slopeOrigin == 0.0){
- //斜率為0 圓心在中垂線上
- //令圓心O(t, 0) A(x1, y1) B(x3, y3)
- // OA = OB
- // t-x1^2 + y1^2 = t-x3^2 + y3^2
- // t-x1^2 - t-x3^2 = y3^2 - y1^2
- float t = 2*coordinate[2]; // = pow(orrdinate[5],2) - pow(orrdinate[2],2);
- float ans = ((pow(coordinate[5],2)) - (pow(coordinate[2],2)))/t;
- cout << "圓心 (" << ans << " , 0 )";
- return 1;
- }
- float verticalSlope1 = -1.0/slopeOrigin;
- // y = mx + k 求k
- float k1 = -(verticalSlope1*centerPoint1[0])+centerPoint1[1];
- //1-2 求x1y1 x3y3線段中垂線方程式
- // x1y1 x3y3 中點
- float centerPoint2[2] = {0};
- centerPoint2[0] = ((coordinate[0]+coordinate[4])/2.0);
- centerPoint2[1] = ((coordinate[1]+coordinate[5])/2.0);
- // x1y1 x3y3 斜率 (又垂線斜率相乘=-1)
- slopeOrigin = ((coordinate[1]-coordinate[5])/(coordinate[0]-coordinate[4]));
- if(slopeOrigin == 0.0){
- //斜率為0 圓心在中垂線上
- //令圓心O(t, 0) A(x1, y1) B(x3, y3)
- // OA = OB
- // t-x1^2 + y1^2 = t-x3^2 + y3^2
- // t-x1^2 - t-x3^2 = y3^2 - y1^2
- float t = 2*coordinate[2]; // = pow(orrdinate[5],2) - pow(orrdinate[2],2);
- float ans = ((pow(coordinate[5],2)) - (pow(coordinate[2],2)))/t;
- cout << "圓心 (" << ans << " , 0 )";
- return 1;
- }
- float verticalSlope2 = -1.0/slopeOrigin;
- // y = mx + k 求k
- float k2 = -(verticalSlope2*centerPoint2[0])+centerPoint2[1];
- //解聯立求兩中垂線方程式交點
- // equ1 => y1 = verticalSlope1*x1 + k1 ==> y1 - verticalSlope1*x1 = k1
- // equ2 => y2 = verticalSlope2*x2 + k2 ==> y2 - verticalSlope2*x2 = k2
- // y1 x1 y2 x2 is unknown
- // eg
- // y = 6x + 9
- // y = 3x + 4
- // 3 5
- float equAnsX = (k1-k2)/(verticalSlope1-verticalSlope2);
- float equAnsY = equAnsX*verticalSlope1 - k1;
- cout << "圓心 (" << -equAnsX << "," << -equAnsY << ")";
- //求r h-x1^2 + k-y1^2 then sqrt
- float destination = sqrt(pow(-equAnsX-coordinate[0],2) + pow(-equAnsY-coordinate[1],2));
- cout << "半徑 " << destination << " 也等於根號" << pow(-equAnsX-coordinate[0],2) + pow(-equAnsY-coordinate[1],2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement