📜  求三角形中心的程序

📅  最后修改于: 2021-10-23 08:18:01             🧑  作者: Mango

给定三角形的顶点和边长。一个圆内接于一个三角形。任务是找到三角形的中心。
例子:

Input: A(2, 2), B(1, 1), C(3, 1) 
        and AB = 2, BC = 1, AC = 1
Output: (2, 1.5)

Input: A(3, 3), B(1, 2), C(2, 2) 
        and AB = 3, BC = 2, AC = 2
Output: (2.5, 2.83)

方法:

  • 与三角形边相接的圆心称为内心。
  • 假设三角形的顶点是 A(x1, y1)、B(x2, y2) 和 C(x3, y3)。
  • 让边 AB = a, BC = b, AC = c 那么中心的坐标由公式给出:

下面是上述方法的实现:

C++
// C++ program to find the
// incenter of a triangle
#include 
using namespace std;
 
// Driver code
int main()
{
    // coordinate of the vertices
    float x1 = 2, x2 = 1, x3 = 3;
    float y1 = 2, y2 = 1, y3 = 1;
    float a = 2, b = 1, c = 1;
 
    // Formula to calculate in-center
    float x = (a * x1 + b *
                   x2 + c * x3) / (a + b + c);
    float y = (a * y1 + b *
                   y2 + c * y3) / (a + b + c);
 
    // System.out.print(setprecision(3));
    cout << "Incenter = "
         << "(" << x << ", " << y << ")";
    return 0;
}
 
// This code is contributed by 29AjayKumar


Java
// Java program to find the
// incenter of a triangle
 
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Driver code
    public static void main(String args[])
    {
        // coordinate of the vertices
        float x1 = 2, x2 = 1, x3 = 3;
        float y1 = 2, y2 = 1, y3 = 1;
        float a = 2, b = 1, c = 1;
 
        // Formula to calculate in-center
        float x
            = (a * x1 + b * x2 + c * x3) / (a + b + c);
        float y
            = (a * y1 + b * y2 + c * y3) / (a + b + c);
 
        // System.out.print(setprecision(3));
        System.out.println("Incenter= "
                           + "(" + x + ", " + y + ")");
    }
}


Python3
# Python3 program to find the
# incenter of a triangle
 
# Driver code
 
# coordinate of the vertices
x1 = 2; x2 = 1; x3 = 3;
y1 = 2; y2 = 1; y3 = 1;
a = 2; b = 1; c = 1;
 
# Formula to calculate in-center
x = (a * x1 + b * x2 + c * x3) / (a + b + c);
y = (a * y1 + b * y2 + c * y3) / (a + b + c);
 
# System.out.print(setprecision(3));
print("Incenter = (", x, ",", y, ")");
 
# This code is contributed
# by Akanksha Rai


C#
// C# program to find the
// incenter of a triangle
 
using System;
 
class GFG
{
 
    // Driver code
    public static void Main()
    {
        // coordinate of the vertices
        float x1 = 2, x2 = 1, x3 = 3;
        float y1 = 2, y2 = 1, y3 = 1;
        float a = 2, b = 1, c = 1;
 
        // Formula to calculate in-center
        float x
            = (a * x1 + b * x2 + c * x3) / (a + b + c);
        float y
            = (a * y1 + b * y2 + c * y3) / (a + b + c);
 
        // System.out.print(setprecision(3));
        Console.WriteLine("Incenter= "
                        + "(" + x + ", " + y + ")");
    }
}
 
// This code is contributed by vt_m.


Javascript


输出:
Incenter= (2.0, 1.5)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程