Encuentra todos los lados de un triángulo rectángulo a partir de la hipotenusa y el área dadas | Serie 1

Dada la hipotenusa y el área de un triángulo rectángulo, obtenga su base y altura y si no es posible ningún triángulo con la hipotenusa y el área dadas, imprima no es posible.
Ejemplos: 
 

Input  : hypotenuse = 5,    area = 6
Output : base = 3, height = 4

Input : hypotenuse = 5, area = 7 
Output : No triangle possible with above specification.

hypotenuse

Podemos usar una propiedad del triángulo de ángulo recto para resolver este problema, que se puede establecer de la siguiente manera, 
 

A right angle triangle with fixed hypotenuse attains
maximum area, when it is isosceles i.e. both height
and base becomes equal so if hypotenuse if H, then 
by pythagorean theorem,
Base2 + Height2 = H2

For maximum area both base and height should be equal, 
b2 + b2 = H2
b = sqrt(H2/2)

Above is the length of base at which triangle attains
maximum area, given area must be less than this maximum
area, otherwise no such triangle will possible.  

Ahora, si el área dada es menor que esta área máxima, podemos hacer una búsqueda binaria para la longitud de la base, ya que el aumento de la base aumentará el área, es una función que aumenta monótonamente donde la búsqueda binaria se puede aplicar fácilmente. 
En el siguiente código, se escribe un método para obtener el área del triángulo de ángulo recto, recuerde que para el área del triángulo de ángulo recto es ½ * base * altura y la altura se puede calcular a partir de la base y la hipotenusa usando el teorema de Pitágoras .
A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ program to get right angle triangle, given
// hypotenuse and area of triangle
#include <bits/stdc++.h>
using namespace std;
   
//  limit for float comparison
#define eps 1e-6
   
// Utility method to get area of right angle triangle,
// given base and hypotenuse
double getArea(double base, double hypotenuse)
{
    double height = sqrt(hypotenuse*hypotenuse - base*base);
    return 0.5 * base * height;
}
   
// Prints base and height of triangle using hypotenuse
// and area information
void printRightAngleTriangle(int hypotenuse, int area)
{
    int hsquare = hypotenuse * hypotenuse;
   
    // maximum area will be obtained when base and height
    // are equal (= sqrt(h*h/2))
    double sideForMaxArea = sqrt(hsquare / 2.0);
    double maxArea = getArea(sideForMaxArea, hypotenuse);
   
    // if given area itself is larger than maxArea then no
    // solution is possible
    if (area > maxArea)
    {
        cout << "Not possiblen";
        return;
    }
   
    double low = 0.0;
    double high = sideForMaxArea;
    double base;
   
    // binary search for base
    while (abs(high - low) > eps)
    {
        base = (low + high) / 2.0;
        if (getArea(base, hypotenuse) >= area)
            high = base;
        else
            low = base;
    }
   
    // get height by pythagorean rule
    double height = sqrt(hsquare - base*base);
    cout << base << " " << height << endl;
}
   
// Driver code to test above methods
int main()
{
    int hypotenuse = 5;
    int area = 6;
   
    printRightAngleTriangle(hypotenuse, area);
    return 0;
}

Java

// Java program to get right angle triangle, given
// hypotenuse and area of triangle
public class GFG {
 
// limit for float comparison
    final static double eps = (double) 1e-6;
 
// Utility method to get area of right angle triangle,
// given base and hypotenuse
    static double getArea(double base, double hypotenuse) {
        double height = Math.sqrt(hypotenuse * hypotenuse - base * base);
        return 0.5 * base * height;
    }
 
// Prints base and height of triangle using hypotenuse
// and area information
    static void printRightAngleTriangle(int hypotenuse, int area) {
        int hsquare = hypotenuse * hypotenuse;
 
        // maximum area will be obtained when base and height
        // are equal (= sqrt(h*h/2))
        double sideForMaxArea = Math.sqrt(hsquare / 2.0);
        double maxArea = getArea(sideForMaxArea, hypotenuse);
 
        // if given area itself is larger than maxArea then no
        // solution is possible
        if (area > maxArea) {
            System.out.print("Not possible");
            return;
        }
 
        double low = 0.0;
        double high = sideForMaxArea;
        double base = 0;
 
        // binary search for base
        while (Math.abs(high - low) > eps) {
            base = (low + high) / 2.0;
            if (getArea(base, hypotenuse) >= area) {
                high = base;
            } else {
                low = base;
            }
        }
 
        // get height by pythagorean rule
        double height = Math.sqrt(hsquare - base * base);
        System.out.println(Math.round(base) + " " + Math.round(height));
    }
 
// Driver code to test above methods
    static public void main(String[] args) {
        int hypotenuse = 5;
        int area = 6;
 
        printRightAngleTriangle(hypotenuse, area);
    }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python 3 program to get right angle triangle, given
# hypotenuse and area of triangle
 
# limit for float comparison
# define eps 1e-6
import math
 
# Utility method to get area of right angle triangle,
# given base and hypotenuse
def getArea(base, hypotenuse):
    height = math.sqrt(hypotenuse*hypotenuse - base*base);
    return 0.5 * base * height
 
# Prints base and height of triangle using hypotenuse
# and area information
def printRightAngleTriangle(hypotenuse, area):
    hsquare = hypotenuse * hypotenuse
 
    # maximum area will be obtained when base and height
    # are equal (= sqrt(h*h/2))
    sideForMaxArea = math.sqrt(hsquare / 2.0)
    maxArea = getArea(sideForMaxArea, hypotenuse)
 
    # if given area itself is larger than maxArea then no
    # solution is possible
    if (area > maxArea):
        print("Not possiblen")
        return
     
    low = 0.0
    high = sideForMaxArea
     
    # binary search for base
    while (abs(high - low) > 1e-6):
        base = (low + high) / 2.0
        if (getArea(base, hypotenuse) >= area):
            high =base
        else:
            low = base
     
    # get height by pythagorean rule
    height = math.ceil(math.sqrt(hsquare - base*base))
    base = math.floor(base)
    print(base,height)
 
# Driver code to test above methods
if __name__ == '__main__':
    hypotenuse = 5
    area = 6
 
    printRightAngleTriangle(hypotenuse, area)
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# program to get right angle triangle, given
// hypotenuse and area of triangle
 
using System;
public class GFG{
 
 
// limit for float comparison
     static double eps = (double) 1e-6;
 
// Utility method to get area of right angle triangle,
// given base and hypotenuse
    static double getArea(double base1, double hypotenuse) {
        double height = Math.Sqrt(hypotenuse * hypotenuse - base1 * base1);
        return 0.5 * base1 * height;
    }
 
// Prints base and height of triangle using hypotenuse
// and area information
    static void printRightAngleTriangle(int hypotenuse, int area) {
        int hsquare = hypotenuse * hypotenuse;
 
        // maximum area will be obtained when base and height
        // are equal (= sqrt(h*h/2))
        double sideForMaxArea = Math.Sqrt(hsquare / 2.0);
        double maxArea = getArea(sideForMaxArea, hypotenuse);
 
        // if given area itself is larger than maxArea then no
        // solution is possible
        if (area > maxArea) {
            Console.Write("Not possible");
            return;
        }
 
        double low = 0.0;
        double high = sideForMaxArea;
        double base1 = 0;
 
        // binary search for base
        while (Math.Abs(high - low) > eps) {
            base1 = (low + high) / 2.0;
            if (getArea(base1, hypotenuse) >= area) {
                high = base1;
            } else {
                low = base1;
            }
        }
 
        // get height by pythagorean rule
        double height = Math.Sqrt(hsquare - base1 * base1);
        Console.WriteLine(Math.Round(base1) + " " + Math.Round(height));
    }
 
// Driver code to test above methods
    static public void Main() {
        int hypotenuse = 5;
        int area = 6;
 
        printRightAngleTriangle(hypotenuse, area);
    }
}
 
// This code is contributed by 29AjayKumar

PHP

<?php
// PHP program to get right angle triangle,
// given hypotenuse and area of triangle
 
// limit for float comparison
$eps =.0000001;
 
// Utility method to get area of right
// angle triangle, given base and hypotenuse
function getArea($base, $hypotenuse)
{
    $height = sqrt($hypotenuse * $hypotenuse -
                                 $base * $base);
    return 0.5 * $base * $height;
}
 
// Prints base and height of triangle
// using hypotenuse and area information
function printRightAngleTriangle($hypotenuse,
                                 $area)
{
    global $eps;
    $hsquare = $hypotenuse * $hypotenuse;
 
    // maximum area will be obtained when base
    // and height are equal (= sqrt(h*h/2))
    $sideForMaxArea = sqrt($hsquare / 2.0);
    $maxArea = getArea($sideForMaxArea,
                       $hypotenuse);
 
    // if given area itself is larger than
    // maxArea then no solution is possible
    if ($area > $maxArea)
    {
        echo "Not possiblen";
        return;
    }
 
    $low = 0.0;
    $high = $sideForMaxArea;
    $base;
 
    // binary search for base
    while (abs($high - $low) > $eps)
    {
        $base = ($low + $high) / 2.0;
        if (getArea($base, $hypotenuse) >= $area)
            $high = $base;
        else
            $low = $base;
    }
 
    // get height by pythagorean rule
    $height = sqrt($hsquare - $base * $base);
        echo (ceil($base)) ," ",
             (floor($height)), "\n";
}
 
// Driver Code
$hypotenuse = 5;
$area = 6;
 
printRightAngleTriangle($hypotenuse, $area);
 
// This code is contributed by Sachin
?>

Javascript

<script>
 
// JavaScript program to get right angle triangle, given
// hypotenuse and area of triangle
 
// limit for float comparison
    let eps =  1e-6;
   
// Utility method to get area of right angle triangle,
// given base and hypotenuse
    function getArea(base, hypotenuse) {
        let height = Math.sqrt(hypotenuse * hypotenuse - base * base);
        return 0.5 * base * height;
    }
   
// Prints base and height of triangle using hypotenuse
// and area information
    function printRightAngleTriangle(hypotenuse, area) {
        let hsquare = hypotenuse * hypotenuse;
   
        // maximum area will be obtained when base and height
        // are equal (= sqrt(h*h/2))
        let sideForMaxArea = Math.sqrt(hsquare / 2.0);
        let maxArea = getArea(sideForMaxArea, hypotenuse);
   
        // if given area itself is larger than maxArea then no
        // solution is possible
        if (area > maxArea) {
            document.write("Not possible");
            return;
        }
   
        let low = 0.0;
        let high = sideForMaxArea;
        let base = 0;
   
        // binary search for base
        while (Math.abs(high - low) > eps) {
            base = (low + high) / 2.0;
            if (getArea(base, hypotenuse) >= area) {
                high = base;
            } else {
                low = base;
            }
        }
   
        // get height by pythagorean rule
        let height = Math.sqrt(hsquare - base * base);
        document.write(Math.round(base) + " " + Math.round(height));
    }
     
// Driver Code
        let hypotenuse = 5;
         let area = 6;
   
        printRightAngleTriangle(hypotenuse, area);
 
// This code is contributed by chinmoy1997pal.
</script>

Producción: 
 

3 4

Complejidad de tiempo: O(log(n)) porque usa la función sqrt incorporada
Espacio auxiliar: O(1)

Una solución más se discute en la publicación a continuación. 
Compruebe si los ángulos rectos son posibles a partir del área y la hipotenusa dadas
. Este artículo es una contribución de Utkarsh Trivedi . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *