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

Tìm điểm cuối đoạn thẳng biết một đầu và trung điểm bằng C++

Mô tả bài toán

Cho điểm A(x1,y1) và trung điểm M(xm,ym). Tìm điểm B(x2,y2).

Công thức

x2 = 2*xm - x1; y2 = 2*ym - y1.

Ví dụ

A=(1,2), M=(3,0) → B=(5,-2).

Mã nguồn C++

#include <iostream>
using namespace std;

void findMissingPointLine(float x1, float y1, float xm, float ym) {
    float x2 = 2*xm - x1;
    float y2 = 2*ym - y1;
    cout << "B(x, y) = (" << x2 << ", " << y2 << ")";
}

int main() {
    float x1=-4, y1=-1, xm=3, ym=5;
    cout << "The other end point of the line is:\n";
    findMissingPointLine(x1, y1, xm, ym);
    return 0;
}

Đầu ra

The other end point of the line is B(x, y) = ( 10, 11 )