📌  相关文章
📜  检查给定的坐标是否可以形成直角三角形

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

给定三个笛卡尔坐标,任务是检查给定的坐标是否可以形成直角三角形。如果可以创建直角三角形,则打印Yes 。否则,打印No
例子:

方法:
这个想法是使用毕达哥拉斯定理来检查直角三角形是否可能。通过连接给定的坐标来计算三角形三边的长度。设边为A、BC。给定的三角形是直角当且仅当A 2 + B 2 = C 2如果条件成立,则打印Yes 。否则,打印编号。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if right-angled
// triangle can be formed by the
// given coordinates
void checkRightAngled(int X1, int Y1,
                      int X2, int Y2,
                      int X3, int Y3)
{
    // Calculate the sides
    int A = (int)pow((X2 - X1), 2)
            + (int)pow((Y2 - Y1), 2);
 
    int B = (int)pow((X3 - X2), 2)
            + (int)pow((Y3 - Y2), 2);
 
    int C = (int)pow((X3 - X1), 2)
            + (int)pow((Y3 - Y1), 2);
 
    // Check Pythagoras Formula
    if ((A > 0 and B > 0 and C > 0)
        and (A == (B + C) or B == (A + C)
             or C == (A + B)))
        cout << "Yes";
 
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    int X1 = 0, Y1 = 2;
    int X2 = 0, Y2 = 14;
    int X3 = 9, Y3 = 2;
 
    checkRightAngled(X1, Y1, X2,
                     Y2, X3, Y3);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to check if right-angled
// triangle can be formed by the
// given coordinates
static void checkRightAngled(int X1, int Y1,
                             int X2, int Y2,
                             int X3, int Y3)
{
     
    // Calculate the sides
    int A = (int)Math.pow((X2 - X1), 2) +
            (int)Math.pow((Y2 - Y1), 2);
 
    int B = (int)Math.pow((X3 - X2), 2) +
            (int)Math.pow((Y3 - Y2), 2);
 
    int C = (int)Math.pow((X3 - X1), 2) +
            (int)Math.pow((Y3 - Y1), 2);
 
    // Check Pythagoras Formula
    if ((A > 0 && B > 0 && C > 0) &&
        (A == (B + C) || B == (A + C) ||
         C == (A + B)))
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver Code
public static void main(String s[])
{
    int X1 = 0, Y1 = 2;
    int X2 = 0, Y2 = 14;
    int X3 = 9, Y3 = 2;
     
    checkRightAngled(X1, Y1, X2, Y2, X3, Y3);
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program for the
# above approach
 
# Function to check if right-angled
# triangle can be formed by the
# given coordinates
def checkRightAngled(X1, Y1, X2,
                     Y2, X3, Y3):
     
    # Calculate the sides
    A = (int(pow((X2 - X1), 2)) +
         int(pow((Y2 - Y1), 2)))
    B = (int(pow((X3 - X2), 2)) +
         int(pow((Y3 - Y2), 2)))
    C = (int(pow((X3 - X1), 2)) +
         int(pow((Y3 - Y1), 2)))
     
    # Check Pythagoras Formula
    if ((A > 0 and B > 0 and C > 0) and
        (A == (B + C) or B == (A + C) or
         C == (A + B))):
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__=='__main__':
     
    X1 = 0; X2 = 0; X3 = 9;
    Y1 = 2; Y2 = 14; Y3 = 2;
     
    checkRightAngled(X1, Y1, X2,
                     Y2, X3, Y3)
     
# This code is contributed by virusbuddah_


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if right-angled
// triangle can be formed by the
// given coordinates
static void checkRightAngled(int X1, int Y1,
                             int X2, int Y2,
                             int X3, int Y3)
{
     
    // Calculate the sides
    int A = (int)Math.Pow((X2 - X1), 2) +
            (int)Math.Pow((Y2 - Y1), 2);
 
    int B = (int)Math.Pow((X3 - X2), 2) +
            (int)Math.Pow((Y3 - Y2), 2);
 
    int C = (int)Math.Pow((X3 - X1), 2) +
            (int)Math.Pow((Y3 - Y1), 2);
 
    // Check Pythagoras Formula
    if ((A > 0 && B > 0 && C > 0) &&
        (A == (B + C) || B == (A + C) ||
         C == (A + B)))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
// Driver Code
public static void Main(String []s)
{
    int X1 = 0, Y1 = 2;
    int X2 = 0, Y2 = 14;
    int X3 = 9, Y3 = 2;
     
    checkRightAngled(X1, Y1, X2, Y2, X3, Y3);
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


输出:
Yes

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