Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Check if it is possible to draw a straight line with the given direction cosines in Python
In 3D geometry, direction cosines represent the cosines of angles that a line makes with the coordinate axes. For a valid straight line, the sum of squares of direction cosines must equal 1.
Direction cosines l, m, and n are defined as:
- l = cos(?), where ? is the angle between the line and x-axis
- m = cos(?), where ? is the angle between the line and y-axis
- n = cos(?), where ? is the angle between the line and z-axis
- The fundamental property: l² + m² + n² = 1
Mathematical Foundation
For any unit vector in 3D space, the sum of squares of its direction cosines equals 1. This is derived from the Pythagorean theorem extended to three dimensions.
Algorithm
To check if given direction cosines are valid:
- Calculate sum_of_squares = l² + m² + n²
- Round the result to handle floating-point precision
- Check if the absolute difference from 1 is within tolerance
Implementation
def is_valid_direction_cosines(l, m, n):
"""Check if given direction cosines can form a valid straight line"""
sum_of_squares = l * l + m * m + n * n
sum_of_squares = round(sum_of_squares, 8)
# Check if sum equals 1 within tolerance
if abs(1 - sum_of_squares) < 0.0001:
return True
return False
# Test with valid direction cosines
l = 0.42426
m = 0.56568
n = 0.7071
result = is_valid_direction_cosines(l, m, n)
print(f"Direction cosines ({l}, {m}, {n}): {result}")
# Verify calculation
sum_squares = l*l + m*m + n*n
print(f"l² + m² + n² = {sum_squares:.5f}")
Direction cosines (0.42426, 0.56568, 0.7071): True l² + m² + n² = 1.00000
Testing with Different Cases
def test_direction_cosines():
test_cases = [
(0.42426, 0.56568, 0.7071), # Valid - vector (3,4,5) normalized
(0.5, 0.5, 0.5), # Invalid - sum > 1
(0.6, 0.8, 0.0), # Valid - 2D case
(1.0, 0.0, 0.0), # Valid - along x-axis
(0.0, 1.0, 0.0), # Valid - along y-axis
(0.0, 0.0, 1.0) # Valid - along z-axis
]
for i, (l, m, n) in enumerate(test_cases, 1):
valid = is_valid_direction_cosines(l, m, n)
sum_val = l*l + m*m + n*n
print(f"Test {i}: ({l}, {m}, {n}) ? {valid} (sum = {sum_val:.5f})")
test_direction_cosines()
Test 1: (0.42426, 0.56568, 0.7071) ? True (sum = 1.00000) Test 2: (0.5, 0.5, 0.5) ? False (sum = 0.75000) Test 3: (0.6, 0.8, 0.0) ? True (sum = 1.00000) Test 4: (1.0, 0.0, 0.0) ? True (sum = 1.00000) Test 5: (0.0, 1.0, 0.0) ? True (sum = 1.00000) Test 6: (0.0, 0.0, 1.0) ? True (sum = 1.00000)
Key Points
- Tolerance Check: Use small tolerance (0.0001) for floating-point comparison
- Rounding: Round to 8 decimal places to handle precision issues
- Unit Vector Property: Valid direction cosines represent a unit vector
- Geometric Meaning: Each cosine value must be between -1 and 1
Conclusion
Direction cosines are valid if their sum of squares equals 1. This fundamental property ensures the line has unit length and proper geometric meaning in 3D space.
Advertisements
