fork download
  1. import math
  2.  
  3. class point:
  4. def __init__(self, x=None, y = None):
  5. if x is None:
  6. x = 0
  7. y = 0
  8. self.x = x
  9. self.y = y
  10. def __str__(self):
  11. return '{:.3f}, {:.3f}'.format(self.x, self.y)
  12.  
  13. def d2r(d):
  14. return d/180 * math.pi
  15. def r2d(d):
  16. return d/math.pi * 180
  17.  
  18. def mkvec(angle):
  19. return point(math.cos(angle), math.sin(angle))
  20. def vec2ang(v):
  21. return math.atan2(v.y, v.x)
  22.  
  23.  
  24. PI=math.pi
  25. sin = math.sin
  26. cos = math.cos
  27.  
  28. def mk_rot_matrix(cos_val, sin_val):
  29. return [ [cos_val, -sin_val],
  30. [sin_val, cos_val] ]
  31. def mul_mat2(mat_a, mat_b):
  32. m0 = mat_a[0][0] * mat_b[0][0] + mat_a[0][1] * mat_b[1][0]
  33. m1 = mat_a[0][0] * mat_b[0][1] + mat_a[0][1] * mat_b[1][1]
  34. m2 = mat_a[1][0] * mat_b[0][0] + mat_a[1][1] * mat_b[1][0]
  35. m3 = mat_a[1][0] * mat_b[0][1] + mat_a[1][1] * mat_b[1][1]
  36. return [ [m0, m1],
  37. [m2, m3] ]
  38. def vec_mul(mat, vec):
  39. x = vec.x * mat[0][0] + vec.y * mat[1][0]
  40. y = vec.x * mat[0][1] + vec.y * mat[1][1]
  41. return point(x,y)
  42.  
  43. def test_assumption(angle_1, angle_2):
  44. v1 = mkvec(angle_1 * PI)
  45. v2 = mkvec(angle_2 * PI)
  46. v_exp = mkvec((angle_1 + angle_2) * PI)
  47.  
  48. mat_a = mk_rot_matrix(v1.x, v1.y)
  49. mat_b = mk_rot_matrix(v2.x, v2.y)
  50. mat_c = mul_mat2(mat_b, mat_a)
  51.  
  52. v3 = point(mat_c[0][0], -mat_c[0][1])
  53. v3_angle = vec2ang(v3) / PI
  54. expected = (angle_1 + angle_2)
  55.  
  56. print(\
  57. '''
  58. hack_rotation: {}
  59. proper_trig_rotation: {}
  60. result in radian: {:.3f}
  61. ({:.3f} + {:.3f}) PI == {:.3f} PI'
  62. '''.format(v3, v_exp, v3_angle, angle_1, angle_2, expected))
  63.  
  64.  
  65. test_assumption(1/6, 1/4)
  66. test_assumption(0.345, 0.765)
  67. test_assumption(0.845, 0.965)
  68. test_assumption(0.845, -0.2365)
  69.  
Success #stdin #stdout 0.02s 27704KB
stdin
Standard input is empty
stdout
hack_rotation: 0.259, 0.966
proper_trig_rotation: 0.259, 0.966
result in radian: 0.417
(0.167 + 0.250) PI == 0.417 PI'


hack_rotation: -0.941, -0.339
proper_trig_rotation: -0.941, -0.339
result in radian: -0.890
(0.345 + 0.765) PI == 1.110 PI'


hack_rotation: 0.827, -0.562
proper_trig_rotation: 0.827, -0.562
result in radian: -0.190
(0.845 + 0.965) PI == 1.810 PI'


hack_rotation: -0.334, 0.942
proper_trig_rotation: -0.334, 0.942
result in radian: 0.609
(0.845 + -0.236) PI == 0.609 PI'