Check if a right-angled triangle can be formed by the given coordinates
Last Updated :
21 Nov, 2022
Given three Cartesian coordinates, the task is to check if a right-angled triangle can be formed by the given coordinates. If it is possible to create a right-angled triangle, print Yes. Otherwise, print No.
Examples:
Input: X1=0, Y1=5, X2=19, Y2=5, X3=0, Y3=0
Output: Yes
Explanation:
Length of side connecting points (X1, Y1) and (X2, Y2) is 12.
Length of side connecting points (X2, Y2) and (X3, Y3) is 15.
Length of side connecting points (X1, Y1) and (X3, Y3) is 9.
122 + 92 = 152.
Therefore, a right-angled triangle can be made.
Input: X1=5, Y1=14, X2=6, Y2=13, X3=8, Y3=7
Output: No
Approach:
The idea is to use the Pythagoras Theorem to check if a right-angled triangle is possible or not. Calculate the length of the three sides of the triangle by joining the given coordinates. Let the sides be A, B, and C. The given triangle is right-angled if and only if A2 + B2 = C2. Print Yes if the condition holds true. Otherwise, print No.
Below is the implementation of the above approach:
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
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
<script>
// JavaScript program to implement
// the above approach
// Function to check if right-angled
// triangle can be formed by the
// given coordinates
function checkRightAngled
(X1, Y1, X2, Y2, X3, Y3)
{
// Calculate the sides
var A = Math.pow(X2 - X1, 2) +
Math.pow(Y2 - Y1, 2);
var B = Math.pow(X3 - X2, 2) +
Math.pow(Y3 - Y2, 2);
var C = Math.pow(X3 - X1, 2) +
Math.pow(Y3 - Y1, 2);
// Check Pythagoras Formula
if (
A > 0 &&
B > 0 &&
C > 0 &&
(A === B + C || B === A + C ||
C === A + B)
)
document.write("Yes");
else document.write("No");
}
// Driver Code
var X1 = 0,
Y1 = 2;
var X2 = 0,
Y2 = 14;
var X3 = 9,
Y3 = 2;
checkRightAngled(X1, Y1, X2, Y2, X3, Y3);
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Check if a right-angled triangle can be formed by moving any one of the coordinates Given three coordinates of a triangle (x1, y1), (x2, y2), (x3, y3). The task is to find out if the triangle can be transformed to a right-angled triangle by moving only one point exactly by the distance 1. If it is possible to make the triangle right-angled, then print "POSSIBLE", else print "NOT PO
12 min read
Check if a right-angled triangle can be formed by the given side lengths Given two positive integers A and B representing the sides of a triangle, the task is to check if the given two sides of the triangle are sides of a valid right-angled triangle or not. If found to be true, print "YES". Otherwise, print "No". Examples: Input: A = 3, B = 4Output: Yes Explanation: A ri
8 min read
Find the dimensions of Right angled triangle Given H (Hypotenuse) and A (area) of a right angled triangle, find the dimensions of right angled triangle such that the hypotenuse is of length H and its area is A. If no such triangle exists, print "Not Possible". Examples: Input : H = 10, A = 24 Output : P = 6.00, B = 8.00 Input : H = 13, A = 36
6 min read
Check if right triangle possible from given area and hypotenuse Given area and hypotenuse, the aim is to print all sides if right triangle can exist, else print -1. We need to print all sides in ascending order. Examples: Input : 6 5 Output : 3 4 5 Input : 10 6 Output : -1 We have discussed a solution of this problem in below post. Find all sides of a right angl
6 min read
Find all sides of a right angled triangle from given hypotenuse and area | Set 1 Given hypotenuse and area of a right angle triangle, get its base and height and if any triangle with given hypotenuse and area is not possible, print not possible.Examples: Input : hypotenuse = 5, area = 6 Output : base = 3, height = 4 Input : hypotenuse = 5, area = 7 Output : No triangle possible
9 min read