Advertisement
Guest User

Untitled

a guest
May 5th, 2021
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. #objective: convert any 2x2 matrix into a series of stretch / shear x / shear y operations, in that order
  2.  
  3. from sympy import symbols, sin, cos, atan
  4.  
  5. def rotate(q):
  6. return [[cos(q), -sin(q)],[sin(q), cos(q)]]
  7.  
  8. theta = symbols("Q")
  9. m = rotate(theta)
  10.  
  11. [w,x],[y,z] = m
  12. det = z*w - x*y
  13. if w == 0:
  14. #should only happen for rotation by k*90, or scaling with cx=0
  15. raise ValueError("w can't be zero (are you rotating by 90?)")
  16. if det == 0:
  17. raise ValueError("determinant can't be zero")
  18.  
  19. #formula derived by haruspicy
  20. scale_x = w
  21. scale_y = y/w
  22. shear_y = z - (x*y/w)
  23. shear_x = x*w / det
  24.  
  25. #paint skews in the opposite of the conventional direction to account for its top-left origin system
  26. skew_x = -atan(shear_x)
  27. skew_y = -atan(shear_y)
  28.  
  29. print("scale_x:", scale_x)
  30. print("scale_y:", scale_y)
  31. print("shear_x:", shear_x)
  32. print("shear_y:", shear_y)
  33. print("skew_x:", skew_x)
  34. print("skew_y:", skew_y)
  35.  
  36. """result:
  37.  
  38. scale_x: cos(Q)
  39. scale_y: sin(Q)/cos(Q)
  40. shear_x: -sin(Q)*cos(Q)/(sin(Q)**2 + cos(Q)**2)
  41. shear_y: sin(Q)**2/cos(Q) + cos(Q)
  42. skew_x: atan(sin(Q)*cos(Q)/(sin(Q)**2 + cos(Q)**2))
  43. skew_y: -atan(sin(Q)**2/cos(Q) + cos(Q))
  44.  
  45. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement