Program to find the Centroid of the triangle
Last Updated :
30 May, 2024
Given the vertices of the triangle. The task is to find the centroid of the triangle:
Examples:
Input: A(1, 2), B(3, -4), C(6, -7)
Output: (3.33, -3)
Input: A(6, 2), B(5, -9), C(2, -7)
Output: (6.5, -9)

Approach:Suppose if the vertices of a triangle are (x1, y1) (x2, y2) (x3, y3) then centroid of the triangle can be find from the following formula:
$$ X = \frac{x_{1}+x_{2}+x_{3}}{2} $$ $$ Y = \frac{y_{1}+y_{2}+y_{3}}{2} $$
1. Set x1, y1, x2, y2, x3, y3 to the coordinates of the vertices of the triangle.
2. Calculate the average x-coordinate of the vertices as x = (x1 + x2 + x3) / 3.
3. Calculate the average y-coordinate of the vertices as y = (y1 + y2 + y3) / 3.
4. Print "Centroid = (" + x + ", " + y + ")".
C++
// CPP program to find the centroid of triangle
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
// coordinate of the vertices
float x1 = 1, x2 = 3, x3 = 6;
float y1 = 2, y2 = -4, y3 = -7;
// Formula to calculate centroid
float x = (x1 + x2 + x3) / 3;
float y = (y1 + y2 + y3) / 3;
cout << setprecision(3);
cout << "Centroid = "
<< "(" << x << ", " << y << ")";
return 0;
}
Java
// Java program to find the centroid of triangle
import java.util.*;
import java.lang.*;
class GFG
{
// Driver code
public static void main(String args[])
{
// coordinate of the vertices
float x1 = 1, x2 = 3, x3 = 6;
float y1 = 2, y2 = -4, y3 = -7;
// Formula to calculate centroid
float x = (x1 + x2 + x3) / 3;
float y = (y1 + y2 + y3) / 3;
//System.out.print(setprecision(3));
System.out.println("Centroid = "
+ "(" + x + ", " + y + ")");
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
Python 3
# Python3 program to find
# the centroid of triangle
# Driver code
if __name__ == "__main__" :
# coordinate of the vertices
x1, x2, x3 = 1, 3, 6
y1, y2, y3 = 2, -4, -7
# Formula to calculate centroid
x = round((x1 + x2 + x3) / 3, 2)
y = round((y1 + y2 + y3) / 3, 2)
print("Centroid =","(",x,",",y,")")
# This code is contributed by ANKITRAI1
C#
// C# program to find the
// centroid of triangle
using System;
class GFG
{
// Driver code
static public void Main ()
{
// coordinate of the vertices
float x1 = 1, x2 = 3, x3 = 6;
float y1 = 2, y2 = -4, y3 = -7;
// Formula to calculate centroid
float x = (x1 + x2 + x3) / 3;
float y = (y1 + y2 + y3) / 3;
//System.out.print(setprecision(3));
Console.Write("Centroid = " +
"(" + x + ", " + y + ")");
}
}
// This code is contributed
// by RaJ
JavaScript
<script>
// javascript program to find the centroid of triangle
// Driver code
// coordinate of the vertices
var x1 = 1, x2 = 3, x3 = 6;
var y1 = 2, y2 = -4, y3 = -7;
// Formula to calculate centroid
var x = (x1 + x2 + x3) / 3;
var y = (y1 + y2 + y3) / 3;
// document.write(setprecision(3));
document.write("Centroid = "
+ "(" + x.toFixed(2) + ", " + y + ")");
// This code contributed by shikhasingrajput
</script>
PHP
<?php
// PHP program to find the
// centroid of triangle
// Driver code
// coordinate of the vertices
$x1 = 1;
$x2 = 3 ;
$x3 = 6;
$y1 = 2;
$y2 = -4;
$y3 = -7;
// Formula to calculate centroid
$x = round(($x1 + $x2 + $x3) / 3, 2);
$y = round(($y1 + $y2 + $y3) / 3, 2);
echo "Centroid = " .
"(" .$x .", " .$y .")";
// This code is contributed
// by ChitraNayal
?>
OutputCentroid = (3.33, -3)
Time complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
New Approach:
Another approach to find the centroid of a triangle is to use the concept of vectors.
- Let the vertices of the triangle be A(x1, y1), B(x2, y2), and C(x3, y3).
- Then, the centroid G can be found using the following formula: G = (A + B + C) / 3 where A, B, and C are position vectors of the respective vertices. In other words, G = ((x1 + x2 + x3) / 3, (y1 + y2 + y3) / 3)
Below is the code for the above approach :
C++
// C++ program to find the centroid of a triangle
#include <iostream>
using namespace std;
// Structure to store coordinates of a point
struct Point {
float x, y;
};
// Function to find the centroid of a triangle
Point findCentroid(Point A, Point B, Point C)
{
float x = (A.x + B.x + C.x) / 3;
float y = (A.y + B.y + C.y) / 3;
Point G = { x, y };
return G;
}
// Driver code
int main()
{
// Coordinates of the vertices
Point A = { 1, 2 }, B = { 3, -4 }, C = { 6, -7 };
// Find the centroid of the triangle
Point G = findCentroid(A, B, C);
// Print the centroid
cout << "Centroid = (" << G.x << ", " << G.y << ")"
<< endl;
return 0;
}
Java
// Java program to find the centroid of a triangle
import java.util.*;
// Class to store coordinates of a point
class Point {
float x, y;
Point(float x, float y) {
this.x = x;
this.y = y;
}
}
// Class to find the centroid of a triangle
class Centroid {
static Point findCentroid(Point A, Point B, Point C) {
float x = (A.x + B.x + C.x) / 3;
float y = (A.y + B.y + C.y) / 3;
Point G = new Point(x, y);
return G;
}
// Driver code
public static void main(String[] args) {
// Coordinates of the vertices
Point A = new Point(1, 2), B = new Point(3, -4), C = new Point(6, -7);
// Find the centroid of the triangle
Point G = findCentroid(A, B, C);
// Print the centroid
System.out.println("Centroid = (" + G.x + ", " + G.y + ")");
}
}
Python
# Python3 program to find the centroid of a triangle
import math
# Structure to store coordinates of a point
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
# Function to find the centroid of a triangle
def findCentroid(A, B, C):
x = (A.x + B.x + C.x) / 3
y = (A.y + B.y + C.y) / 3
G = Point(round(x, 6), round(y, 6))
return G
# Driver code
if __name__ == "__main__":
# Coordinates of the vertices
A = Point(1, 2)
B = Point(3, -4)
C = Point(6, -7)
# Find the centroid of the triangle
G = findCentroid(A, B, C)
# Print the centroid
print("Centroid = ({}, {})".format(G.x, G.y))
C#
// C# program to find the centroid of a triangle
using System;
// Structure to store coordinates of a point
struct Point
{
public float x;
public float y;
}
// Class to find the centroid of a triangle
class Centroid {
// Function to find the centroid of a triangle
public static Point findCentroid(Point A, Point B,
Point C)
{
float x = (A.x + B.x + C.x) / 3;
float y = (A.y + B.y + C.y) / 3;
Point G = new Point{ x = x, y = y };
return G;
}
// Driver code
static void Main(string[] args)
{
// Coordinates of the vertices
Point A = new Point{ x = 1, y = 2 };
Point B = new Point{ x = 3, y = -4 };
Point C = new Point{ x = 6, y = -7 };
// Find the centroid of the triangle
Point G = findCentroid(A, B, C);
// Print the centroid
Console.WriteLine("Centroid = ({0}, {1})", G.x,
G.y);
}
}
// This code is contributed by sarojmcy2e
JavaScript
// Class to store coordinates of a point
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
// Class to find the centroid of a triangle
class Centroid {
static findCentroid(A, B, C) {
let x = (A.x + B.x + C.x) / 3;
let y = (A.y + B.y + C.y) / 3;
let G = new Point(x, y);
return G;
}
// Driver code
static main() {
// Coordinates of the vertices
let A = new Point(1, 2), B = new Point(3, -4), C = new Point(6, -7);
// Find the centroid of the triangle
let G = Centroid.findCentroid(A, B, C);
// Print the centroid
console.log(`Centroid = (${G.x}, ${G.y})`);
}
}
// Call the main method
Centroid.main();
OutputCentroid = (3.33333, -3)
Time complexity: O(1), as the calculations involved are simple arithmetic operations.
Auxiliary Space: O(1), as the only variables used are the coordinates of the vertices and the centroid, which require constant space.
Similar Reads
Program to find the Excenters of a Triangle
Given six integers representing the vertices of a triangle, say A(x1, y1), B(x2, y2), and C(x3, y3), the task is to find the coordinates of the excenters of the given triangle. Excenter is a point where the bisector of one interior angle and bisectors of two external angle bisectors of the opposite
10 min read
Program to find Circumcenter of a Triangle
Given 3 non-collinear points in the 2D Plane P, Q and R with their respective x and y coordinates, find the circumcenter of the triangle.Note: Circumcenter of a triangle is the centre of the circle, formed by the three vertices of a triangle. Note that three points can uniquely determine a circle.Ex
13 min read
Program to Find the Incenter of a Triangle
Given the vertices of a triangle and length of its sides. A circle is inscribed in a triangle. The task is to find the incenter of a triangle.Examples: Input: A(2, 2), B(1, 1), C(3, 1) Output: (2, 1.5) Input: A(3, 3), B(1, 2), C(2, 2) Output: (2.5, 2.83) Approach: The center of the circle that touch
4 min read
Program to find the Orthocenter of a Triangle
Given three integers P, Q, and R representing 3 non-collinear points on a 2D plane with their respective x and y-coordinates, the task is to find the orthocenter of the triangle. The orthocenter of the triangle is usually denoted by H, which is the intersection point of three altitudes of a triangle
14 min read
Program to find the Volume of a Triangular Prism
Given the length, width, and height of a triangular prism, the task is to find the volume of the triangular prism. Examples: Input: l = 18, b = 12, h = 9 Output: Volume of triangular prism: 972 Input: l = 10, b = 8, h = 6 Output: Volume of triangular prism: 240 In mathematics, a triangular prism is
3 min read
Find Perimeter of a triangle
Given side (a, b, c) of a triangle, we have to find the perimeter of a triangle. Perimeter : Perimeter of a triangle is the sum of the length of side of a triangle. where a, b, c are length of side of a triangle.Perimeter of a triangle can simply be evaluated using following formula : perimeter = (a
3 min read
Program to print binary right angle triangle
Binary right angle triangle consists of only 0's and 1's in alternate positions. Examples : Input : 4 Output : 0 1 0 0 1 0 1 0 1 0 Input : 3 Output : 0 1 0 0 1 0 C++ // C program to print binary right angle // triangle. #include <stdio.h> // function to print binary right angle // triangle voi
4 min read
Program to check congruency of two triangles
Given four arrays of 3 numbers each which represents sides and angles of two triangles. The task is to check if the two triangles are Congruent or not. Also print the theorem by which they are congruent. Note: All sides and angles given as input are for valid triangles. Examples: Input : side1 = [3,
15+ min read
Program to check similarity of given two triangles
Given four array of 3 numbers each which represents sides and angles of two triangles. The task is to check if two triangles are similar or not. If it is similar, print the theorem by which it is. Examples: Input : side1 = [2, 3, 3] angle1 = [80, 60, 40] side2 = [4, 6, 6] angle2 = [40, 60, 80] Outpu
13 min read
Program to find area of a Trapezoid
Definition of Trapezoid : A Trapezoid is a convex quadrilateral with at least one pair of parallel sides. The parallel sides are called the bases of the trapezoid and the other two sides which are not parallel are referred to as the legs. There can also be two pairs of bases. In the above figure CD
4 min read