Given three pairs of integers A(x, y), B(x, y), and C(x, y), representing the coordinates of a right-angled triangle, the task is to find the distance between the orthocenter and circumcenter.
Examples:
Input: A = {0, 0}, B = {5, 0}, C = {0, 12}
Output: 6.5
Explanation:
Triangle ABC is right-angled at the point A. Therefore, orthocenter lies on the point A which is (0, 0).
The co-ordinate of circumcenter is (2.5, 6).
Therefore, the distance between the orthocenter and the circumcenter is 6.5.Input: A = {0, 0}, B = {6, 0}, C = {0, 8}
Output: 5
Explanation:
Triangle ABC is right-angled at the point A. Therefore, orthocenter lies on the point A which is (0, 0).
The co-ordinate of circumcenter is (3, 4).
Therefore, the distance between the orthocenter and the circumcenter is 5.
Approach: The idea is to find the coordinates of the orthocenter and the circumcenter of the given triangle based on the following observations:
The orthocenter is a point where three altitude meets. In a right angle triangle, the orthocenter is the vertex which is situated at the right-angled vertex.
The circumcenter is the point where the perpendicular bisector of the triangle meets. In a right-angled triangle, the circumcenter lies at the center of the hypotenuse.
Follow the steps below to solve the problem:
- Find the longest of the three sides of the right-angled triangle, i.e. the hypotenuse.
- Find the center of the hypotenuse and set it as the circumcenter.
- Find the vertex opposite to the longest side and set it as the orthocenter.
- Calculate the distance between them and print it as the result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Function to calculate Euclidean // distance between the points p1 and p2 double distance(pair< double , double > p1,
pair< double , double > p2)
{ // Stores x coordinates of both points
double x1 = p1.first, x2 = p2.first;
// Stores y coordinates of both points
double y1 = p1.second, y2 = p2.second;
// Return the Euclid distance
// using distance formula
return sqrt ( pow (x2 - x1, 2)
+ pow (y2 - y1, 2) * 1.0);
} // Function to find orthocenter of // the right angled triangle pair< double , double >
find_orthocenter(pair< double , double > A,
pair< double , double > B,
pair< double , double > C)
{ // Find the length of the three sides
double AB = distance(A, B);
double BC = distance(B, C);
double CA = distance(C, A);
// Orthocenter will be the vertex
// opposite to the largest side
if (AB > BC && AB > CA)
return C;
if (BC > AB && BC > CA)
return A;
return B;
} // Function to find the circumcenter // of right angle triangle pair< double , double >
find_circumcenter(pair< double , double > A,
pair< double , double > B,
pair< double , double > C)
{ // Circumcenter will be located
// at center of hypotenuse
double AB = distance(A, B);
double BC = distance(B, C);
double CA = distance(C, A);
// Find the hypotenuse and then
// find middle point of hypotenuse
// If AB is the hypotenuse
if (AB > BC && AB > CA)
return { (A.first + B.first) / 2,
(A.second + B.second) / 2 };
// If BC is the hypotenuse
if (BC > AB && BC > CA)
return { (B.first + C.first) / 2,
(B.second + C.second) / 2 };
// If AC is the hypotenuse
return { (C.first + A.first) / 2,
(C.second + A.second) / 2 };
} // Function to find distance between // orthocenter and circumcenter void findDistance(pair< double , double > A,
pair< double , double > B,
pair< double , double > C)
{ // Find circumcenter
pair< double , double > circumcenter
= find_circumcenter(A, B, C);
// Find orthocenter
pair< double , double > orthocenter
= find_orthocenter(A, B, C);
// Find the distance between the
// orthocenter and circumcenter
double distance_between
= distance(circumcenter,
orthocenter);
// Print distance between orthocenter
// and circumcenter
cout << distance_between << endl;
} // Driver Code int main()
{ pair< double , double > A, B, C;
// Given coordinates A, B, and C
A = { 0.0, 0.0 };
B = { 6.0, 0.0 };
C = { 0.0, 8.0 };
// Function Call
findDistance(A, B, C);
return 0;
} |
// Java program for the above approach import java.util.*;
class GFG{
static class pair
{
double first, second;
public pair( double first, double second)
{
this .first = first;
this .second = second;
}
}
// Function to calculate Euclidean
// distance between the points p1 and p2
static double distance(pair p1,
pair p2)
{
// Stores x coordinates of both points
double x1 = p1.first, x2 = p2.first;
// Stores y coordinates of both points
double y1 = p1.second, y2 = p2.second;
// Return the Euclid distance
// using distance formula
return Math.sqrt(Math.pow(x2 - x1, 2 )
+ Math.pow(y2 - y1, 2 ) * 1.0 );
}
// Function to find orthocenter of
// the right angled triangle
static pair
find_orthocenter(pair A,
pair B,
pair C)
{
// Find the length of the three sides
double AB = distance(A, B);
double BC = distance(B, C);
double CA = distance(C, A);
// Orthocenter will be the vertex
// opposite to the largest side
if (AB > BC && AB > CA)
return C;
if (BC > AB && BC > CA)
return A;
return B;
}
// Function to find the circumcenter
// of right angle triangle
static pair
find_circumcenter(pair A,
pair B,
pair C)
{
// Circumcenter will be located
// at center of hypotenuse
double AB = distance(A, B);
double BC = distance(B, C);
double CA = distance(C, A);
// Find the hypotenuse and then
// find middle point of hypotenuse
// If AB is the hypotenuse
if (AB > BC && AB > CA)
return new pair((A.first + B.first) / 2 ,
(A.second + B.second) / 2 );
// If BC is the hypotenuse
if (BC > AB && BC > CA)
return new pair((B.first + C.first) / 2 ,
(B.second + C.second) / 2 );
// If AC is the hypotenuse
return new pair( (C.first + A.first) / 2 ,
(C.second + A.second) / 2 );
}
// Function to find distance between
// orthocenter and circumcenter
static void findDistance(pair A,
pair B,
pair C)
{
// Find circumcenter
pair circumcenter
= find_circumcenter(A, B, C);
// Find orthocenter
pair orthocenter
= find_orthocenter(A, B, C);
// Find the distance between the
// orthocenter and circumcenter
double distance_between
= distance(circumcenter,
orthocenter);
// Print distance between orthocenter
// and circumcenter
System.out.print(distance_between + "\n" );
}
// Driver Code
public static void main(String[] args)
{
pair A, B, C;
// Given coordinates A, B, and C
A = new pair( 0.0 , 0.0 );
B = new pair( 6.0 , 0.0 );
C = new pair( 0.0 , 8.0 );
// Function Call
findDistance(A, B, C);
}
} // This code is contributed by shikhasingrajput |
# Python3 program for the above approach import math
# Function to calculate Euclidean # distance between the points p1 and p2 def distance(p1, p2) :
# Stores x coordinates of both points
x1, x2 = p1[ 0 ], p2[ 0 ]
# Stores y coordinates of both points
y1, y2 = p1[ 1 ], p2[ 1 ]
# Return the Euclid distance
# using distance formula
return int (math.sqrt( pow (x2 - x1, 2 ) + pow (y2 - y1, 2 )))
# Function to find orthocenter of # the right angled triangle def find_orthocenter(A, B, C) :
# Find the length of the three sides
AB = distance(A, B)
BC = distance(B, C)
CA = distance(C, A)
# Orthocenter will be the vertex
# opposite to the largest side
if (AB > BC and AB > CA) :
return C
if (BC > AB and BC > CA) :
return A
return B
# Function to find the circumcenter # of right angle triangle def find_circumcenter(A, B, C) :
# Circumcenter will be located
# at center of hypotenuse
AB = distance(A, B)
BC = distance(B, C)
CA = distance(C, A)
# Find the hypotenuse and then
# find middle point of hypotenuse
# If AB is the hypotenuse
if (AB > BC and AB > CA) :
return ((A[ 0 ] + B[ 0 ]) / / 2 , (A[ 1 ] + B[ 1 ]) / / 2 )
# If BC is the hypotenuse
if (BC > AB and BC > CA) :
return ((B[ 0 ] + C[ 0 ]) / / 2 , (B[ 1 ] + C[ 1 ]) / / 2 )
# If AC is the hypotenuse
return ((C[ 0 ] + A[ 0 ]) / / 2 , (C[ 1 ] + A[ 1 ]) / / 2 )
# Function to find distance between # orthocenter and circumcenter def findDistance(A, B, C) :
# Find circumcenter
circumcenter = find_circumcenter(A, B, C)
# Find orthocenter
orthocenter = find_orthocenter(A, B, C)
# Find the distance between the
# orthocenter and circumcenter
distance_between = distance(circumcenter, orthocenter)
# Print distance between orthocenter
# and circumcenter
print (distance_between)
# Given coordinates A, B, and C A = [ 0 , 0 ]
B = [ 6 , 0 ]
C = [ 0 , 8 ]
# Function Call findDistance(A, B, C) # This code is contributed by divyesh072019. |
// C# program for the above approach using System;
class GFG{
public class pair
{
public double first, second;
public pair( double first, double second)
{
this .first = first;
this .second = second;
}
}
// Function to calculate Euclidean
// distance between the points p1 and p2
static double distance(pair p1,
pair p2)
{
// Stores x coordinates of both points
double x1 = p1.first, x2 = p2.first;
// Stores y coordinates of both points
double y1 = p1.second, y2 = p2.second;
// Return the Euclid distance
// using distance formula
return Math.Sqrt(Math.Pow(x2 - x1, 2)
+ Math.Pow(y2 - y1, 2) * 1.0);
}
// Function to find orthocenter of
// the right angled triangle
static pair
find_orthocenter(pair A,
pair B,
pair C)
{
// Find the length of the three sides
double AB = distance(A, B);
double BC = distance(B, C);
double CA = distance(C, A);
// Orthocenter will be the vertex
// opposite to the largest side
if (AB > BC && AB > CA)
return C;
if (BC > AB && BC > CA)
return A;
return B;
}
// Function to find the circumcenter
// of right angle triangle
static pair
find_circumcenter(pair A,
pair B,
pair C)
{
// Circumcenter will be located
// at center of hypotenuse
double AB = distance(A, B);
double BC = distance(B, C);
double CA = distance(C, A);
// Find the hypotenuse and then
// find middle point of hypotenuse
// If AB is the hypotenuse
if (AB > BC && AB > CA)
return new pair((A.first + B.first) / 2,
(A.second + B.second) / 2 );
// If BC is the hypotenuse
if (BC > AB && BC > CA)
return new pair((B.first + C.first) / 2,
(B.second + C.second) / 2 );
// If AC is the hypotenuse
return new pair( (C.first + A.first) / 2,
(C.second + A.second) / 2 );
}
// Function to find distance between
// orthocenter and circumcenter
static void findDistance(pair A,
pair B,
pair C)
{
// Find circumcenter
pair circumcenter
= find_circumcenter(A, B, C);
// Find orthocenter
pair orthocenter
= find_orthocenter(A, B, C);
// Find the distance between the
// orthocenter and circumcenter
double distance_between
= distance(circumcenter,
orthocenter);
// Print distance between orthocenter
// and circumcenter
Console.Write(distance_between + "\n" );
}
// Driver Code
public static void Main(String[] args)
{
pair A, B, C;
// Given coordinates A, B, and C
A = new pair( 0.0, 0.0 );
B = new pair(6.0, 0.0 );
C = new pair(0.0, 8.0 );
// Function Call
findDistance(A, B, C);
}
} // This code is contributed by 29AjayKumar |
<script> // Javascript implementation for the above approach // Function to calculate Euclidean // distance between the points p1 and p2 function distance( p1, p2)
{ // Stores x coordinates of both points
var x1 = p1[0], x2 = p2[0];
// Stores y coordinates of both points
var y1 = p1[1], y2 = p2[1];
// Return the Euclid distance
// using distance formula
return Math.sqrt(Math.pow(x2 - x1, 2)
+ Math.pow(y2 - y1, 2) * 1.0);
} // Function to find orthocenter of // the right angled triangle function find_orthocenter( A, B, C)
{ // Find the length of the three sides
var AB = distance(A, B);
var BC = distance(B, C);
var CA = distance(C, A);
// Orthocenter will be the vertex
// opposite to the largest side
if (AB > BC && AB > CA)
return C;
if (BC > AB && BC > CA)
return A;
return B;
} // Function to find the circumcenter // of right angle triangle function find_circumcenter( A, B, C)
{ // Circumcenter will be located
// at center of hypotenuse
var AB = distance(A, B);
var BC = distance(B, C);
var CA = distance(C, A);
// Find the hypotenuse and then
// find middle point of hypotenuse
// If AB is the hypotenuse
if (AB > BC && AB > CA)
return [ Math.floor((A[0] + B[0]) / 2),
Math.floor((A[1] + B[1]) / 2) ];
// If BC is the hypotenuse
if (BC > AB && BC > CA)
return [ Math.floor((B[0] + C[0]) / 2),
Math.floor((B[1] + C[1]) / 2) ];
// If AC is the hypotenuse
return [ Math.floor((C[0] + A[0]) / 2),
Math.floor((C[1] + A[1]) / 2) ];
} // Function to find distance between // orthocenter and circumcenter function findDistance( A, B, C)
{ // Find circumcenter
circumcenter = find_circumcenter(A, B, C);
// Find orthocenter
orthocenter = find_orthocenter(A, B, C);
// Find the distance between the
// orthocenter and circumcenter
var distance_between
= distance(circumcenter,
orthocenter);
// Print distance between orthocenter
// and circumcenter
document.write(distance_between+ "<br>" );
} // Driver Code var A, B, C;
// Given coordinates A, B, and C A = [ 0, 0 ]; B = [ 6, 0 ]; C = [ 0, 8 ]; // Function Call findDistance(A, B, C); // This code is contributed by Shubham Singh </script> |
5
Time Complexity: O(1)
Auxiliary Space: O(1)