Find the dimensions of Right angled triangle

2

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
Output : Not Possible

Approach:
Before moving to exact solution, let’s do some of mathematical calculations related to properties of Right angled triangle.
Suppose H = Hypotenuse, P = Perpendicular, B = Base and A = Area of right angled triangle.

We have some sort of equations as :

P^2 + B^2 = H^2
P * B = 2 * A
(P+B)^2 = P^2 + B^2 + 2*P*B = H^2 + 4*A
(P+B) = sqrt(H^2 + 4*A)  ----1
(P-B)^2 = P^2 + B^2 - 2*P*B = H^2 - 4*A
mod(P-B) = sqrt(H^2 - 4*A)  ----2
from equation (2) we can conclude that if 
H^2 < 4*A then no solution is possible.

Further from (1)+(2) and (1)-(2) we have :
P = (sqrt(H^2 + 4*A) + sqrt(H^2 - 4*A) ) / 2
B = (sqrt(H^2 + 4*A) - sqrt(H^2 - 4*A) ) / 2

Below is the implementation of above approach:

C++

// CPP program to find dimensions of 
// Right angled triangle
#include <bits/stdc++.h>
using namespace std;

// function to calculate dimension
void findDimen(int H, int A)
{
    // P^2+B^2 = H^2
    // P*B = 2*A
    // (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
    // (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
    // P+B = sqrt(H^2+4*A)
    // |P-B| = sqrt(H^2-4*A)

    if (H * H < 4 * A) {
        cout << "Not Possible\n";
        return;
    }

    // sqrt value of H^2 + 4A and H^2- 4A
    double apb = sqrt(H * H + 4 * A);
    double asb = sqrt(H * H - 4 * A);

    // Set precision
    cout.precision(2);

    cout << "P = " << fixed 
         << (apb - asb) / 2.0 << "\n";
    cout << "B = " << (apb + asb) / 2.0;
}

// driver function
int main()
{
    int H = 5;
    int A = 6;
    findDimen(H, A);
    return 0;
}

Python3

# Python code to find dimensions
# of Right angled triangle

# importing the math package 
# to use sqrt function
from math import sqrt

# function to find the dimensions
def findDimen( H, A):

    # P^2+B^2 = H^2
    # P*B = 2*A
    # (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
    # (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
    # P+B = sqrt(H^2+4*A)
    # |P-B| = sqrt(H^2-4*A)
    if H * H < 4 * A:
        print("Not Possible")
        return

    # sqrt value of H^2 + 4A and H^2- 4A
    apb = sqrt(H * H + 4 * A)
    asb = sqrt(H * H - 4 * A)
    
    # printing the dimensions
    print("P = ","%.2f" %((apb - asb) / 2.0))
    print("B = ","%.2f" %((apb + asb) / 2.0))
    
    
# driver code
H = 5 # assigning value to H
A = 6 # assigning value to A
findDimen(H, A) # calliing function

# This code is contributed by "Abhishek Sharma 44"


Output:

P = 3.00
B = 4.00

This article is contributed by ma5ter6it. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 1 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.