📜  求三角形外心的程序

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

给定表示三角形顶点的六个整数,比如A(x1, y1)B(x2, y2)C(x3, y3) ,任务是找到给定三角形的外心坐标。

例子:

方法:给定的问题可以通过使用求三角形的外心的公式来解决。请按照以下步骤解决问题:

  • 假设三角形的顶点是A(x1, y1)B(x2, y2)C(x3, y3)
  • 设边长为ABBCAC分别cab
    因此,寻找三角形的偏心器具的坐标公式由下式给出:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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 > 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
// 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
# 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#
// 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


Javascript


输出:
6 6
-3 3
2 -2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程