โจทย์
ให้ปลายเส้นผ่านศูนย์กลางของวงกลม 2 จุด (x1,y1) และ (x2,y2) ต้องการหาจุดศูนย์กลาง
สูตรจุดกึ่งกลาง: ((x1+x2)/2, (y1+y2)/2)
ตัวอย่าง: (-9,3) และ (5,-7) → จุดศูนย์กลาง (-2,-2)
ตัวอย่างโค้ด C++
#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 = (float)(p1.x + p2.x) / 2;
int 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();
}
ผลลัพธ์
Center is at: (-2, -2)