Distance between orthocenter and circumcenter of a right-angled triangle

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++
// 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
// 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
# 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#
// 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 |
Javascript
<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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



