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++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to calculate the
// distance between a pair of points
float 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 triangle
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
vector<pair<float, float> > 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 Code
int 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 program for the above approach
import 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 points
static 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 triangle
static 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 code
public 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 program for the above approach
from math import sqrt
# Function to calculate the
# distance between a pair of points
def 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 triangle
def 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 Code
if __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# program for the above approach
using 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 points
static 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 triangle
static 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 code
static 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
<script>
// Javascript implementation for the above approach
// Function to calculate the
// distance between a pair of points
function distance( m, n, p, q)
{
return Math.sqrt(Math.pow(n - m, 2)
+ Math.pow(q - p, 2) * 1.0);
}
// Function to calculate the coordinates
// of the excenters of a triangle
function Excenters( x1, y1, x2, y2, x3, y3)
{
// Length of the sides of the triangle
var a = distance(x2, x3, y2, y3);
var b = distance(x3, x1, y3, y1);
var c = distance(x1, x2, y1, y2);
// Stores the coordinates of the
// excenters of the triangle
var excenter = new Array(4);
for (var i= 0; i<4;i++)
excenter[i] = new Array(2);
// 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 (var i = 1; i <= 3; i++) {
document.write(excenter[i][0] + " " + excenter[i][1] +"<br>");
}
}
// Driver Code
var 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 Shubham Singh
</script>
Output:
6 6 -3 3 2 -2
Time Complexity: O(logn) as using inbuilt sqrt function
Auxiliary Space: O(1)
