【題解】ZeroJudge e512: 10242 – Fourth Point!!

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e512
【解題想法】

  • 題目給定平行四邊形的「兩個相鄰邊」的端點的 (x, y)座標,這四個端點中必有兩個座標是同一點。
  • 決定三個相異的座標點後,即可決定第四點的座標。
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double x[4], y[4];
    double x1, y1, x2, y2, x3, y3;
    while (cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2] >> x[3] >> y[3]){
        if (x[1] == x[2] && y[1] == y[2]){
            x1 = x[0]; y1 = y[0];
            x2 = x[1]; y2 = y[1];
            x3 = x[3]; y3 = y[3];
        } else if (x[0] == x[2] && y[0] == y[2]){
            x1 = x[1]; y1 = y[1];
            x2 = x[0]; y2 = y[0];
            x3 = x[3]; y3 = y[3];
        } else if (x[0] == x[3] && y[0] == y[3]){
            x1 = x[1]; y1 = y[1];
            x2 = x[0]; y2 = y[0];
            x3 = x[2]; y3 = y[2];
        } else if (x[1] == x[3] && y[1] == y[3]){
            x1 = x[0]; y1 = y[0];
            x2 = x[1]; y2 = y[1];
            x3 = x[2]; y3 = y[2];
        }
        cout << fixed << setprecision(3)
        << x3 + x1 - x2 << " " << y3 + y1 - y2 << "\n";
    }
    return 0;
}
分享本文 Share with friends