📜  直角三角形的垂心和外心之间的距离

📅  最后修改于: 2021-10-23 08:09:07             🧑  作者: Mango

给定三对整数A(x, y)B(x, y)C(x, y) ,代表直角三角形的坐标,任务是找到正交中心和外心之间的距离。

例子:

方法:这个想法是根据以下观察找到给定三角形的正交中心外心的坐标:

请按照以下步骤解决问题:

  • 求直角三角形三边中最长的一条边,即斜边。
  • 找到斜边的中心并将其设置为外心
  • 找到与最长边相对的顶点并将其设置为orthocenter
  • 计算它们之间的距离并将其作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate Euclidean
// distance between the points p1 and p2
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 sqrt(pow(x2 - x1, 2)
                + pow(y2 - y1, 2) * 1.0);
}
 
// Function to find orthocenter of
// the right angled triangle
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
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 { (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 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
    cout << distance_between << endl;
}
 
// Driver Code
int main()
{
    pair 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 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
# 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#
// 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


Javascript


输出:
5

时间复杂度: O(1)
辅助空间: O(1)