在 C++ 中使用直徑的端點查詢圓心


假設我們有兩個圓的直徑端點。它們是 (x1, y1) 和 (x2, y2),我們需要找到圓心。所以如果兩個點是 (-9, 3) 和 (5, -7),那麼圓心位於 (-2, -2) 位置。

我們知道兩點的中點是 -

$$(x_{m},y_{m})=\left(\frac{(x_{1}+x_{2})}{2},\frac{(y_{1}+y_{2})}{2}\right)$$

示例

 線上演示

#include<iostream>
using namespace std;
class point{
   public:
      float x, y;
      point(float x, float y){
         this->x = x;
         this->y = y;
      }
      void display(){
         cout << "(" << x << ", " <<y<<")";
      }
};
point center(point p1, point p2) {
   int x, y;
   x = (float)(p1.x + p2.x) / 2;
   y = (float)(p1.y + p2.y) / 2;
   point res(x, y);
   return res;
}
int main() {
   point p1(-9.0, 3.0), p2(5.0, -7.0);
   point res = center(p1, p2);
   cout << "Center is at: ";
   res.display();
}

輸出

Center is at: (-2, -2)

更新於: 2019年12月19日

141 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告