Program to find the Centroid of the triangle
Last Updated :
18 Apr, 2023
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:
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
float x1 = 1, x2 = 3, x3 = 6;
float y1 = 2, y2 = -4, y3 = -7;
float x = (x1 + x2 + x3) / 3;
float y = (y1 + y2 + y3) / 3;
cout << setprecision(3);
cout << "Centroid = "
<< "(" << x << ", " << y << ")" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
public static void main(String args[])
{
float x1 = 1 , x2 = 3 , x3 = 6 ;
float y1 = 2 , y2 = - 4 , y3 = - 7 ;
float x = (x1 + x2 + x3) / 3 ;
float y = (y1 + y2 + y3) / 3 ;
System.out.println( "Centroid = "
+ "(" + x + ", " + y + ")" );
}
}
|
Python 3
if __name__ = = "__main__" :
x1, x2, x3 = 1 , 3 , 6
y1, y2, y3 = 2 , - 4 , - 7
x = round ((x1 + x2 + x3) / 3 , 2 )
y = round ((y1 + y2 + y3) / 3 , 2 )
print ( "Centroid =" , "(" ,x, "," ,y, ")" )
|
Javascript
<script>
var x1 = 1, x2 = 3, x3 = 6;
var y1 = 2, y2 = -4, y3 = -7;
var x = (x1 + x2 + x3) / 3;
var y = (y1 + y2 + y3) / 3;
document.write( "Centroid = "
+ "(" + x.toFixed(2) + ", " + y + ")" );
</script>
|
C#
using System;
class GFG
{
static public void Main ()
{
float x1 = 1, x2 = 3, x3 = 6;
float y1 = 2, y2 = -4, y3 = -7;
float x = (x1 + x2 + x3) / 3;
float y = (y1 + y2 + y3) / 3;
Console.Write( "Centroid = " +
"(" + x + ", " + y + ")" );
}
}
|
PHP
<?php
$x1 = 1;
$x2 = 3 ;
$x3 = 6;
$y1 = 2;
$y2 = -4;
$y3 = -7;
$x = round (( $x1 + $x2 + $x3 ) / 3, 2);
$y = round (( $y1 + $y2 + $y3 ) / 3, 2);
echo "Centroid = " .
"(" . $x . ", " . $y . ")" ;
?>
|
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++
#include <iostream>
using namespace std;
struct Point {
float x, y;
};
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;
}
int main()
{
Point A = { 1, 2 }, B = { 3, -4 }, C = { 6, -7 };
Point G = findCentroid(A, B, C);
cout << "Centroid = (" << G.x << ", " << G.y << ")"
<< endl;
return 0;
}
|
Python3
import math
class Point:
def __init__( self , x, y):
self .x = x
self .y = y
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
if __name__ = = "__main__" :
A = Point( 1 , 2 )
B = Point( 3 , - 4 )
C = Point( 6 , - 7 )
G = findCentroid(A, B, C)
print ( "Centroid = ({}, {})" . format (G.x, G.y))
|
Java
import java.util.*;
class Point {
float x, y;
Point( float x, float y) {
this .x = x;
this .y = y;
}
}
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;
}
public static void main(String[] args) {
Point A = new Point( 1 , 2 ), B = new Point( 3 , - 4 ), C = new Point( 6 , - 7 );
Point G = findCentroid(A, B, C);
System.out.println( "Centroid = (" + G.x + ", " + G.y + ")" );
}
}
|
Javascript
class Point {
constructor(x, y) {
this .x = x;
this .y = y;
}
}
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;
}
static main() {
let A = new Point(1, 2), B = new Point(3, -4), C = new Point(6, -7);
let G = Centroid.findCentroid(A, B, C);
console.log(`Centroid = (${G.x}, ${G.y})`);
}
}
Centroid.main();
|
C#
using System;
struct Point
{
public float x;
public float y;
}
class Centroid {
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;
}
static void Main( string [] args)
{
Point A = new Point{ x = 1, y = 2 };
Point B = new Point{ x = 3, y = -4 };
Point C = new Point{ x = 6, y = -7 };
Point G = findCentroid(A, B, C);
Console.WriteLine( "Centroid = ({0}, {1})" , G.x,
G.y);
}
}
|
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.
Share your thoughts in the comments
Please Login to comment...