Length of diagonal of a parallelogram using adjacent sides and angle between them

08 Apr, 2021
Given two integers a and b where a and b represents the length of adjacent sides of a parallelogram and an angle 0 between them, the task is to find the length of diagonal of the parallelogram.
Examples:
Input: a = 6, b = 10, 0=30Output: 6.14 
Input: a = 3, b = 5, 0=45Output: 3.58
Approach: Consider a parallelogram ABCD with sides a and b, now apply cosine rule at angle A in the triangle ABD to find the length of diagonal p, similarly find diagonal q from triangle ABC.
Therefore the diagonals is given by: 
 
C++
Java
Python3
C#
Javascript
// C++ program to find length// Of diagonal of a parallelogram// Using sides and angle between them.#include <bits/stdc++.h>using namespace std;#define PI 3.147 // Function to return the length// Of diagonal of a parallelogram// using sides and angle between them.double Length_Diagonal(int a, int b, double theta){    double diagonal = sqrt((pow(a, 2) + pow(b, 2)) -                      2 * a * b * cos(theta * (PI / 180)));     return diagonal;} // Driver Codeint main(){     // Given sides    int a = 3;    int b = 5;     // Given angle    double theta = 45;     // Function call    double ans = Length_Diagonal(a, b, theta);     // Print the final answer    printf("%.2f", ans);} // This code is contributed by Amit Katiyar
// Java program to find length// Of diagonal of a parallelogram// Using sides and angle between them.class GFG{ // Function to return the length// Of diagonal of a parallelogram// using sides and angle between them.static double Length_Diagonal(int a, int b,                              double theta){    double diagonal = Math.sqrt((Math.pow(a, 2) +                                 Math.pow(b, 2)) -                                 2 * a * b *                                 Math.cos(theta *                                 (Math.PI / 180)));     return diagonal;} // Driver Codepublic static void main(String[] args){         // Given sides    int a = 3;    int b = 5;     // Given angle    double theta = 45;     // Function call    double ans = Length_Diagonal(a, b, theta);     // Print the final answer    System.out.printf("%.2f", ans);}} // This code is contributed by amal kumar choubey
# Python3 Program to find length# Of diagonal of a parallelogram# Using sides and angle between them. import math    # Function to return the length# Of diagonal of a parallelogram# using sides and angle between them. def Length_Diagonal(a, b, theta):        diagonal = math.sqrt( ((a**2) + (b**2))    - 2 * a*b * math.cos(math.radians(theta)))         return diagonal    # Driver Code # Given Sidesa = 3b = 5 # Given Angletheta = 45   # Function Call ans = Length_Diagonal(a, b, theta)    # Print the final answerprint(round(ans, 2))
// C# program to find length// Of diagonal of a parallelogram// Using sides and angle between them.using System; class GFG{ // Function to return the length// Of diagonal of a parallelogram// using sides and angle between them.static double Length_Diagonal(int a, int b,                              double theta){    double diagonal = Math.Sqrt((Math.Pow(a, 2) +                                 Math.Pow(b, 2)) -                                 2 * a * b *                                 Math.Cos(theta *                                (Math.PI / 180)));     return diagonal;} // Driver Codepublic static void Main(String[] args){         // Given sides    int a = 3;    int b = 5;     // Given angle    double theta = 45;     // Function call    double ans = Length_Diagonal(a, b, theta);     // Print the readonly answer    Console.Write("{0:F2}", ans);}} // This code is contributed by amal kumar choubey
<script> // javascript program to find length// Of diagonal of a parallelogram// Using sides and angle between them. // Function to return the length// Of diagonal of a parallelogram// using sides and angle between them.function Length_Diagonal(a , b,theta){    var diagonal = Math.sqrt((Math.pow(a, 2) +                                 Math.pow(b, 2)) -                                 2 * a * b *                                 Math.cos(theta *                                 (Math.PI / 180)));     return diagonal;} // Driver Code // Given sidesvar a = 3;var b = 5; // Given anglevar theta = 45; // Function callvar ans = Length_Diagonal(a, b, theta); // Print the final answerdocument.write(ans.toFixed(2)); // This code is contributed by 29AjayKumar </script>
3.58
 
Time Complexity: O(1)Auxiliary Space: O(1)
Amal Kumar Choubey
amit143katiyar
29AjayKumar
Geometric
School Programming
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Optimum location of point to minimize total distance
Check if two given circles touch or intersect each other
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java