Horje
Program to find the shortest distance between diagonal and edge skew of a Cube

Given an integer A, denoting the length of a cube, the task is to find the shortest distance between the diagonal of a cube and an edge skew to it i.e. KL in the below figure.

Examples :

Input: A = 2
Output: 1.4142
Explanation:
Length of KL = A / ?2
Length of KL = 2 / 1.41 = 1.4142

Input: A = 3
Output: 2.1213

Approach: The idea to solve the problem is based on the following mathematical formula:

Let’s draw a perpendicular from K to down face of cube as Q.
Using Pythagorean Theorem in triangle QKL,

KL2 = QK2 + QL2

l2 = (a/2)2 + (a/2)2 

l2 = 2 * (a/2)2

l = a / sqrt(2)

Below is the implementation of the above approach:

C++

<?php
// PHP program for the above approach
   
// Function to find the shortest
// distance between the diagonal of a
// cube and an edge skew to it
function diagonalLength($a)
{
    // Stores the required distance
    $L = $a / sqrt(2);
 
    # Print the required distance
    echo $L;
}
 
// Given side of the cube
$a = 2;
 
// Function call to find the shortest
// distance between the diagonal of a
// cube and an edge skew to it
diagonalLength($a);
 
?>

Javascript

<script>
// javascript program for the above approach
 
// Function to find the shortest distance
// between the diagonal of a cube and
// an edge skew to it
function diagonalLength( a)
{
 
    // Stores the required distance
    let L = a / Math.sqrt(2);
 
    // Print the required distance
     document.write( L.toFixed(5));
}
 
// Driver Code
 
    // Given side of the cube
    let a = 2;
 
    // Function call to find the shortest
    // distance between the diagonal of a
    // cube and an edge skew to it
    diagonalLength(a);
 
// This code is contributed by todaysgaurav
 
</script>

Output: 

1.41421

 

Time Complexity: O(1)
Auxiliary Space: O(1)




Reffered: https://www.geeksforgeeks.org


Geometric

Related
Largest area possible after removal of a series of horizontal &amp; vertical bars Largest area possible after removal of a series of horizontal &amp; vertical bars
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides Queries to count points lying on or inside an isosceles Triangle with given length of equal sides
Program to find the length of Latus Rectum of a Parabola Program to find the length of Latus Rectum of a Parabola
Program to find the Orthocenter of a Triangle Program to find the Orthocenter of a Triangle
Program to find height of a Trapezoid Program to find height of a Trapezoid

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14