Program to Find the Incenter of a Triangle - GeeksforGeeks

10 Mar, 2022
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) 
        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)
 
 
Approach:  
The center of the circle that touches the sides of a triangle is called its incenter.
Suppose the vertices of the triangle are A(x1, y1), B(x2, y2) and C(x3, y3).
Let the side AB = a, BC = b, AC = c then the coordinates of the in-center is given by the formula: 
 
 
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ program to find the// incenter of a triangle#include <bits/stdc++.h>using namespace std; // Driver codeint 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 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 program to find the# incenter of a triangle # Driver code # coordinate of the verticesx1 = 2; x2 = 1; x3 = 3;y1 = 2; y2 = 1; y3 = 1;a = 2; b = 1; c = 1; # Formula to calculate in-centerx = (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# 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.
<script>      // JavaScript program to find the      // incenter of a triangle      // Driver code      // coordinate of the vertices      var x1 = 2,        x2 = 1,        x3 = 3;      var y1 = 2,        y2 = 1,        y3 = 1;      var a = 2,        b = 1,        c = 1;       // Formula to calculate in-center      var x = (a * x1 + b * x2 + c * x3) / (a + b + c);      var y = (a * y1 + b * y2 + c * y3) / (a + b + c);       document.write(        "Incenter = " + "(" + x.toFixed(1) + ", " + y.toFixed(1) + ")"      );    </script>
Incenter= (2.0, 1.5)
 
vt_m
29AjayKumar
Akanksha_Rai
rdtank
ManasChhabra2
triangle
Geometric
Java
Mathematical
Mathematical
Java
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Convex Hull | Set 2 (Graham Scan)
Check whether a given point lies inside a triangle or not
Optimum location of point to minimize total distance
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples