C++程式:求三角形的重心
在這個問題中,我們給定了一個二維陣列,表示三角形三個頂點的座標。我們的任務是建立一個 C++ 程式來找到三角形的重心。
重心是三角形三條中線交點。
中線是連線三角形頂點與其對邊中點的線段。

讓我們舉個例子來理解這個問題,
輸入
(-3, 1), (1.5, 0), (-3, -4)
輸出
(-3.5, -1)
解釋
Centroid (x, y) = ((-3+2.5-3)/3, (1 + 0 - 4)/3) = (-3.5, -1)
解決方案方法
為了解決這個問題,我們將使用三角形重心的幾何公式。
對於點 (ax, ay), (bx, by), (cx, cy)
Centroid, x = (ax + bx + cx) / 3 y = (ay + by + cy) / 3
程式演示解決方案的工作原理,
示例
#include <iostream>
using namespace std;
int main() {
float tri[3][2] = {{-3, 1},{1.5, 0},{-3, -4}};
cout<<"Centroid of triangle is (";
cout<<((tri[0][0]+tri[1][0]+tri[2][0])/3)<<" , ";
cout<<((tri[0][1]+tri[1][1]+tri[2][1])/3)<<")";
return 0;
}輸出
Centroid of triangle is (-1.5 , -1)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP