Программа для поиска центра тяжести треугольника
Опубликовано: 15 Января, 2022
Даны вершины треугольника. Задача - найти центр тяжести треугольника:
Примеры:
Ввод: A (1, 2), B (3, -4), C (6, -7) Выход: (3,33, -3) Ввод: A (6, 2), B (5, -9), C (2, -7). Выход: (6.5, -9)
Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.

Подход: предположим, что если вершины треугольника равны (x1, y1) (x2, y2) (x3, y3), то центроид треугольника можно найти по следующей формуле:

C++
// CPP program to find the centroid of triangle#include <bits/stdc++.h>using namespace std;// Driver codeint 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 triangleimport java.util.*;import java.lang.*;class GFG{ // Driver codepublic 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 triangleusing System;class GFG{ // Driver codestatic 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 |
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?> |
Javascript
<script>// javascript program to find the centroid of triangle// Driver code// coordinate of the verticesvar x1 = 1, x2 = 3, x3 = 6;var y1 = 2, y2 = -4, y3 = -7;// Formula to calculate centroidvar 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> |
Output:
Centroid = (3.33, -3)
Вниманию читателя! Не прекращайте учиться сейчас. Получите все важные математические концепции для соревновательного программирования с курсом Essential Maths for CP по доступной для студентов цене. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .