Distance between orthocenter and circumcenter of a right-angled triangle

24 Oct, 2021
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.5Explanation: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: 5Explanation: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++
Java
Python3
C#
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to calculate Euclidean// distance between the points p1 and p2double 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 trianglepair<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 trianglepair<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 circumcentervoid 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 Codeint 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 approachimport 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 approachimport math # Function to calculate Euclidean# distance between the points p1 and p2def 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 triangledef 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 triangledef 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 circumcenterdef 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 CA = [ 0, 0 ]B = [ 6, 0 ]C = [ 0, 8 ] # Function CallfindDistance(A, B, C) # This code is contributed by divyesh072019.
// C# program for the above approachusing 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 p2function 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 trianglefunction 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 trianglefunction 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 circumcenterfunction 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 Codevar A, B, C;  // Given coordinates A, B, and CA = [ 0, 0 ];B = [ 6, 0 ];C = [ 0, 8 ]; // Function CallfindDistance(A, B, C); // This code is contributed by Shubham Singh</script>
5
 
Time Complexity: O(1)Auxiliary Space: O(1)
shikhasingrajput
29AjayKumar
divyesh072019
shivanisinghss2110
sweetyty
Geometric-Lines
triangle
Geometric
Mathematical
School Mathematics
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum Manhattan distance between a distinct pair from N coordinates
Total area of two overlapping rectangles
Largest area possible after removal of a series of horizontal & vertical bars
Orientation of 3 ordered points
Program to find line passing through 2 Points
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays