Related Articles
Program to find the Excenters of a Triangle
• Difficulty Level : Easy
• Last Updated : 14 Jun, 2021

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 side of the triangle, intersect. There are a total of three excenters in a triangle.

Examples:

Input: x1 = 0, y1 = 0, x2 = 3, y2 = 0, x3 = 0, y3 = 4
Output:
6 6
-3 3
2 -2
Explanation: The coordinates of the Excenters of the triangle are: (6, 6), (-3, 3), (2, -2)

Input: x1 = 0, y1 = 0, x2 = 12, y2 = 0, x3 = 0, y3 = 5
Output:
15 15
-3 3
10 -10

Approach: The given problem can be solved by using the formula for finding the excenter of the triangles. Follow the steps below to solve the problem:

• Suppose the vertices of the triangle are A(x1, y1), B(x2, y2), and C(x3, y3).
• Let the length of the sides be AB, BC and AC be c, a and b respectively.
Therefore, the formula to find the coordinates of the Excenters of the triangle is given by:

I1 = { (-a*x1 + b*x2 + c*x3) / (-a + b + c ), (-a*y1 +b*y2 + c*y3 ) / (-a + b + c) }

I2 = { ( a*x1 – b*x2 + c*x3) / ( a – b + c ), ( a*y1 -b*y2 + c*y3 ) / (a – b + c) }

I3 = { ( a*x1 + b*x2 – c*x3 / (a + b – c ), ( a*y1 +b*y2 – c*y3) / (a + b – c) }

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include using namespace std; // Function to calculate the// distance between a pair of pointsfloat distance(int m, int n, int p, int q){    return sqrt(pow(n - m, 2)                + pow(q - p, 2) * 1.0);} // Function to calculate the coordinates// of the excenters of a trianglevoid Excenters(int x1, int y1, int x2,               int y2, int x3, int y3){    // Length of the sides of the triangle    float a = distance(x2, x3, y2, y3);    float b = distance(x3, x1, y3, y1);    float c = distance(x1, x2, y1, y2);     // Stores the coordinates of the    // excenters of the triangle    vector > excenter(4);     // Applying formula to find the    // excenters of the triangle     // For I1    excenter[1].first        = (-(a * x1) + (b * x2) + (c * x3))          / (-a + b + c);    excenter[1].second        = (-(a * y1) + (b * y2) + (c * y3))          / (-a + b + c);     // For I2    excenter[2].first        = ((a * x1) - (b * x2) + (c * x3))          / (a - b + c);    excenter[2].second        = ((a * y1) - (b * y2) + (c * y3))          / (a - b + c);     // For I3    excenter[3].first        = ((a * x1) + (b * x2) - (c * x3))          / (a + b - c);    excenter[3].second        = ((a * y1) + (b * y2) - (c * y3))          / (a + b - c);     // Print the excenters of the triangle    for (int i = 1; i <= 3; i++) {        cout << excenter[i].first << " "             << excenter[i].second             << endl;    }} // Driver Codeint main(){    float x1, x2, x3, y1, y2, y3;    x1 = 0;    x2 = 3;    x3 = 0;    y1 = 0;    y2 = 0;    y3 = 4;     Excenters(x1, y1, x2, y2, x3, y3);     return 0;}

## Java

 // Java program for the above approachimport java.util.*; class GFG{     static class pair{    float first, second;         pair(float first, float second)    {        this.first = first;        this.second = second;    }} // Function to calculate the// distance between a pair of pointsstatic float distance(int m, int n,                      int p, int q){    return (float)Math.sqrt(Math.pow(n - m, 2) +                            Math.pow(q - p, 2) * 1.0);} // Function to calculate the coordinates// of the excenters of a trianglestatic void Excenters(int x1, int y1, int x2,                      int y2, int x3, int y3){         // Length of the sides of the triangle    float a = distance(x2, x3, y2, y3);    float b = distance(x3, x1, y3, y1);    float c = distance(x1, x2, y1, y2);     // Stores the coordinates of the    // excenters of the triangle    pair[] excenter = new pair[4];     // Applying formula to find the    // excenters of the triangle     // For I1    excenter[1] = new pair((-(a * x1) + (b * x2) +                             (c * x3)) / (-a + b + c),                           (-(a * y1) + (b * y2) +                             (c * y3)) / (-a + b + c));     // For I2    excenter[2] = new pair(((a * x1) - (b * x2) +                            (c * x3)) / (a - b + c),                           ((a * y1) - (b * y2) +                            (c * y3)) / (a - b + c));     // For I3    excenter[3] = new pair(((a * x1) + (b * x2) -                            (c * x3)) / (a + b - c),                           ((a * y1) + (b * y2) -                            (c * y3)) / (a + b - c));     // Print the excenters of the triangle    for(int i = 1; i <= 3; i++)    {        System.out.println((int)excenter[i].first + " " +                           (int)excenter[i].second);    }} // Driver codepublic static void main(String[] args){    int x1, x2, x3, y1, y2, y3;    x1 = 0;    x2 = 3;    x3 = 0;    y1 = 0;    y2 = 0;    y3 = 4;     Excenters(x1, y1, x2, y2, x3, y3);}} // This code is contributed by offbeat

## Python3

 # Python3 program for the above approachfrom math import sqrt # Function to calculate the# distance between a pair of pointsdef distance(m, n, p, q):         return (sqrt(pow(n - m, 2) +                 pow(q - p, 2) * 1.0)) # Function to calculate the coordinates# of the excenters of a triangledef Excenters(x1, y1, x2, y2, x3, y3):         # Length of the sides of the triangle    a = distance(x2, x3, y2, y3)    b = distance(x3, x1, y3, y1)    c = distance(x1, x2, y1, y2)     # Stores the coordinates of the    # excenters of the triangle    excenter = [[0, 0] for i in range(4)]     # Applying formula to find the    # excenters of the triangle     # For I1    excenter[1][0] = ((-(a * x1) + (b * x2) +                        (c * x3)) // (-a + b + c))    excenter[1][1] = ((-(a * y1) + (b * y2) +                        (c * y3)) // (-a + b + c))     # For I2    excenter[2][0] = (((a * x1) - (b * x2) +                       (c * x3)) // (a - b + c))    excenter[2][1] = (((a * y1) - (b * y2) +                       (c * y3)) // (a - b + c))     # For I3    excenter[3][0] = (((a * x1) + (b * x2) -                       (c * x3)) // (a + b - c))    excenter[3][1] = (((a * y1) + (b * y2) -                       (c * y3)) // (a + b - c))     # Print the excenters of the triangle    for i in range(1, 4):        print(int(excenter[i][0]),              int(excenter[i][1])) # Driver Codeif __name__ == '__main__':         x1 = 0    x2 = 3    x3 = 0    y1 = 0    y2 = 0    y3 = 4     Excenters(x1, y1, x2, y2, x3, y3) # This code is contributed by mohit kumar 29

## C#

 // C# program for the above approachusing System; class GFG{ class pair{    public float first, second;     public pair(float first, float second)    {        this.first = first;        this.second = second;    }} // Function to calculate the// distance between a pair of pointsstatic float distance(int m, int n, int p, int q){    return (float)Math.Sqrt(Math.Pow(n - m, 2) +                            Math.Pow(q - p, 2) * 1.0);} // Function to calculate the coordinates// of the excenters of a trianglestatic void Excenters(int x1, int y1, int x2,                      int y2, int x3, int y3){     // Length of the sides of the triangle    float a = distance(x2, x3, y2, y3);    float b = distance(x3, x1, y3, y1);    float c = distance(x1, x2, y1, y2);     // Stores the coordinates of the    // excenters of the triangle    pair[] excenter = new pair[4];     // Applying formula to find the    // excenters of the triangle     // For I1    excenter[1] = new pair((-(a * x1) + (b * x2) +                             (c * x3)) / (-a + b + c),                           (-(a * y1) + (b * y2) +                             (c * y3)) / (-a + b + c));     // For I2    excenter[2] = new pair(((a * x1) - (b * x2) +                            (c * x3)) / (a - b + c),                           ((a * y1) - (b * y2) +                            (c * y3)) / (a - b + c));     // For I3    excenter[3] = new pair(((a * x1) + (b * x2) -                            (c * x3)) / (a + b - c),                           ((a * y1) + (b * y2) -                            (c * y3)) / (a + b - c));     // Print the excenters of the triangle    for (int i = 1; i <= 3; i++)    {        Console.WriteLine((int)excenter[i].first + " " +                          (int)excenter[i].second);    }} // Driver codestatic void Main(){    int x1, x2, x3, y1, y2, y3;    x1 = 0;    x2 = 3;    x3 = 0;    y1 = 0;    y2 = 0;    y3 = 4;     Excenters(x1, y1, x2, y2, x3, y3);}} // This code is contributed by abhinavjain194

## Javascript


Output:
6 6
-3 3
2 -2

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up