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