1. 程式人生 > >C++ 已知兩點座標和半徑求圓心座標程式

C++ 已知兩點座標和半徑求圓心座標程式

已知圓上的兩點座標和半徑,求圓心。

數學分析:這個題目,涉及到簡單的數學問題,但是計算比較繁瑣。
假設已知圓上的兩點座標分別為N(X1,Y1)和M(X2,Y2),半徑為R,圓心座標為o(a,b),根據數學知識可得到:
(x1-a)^2 + (y1-b)^2 = R^2----(1)式
(x2-a)^2 + (y2-b)^2 = R^2----(2)式
分別展開上述兩個式子得到
(x1)^2 - 2*x1*a + a^2 + (y1)^2 - 2*y1*b + b^2 = R^2   ----(3)式
(x2)^2 - 2*x2*a + a^2 + (y2)^2 - 2*y2*b + b^2 = R^2   ----(4)式
(3)式 -  (4)式
得到:
 x1^2 - x2^2 + 2*(x2-x1)*a + y1^2 - y2^2 + 2*(y2-y1)*b = 0
變形得到:
a = (x2^2 - x1^2 + y2^2 - y1^2)/2/(x2-x1) - (y2-y1)/(x2-x2) * b

設:C1 = (x2^2 - x1^2 + y2^2 - y1^2)/2/(x2-x1)
設:C2 = (y2-y1)/(x2-x2)

a = c1 - c2 * b   ----(5)式
把(5)式代入(1)式,得到;
x1^2 - 2*x1*(C1-C2*b) + (C1-C2*b)^2 + y1^2 -2*y1*b + b^2 = R^2
展開簡化為關於b的
一元二次方程
一般形式; (C2^2+1)*b^2 + (2*x1*C1-2*C1*C2-2*y1)*b + x1^2-2*x1*C1+C1^2+y1^2-R^2 = 0
得到求b的方程組

二次項係數:A = (C2^2+1)
一次項係數:B = (2*x1*C1-2*C1*C2-2*y1)
常數項:    C = x1^2-2*x1*C1+C1^2+y1^2-R^2
已知兩點座標和半徑求圓心座標程式  
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

ofstream fout;

typedef struct 
{
    double x;
    double y;
}Point;

double Y_Coordinates(double x,double y,double k,double x0);//4個引數
void Circle_Center(Point p1,Point p2,double dRadius);
bool Data_Validation(Point p1,Point p2,double dRadius);
void ReadData(Point &p1,Point &p2,double &dRadius);

void main()
{
    Point p1,p2;
    double dRadius = 0.0;

    ReadData(p1,p2,dRadius);

    fout.open("Result.txt");
    Circle_Center(p1,p2,dRadius);
    fout.close();
}

void Circle_Center(Point p1,Point p2,double dRadius)
{
    double k = 0.0,k_verticle = 0.0;
    double mid_x = 0.0,mid_y = 0.0;
    double a = 1.0;
    double b = 1.0;
    double c = 1.0;
    Point center1,center2;
    k = (p2.y - p1.y) / (p2.x - p1.x);
    if(k == 0)
    {
        center1.x = (p1.x + p2.x) / 2.0;
        center2.x = (p1.x + p2.x) / 2.0;
        center1.y = p1.y + sqrt(dRadius * dRadius -(p1.x - p2.x) * (p1.x - p2.x) / 4.0);
        center2.y = p2.y - sqrt(dRadius * dRadius -(p1.x - p2.x) * (p1.x - p2.x) / 4.0);
    }
    else
    {
        k_verticle = -1.0 / k;
        mid_x = (p1.x + p2.x) / 2.0;
        mid_y = (p1.y + p2.y) / 2.0;
        a = 1.0 + k_verticle * k_verticle;
        b = -2 * mid_x - k_verticle * k_verticle * (p1.x + p2.x);
        c = mid_x * mid_x + k_verticle * k_verticle * (p1.x + p2.x) * (p1.x + p2.x) / 4.0 - 
            (dRadius * dRadius - ((mid_x - p1.x) * (mid_x - p1.x) + (mid_y - p1.y) * (mid_y - p1.y)));
        
        center1.x = (-1.0 * b + sqrt(b * b -4 * a * c)) / (2 * a);
        center2.x = (-1.0 * b - sqrt(b * b -4 * a * c)) / (2 * a);
        center1.y = Y_Coordinates(mid_x,mid_y,k_verticle,center1.x);
        center2.y = Y_Coordinates(mid_x,mid_y,k_verticle,center2.x);
    }

    fout << center1.x << "    " << center1.y << endl;
    fout << center2.x << "    " << center2.y << endl;
}

double Y_Coordinates(double x,double y,double k,double x0)
{
    return k * x0 - k * x + y;
}

bool Data_Validation(Point p1,Point p2,double dRadius)
{
    double dDistance = 0.0;
    dDistance = sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
    if(dDistance == 0.0)
    {
        cout << "\n輸入了相同的點!\n";
        return false;
    }
    if((2 * dRadius) >= dDistance)
        return true;
    else
    {
        cout << "\n兩點間距離大於直徑!\n";
        return false;
    }
}

void ReadData(Point &p1,Point &p2,double &dRadius)
{
    cout << "請輸入圓周上一點的座標:";
    cin >> p1.x >> p1.y;
    cout << "請輸入圓周上另一點的座標:";
    cin >> p2.x >> p2.y;
    cout << "請輸入圓的半徑:";
    cin >> dRadius;

    if(! Data_Validation(p1,p2,dRadius))
    {
        cout << endl << "資料不合理!\n";
        exit(0);
    }
}

相關推薦

C++ 兩點座標半徑圓心座標程式

已知圓上的兩點座標和半徑,求圓心。 數學分析:這個題目,涉及到簡單的數學問題,但是計算比較繁瑣。 假設已知圓上的兩點座標分別為N(X1,Y1)和M(X2,Y2),半徑為R,圓心座標為o(a,b),根據數學知識可得到: (x1-a)^2 + (y1-b)^2 = R^2---

兩點座標半徑圓心座標程式C++

已知圓上的兩點座標和半徑,求圓心。數學分析:這個題目,涉及到簡單的數學問題,但是計算比較繁瑣。假設已知圓上的兩點座標分別為N(X1,Y1)和M(X2,Y2),半徑為R,圓心座標為o(a,b),根據數學知識可得到:(x1-a)^2 + (y1-b)^2 = R^2----(1

圓弧上兩點座標半徑圓心座標的演算法(C++)

#include<iostream> #include<math.h> using namespace std; void YuanXin(double x1,double y1,double x2,double y2,double R,double

兩點距離其中一點經緯度,另外一點經緯度

所提供的程式碼有些問題:但下面還有其他方法的連結: https://stackoverflow.com/questions/7222382/get-lat-long-given-current-point-distance-and-bearing  已知兩點經緯度,求兩點間距離

【筆記】圓上兩點座標半徑圓心

參考了一下這個博主的部落格:https://blog.csdn.net/liumoude6/article/details/78114255?locationNum=2&fps=1 已知兩點座標(x1, y1), (x2, y2)和半徑R,求圓心座標(x0, y0)。 程式設計

高德地圖---兩點經緯度座標距離角度

計算兩個座標點之間的距離 function calcDistance(lonA,latA,lonB,latB) { var earthR = 6371000; var x = Math

兩點的經度緯度,兩點間的距離(php,javascript)

turn lan span cti href script 返回 ng2 pan php代碼:轉載  http://www.cnblogs.com/caichenghui/p/5977431.html 1 /** 2 * 求兩個已知經緯度之間的距離,單位為米

C#起點座標、角度、長度終點座標(三角函式)

在一個CAD繪圖工具開發過程中需要根據起點座標、角度、長度求終點座標,作為一個數學渣來說,遇到這個問題真的感覺非常對不起初中數學老師,重新撿起三角函式學了兩天,當然,最後還是沒學太明白,但是還是把這個方法寫出來了,真的太簡單了,這種初級的數學知識都不會,我真的感覺很羞愧,想想還是記錄一下吧,

C# 圓心兩點,用DrawArc()畫圓弧(演算法)

            //oa和X軸上向量的點乘積             int Point_Mul_a = (Vector_ax * Vector_Xx) + (Vector_ay * Vector_Xy);             double Mul_a = Math.Sqrt(Vector_ax

兩點座標,及在從其中一點開始移動的距離,移動到的座標

    //兩點間距離       double dis = Math.pow(MathUtil.getDistanceSquare(list.get(i + 1).x, list.get(i + 1).y, list.get(i).x, list.get(i).y), 0

Codeforce 459A - Pashmak and Garden (兩點另外兩點構成正方形)

like tty cos mod ner pro tin content ini Pashmak has fallen in love with an attractive girl called Parmida since one year ago... To

數列的的增幅,起始列結束列,中間階梯數

圖片 分享圖片 公式 結束 com bubuko http nbsp 技術分享 求數列的的增幅,已知起始列和結束列,中間階梯數 已知 n1=2 n2=100 階梯=4 上面4個空列 每列增幅多少,正好填到100? 公式 (n2-n1)/(階梯+1) 為什

一點經緯度距離,方位角;另外一點的經緯度

經緯度 經緯 ant cat cse rpo pass quest red It seems you are measuring distance (R) in meters, and bearing (theta) counterclockwise from due e

前序中序遍歷二叉樹

描述 輸入某二叉樹的前序遍歷和中序遍歷的結果,請輸出後序遍歷序列。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},重建二叉樹並返回後序遍歷序列 輸入 輸入

C#兩天日期之間每一天日期字串集合

問題描述:         在《C#判斷判斷某一時刻屬於什麼時間段》中提到的訂單處理系統中,有這麼一個需求,就是根據使用者選擇的兩個日期,去mdb中查詢在這連個日期之間的每一天的相關資訊,故需要用每一天的日期字串來拼接sql語句。 解決方法:

C# 三點 三點之間夾角角度

public static double Angle(Point cen, Point first, Point second)   {    &nb

安卓(JAVA)兩點經緯度,出一條線上幾等分的點的經緯度。

話不多說,直接上程式碼。 /** * 計算兩點之間等距的經緯度 */ private List<LatLng> getDengLatLng(int number) { double aaa, bbb, ccc = 0

1119. Pre- and Post-order Traversals (30)【前序後序中序】

1119. Pre- and Post-order Traversals (30) 時間限制 400 ms 記憶體限制 65536 kB

1138. Postorder Traversal (25)【前序中序後序】

1138. Postorder Traversal (25) 時間限制 600 ms 記憶體限制 65536 kB 程式碼長度

置信區間(樣本均值樣本的方差,總體均值的置信區間)(n < 30)

當樣本很小時 X¯¯¯服從T分佈 T ~ t(v) 樣本的數量為n時,v = n-1 T = (X¯¯¯ - μ)/(s/n√) 與上篇文章的置信區間相似,只不過c換成了t 置信區間取值範圍為(X¯¯¯ - t(v)*s/n√, X¯¯¯ + t