- Related Questions & Answers
- C++ program to find the Radius of the incircle of the triangle
- Java program to find the area of a triangle
- C++ program to find the Area of the Largest Triangle inscribed in a Hexagon?
- Program to find Circumcenter of a Triangle in C++
- Program to find the nth row of Pascal's Triangle in Python
- Find coordinates of the triangle given midpoint of each side in C++
- Find the altitude and area of an isosceles triangle in C++
- C++ Program to Compute the Area of a Triangle Using Determinants
- Java Program to find all angles of a triangle
- Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle What is Equilateral Triangle in C?
- PHP program to print continuous numbers in the form of triangle
- C++ program to find the type of the given iterator
- C# program to find IP Address of the client
- C# program to find the maximum of three numbers
- C# Program to find the sum of a sequence

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given a 2D array that denotes coordinates of three vertices of the triangle. Our task is to create a program to find the Centroid of the Triangle in C++.

**Centroid** of a triangle is the point at which the three medians of the triangles intersect.

**Median** of a triangle is the line that connects the vertex of the triangle with the center point of the line opposite to it.

Let’s take an example to understand the problem,

(-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)

For solving the problem, we will use the geometrical formula for the centroid of the triangle.

For points (ax, ay), (bx, by), (cx, cy)

Centroid, x = (ax + bx + cx) / 3 y = (ay + by + cy) / 3

**Program to illustrate the working of our solution,**

#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)

Advertisements