bisected_hypotenuse.sf 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 06 July 2018
  4. # https://github.com/trizen
  5. # Given two catheti lengths (A+B and C) of a right triangle and assuming a parallel line,
  6. # relative to side C, that divides the triangle, find the values of X and Y, satisfying:
  7. #
  8. # 1. (X+Y)^2 = (A+B)^2 + C^2
  9. # 2. X/Y = A/B
  10. #
  11. # C
  12. # ----================------- L1
  13. # \ |
  14. # X \ | A
  15. # \ |
  16. # --------------------------- L2
  17. # \ |
  18. # Y \ | B
  19. # \ |
  20. # \ |
  21. # \ |
  22. # \ |
  23. # \ |
  24. # \ |
  25. # \ |
  26. # \|
  27. # Because the lines L1 and L2 are parallel to each other, we have the identity:
  28. # A/B = X/Y
  29. # See also:
  30. # https://www.youtube.com/watch?v=ffvojZONF_A
  31. func approximate_solution(A, B, C) { # approximation in positive integers
  32. var h = sqrt((A+B)**2 + C**2)
  33. var r = (A > B ? (A / B) : (B / A))
  34. var Y = bsearch_le(0, h, {|n|
  35. n / (h-n) <=> r
  36. })
  37. var X = h-Y
  38. (X, Y) = (Y, X) if (A > B)
  39. return [X,Y]
  40. }
  41. func exact_solution(A, B, C) { # exact solution in complex numbers
  42. var X = (1i * A * sqrt(A**2 + 2*A*B + B**2 + C**2))/sqrt(-((A+B)**2))
  43. var Y = (1i * B * sqrt(A**2 + 2*A*B + B**2 + C**2))/sqrt(-((A+B)**2))
  44. return [X, Y]
  45. }
  46. func exact_solution_real(A, B, C) { # exact solution in real numbers
  47. var X = (A * sqrt((A+B)**2 + C**2))/abs(A+B)
  48. var Y = (B * sqrt((A+B)**2 + C**2))/abs(A+B)
  49. return [X, Y]
  50. }
  51. say approximate_solution(49, 161-49, 240) #=> [88, 201]
  52. say approximate_solution(161-29, 29, 240) #=> [236, 53]
  53. >''
  54. say exact_solution(49, 161-49, 240) #=> [87.9565217391304347826086956521739130434782608696, 201.04347826086956521739130434782608695652173913]
  55. say exact_solution(161-29, 29, 240) #=> [236.944099378881987577639751552795031055900621118, 52.055900621118012422360248447204968944099378882]
  56. >''
  57. say exact_solution_real(49, 161-49, 240) #=> [87.9565217391304347826086956521739130434782608696, 201.04347826086956521739130434782608695652173913]
  58. say exact_solution_real(161-29, 29, 240) #=> [236.944099378881987577639751552795031055900621118, 52.055900621118012422360248447204968944099378882]