Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Tìm tâm đường tròn từ hai đầu đường kính trong C++


Giả sử có hai đầu đường kính của đường tròn: (x1, y1) và (x2, y2), tìm tâm đường tròn. Nếu hai điểm (-9, 3) và (5, -7), tâm tại (-2, -2).

Biết trung điểm của hai điểm:

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

Ví dụ mã nguồn

#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(); }

Kết quả

Center is at: (-2, -2)