กำหนด Midpoint 3 จุดของด้านสามเหลี่ยม หาพิกัดยอดสามเหลี่ยมเดิม
ตัวอย่าง: Midpoints (5,3), (4,4), (5,5) → Vertices (4,2), (4,6), (6,4)
หลักการทางคณิตศาสตร์
ให้ยอดเป็น (x1,y1), (x2,y2), (x3,y3)
Midpoints: (x1+x2)/2, (x2+x3)/2, (x3+x1)/2
ผลรวม Midpoint X = (x1+x2 + x2+x3 + x3+x1)/2 = x1+x2+x3
→ x1 = Sum - 2×Mid2, x2 = Sum - 2×Mid3, x3 = Sum - 2×Mid1
ทำ Y เหมือนกัน
ตัวอย่างโค้ด
#include <iostream> #include <vector> #define N 3 using namespace std; vectorgetResult(int v[]) { vector res; int sum = v[0] + v[1] + v[2]; res.push_back(sum - 2*v[1]); // x1 res.push_back(sum - 2*v[2]); // x2 res.push_back(sum - 2*v[0]); // x3 return res; } void findVertices(int mid_x[], int mid_y[]) { vector x_vals = getResult(mid_x); vector y_vals = getResult(mid_y); for (int i = 0; i < 3; i++) cout << "(" << x_vals[i] << ", " << y_vals[i] << ")" << endl; } int main() { int mid_x[N] = {5, 4, 5}; int mid_y[N] = {3, 4, 5}; findVertices(mid_x, mid_y); return 0; }
ผลลัพธ์
(6, 4) (4, 2) (4, 6)ตรวจสอบ: Midpoint ของ (6,4)-(4,2) = (5,3) ✓ Midpoint ของ (4,2)-(4,6) = (4,4) ✓ Midpoint ของ (4,6)-(6,4) = (5,5) ✓
ความซับซ้อน
เวลา: O(1), พื้นที่: O(1)