# Geometry# Basic Math# Coordinate Geometry

e512 - 10242 - Fourth Point!!

🔗 前往 ZeroJudge 原題

題目描述

題目給定平行四邊形的兩個相鄰邊的端點座標,要求計算出第四個頂點的座標。平行四邊形的邊由輸入的八個浮點數定義,分別代表兩個相鄰邊的兩個端點的 x 和 y 座標。

解題思路

平行四邊形的對邊平行且相等。因此,第四個頂點的座標可以通過向量加法來計算。設給定的點分別為 A(a, b), B(c, d), C(e, f), D(g, h)。 如果 A 和 B 是相鄰的,C 和 D 也是相鄰的,那麼第四個點 P 的座標可以通過以下公式計算:P = A + C - B 或者 P = B + D - A。 程式碼中,首先判斷輸入的點是否需要交換,以確保計算的正確性。然後,根據平行四邊形的性質,計算出第四個頂點的座標,並輸出結果。

複雜度分析

  • 時間複雜度: O(1)
  • 空間複雜度: O(1)

程式碼

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
	cin.tie(0); ios::sync_with_stdio(false);
	double a,b,c,d,e,f,g,h;
	while(cin >> a >> b >> c >> d >> e >> f >> g >> h){
		if(a==e&&b==f){
			float tmp=a;a=c;c=tmp;
			tmp=b;b=d;d=tmp;
		}
		else if(c==g&&d==h){
			float tmp=e;e=g;g=tmp;
			tmp=f;f=h;h=tmp;
		}
		else if(a==g&&b==h){
			float tmp=a;a=c;c=tmp;
			tmp=b;b=d;d=tmp;
			tmp=e;e=g;g=tmp;
			tmp=f;f=h;h=tmp;
		}
		cout<<fixed<<setprecision(3)<< a+g-c << " " << b+h-d<<'\n';
	}
}

Discussion