HackerRank Python - Class 2 - Find the Torsional Angle




Class 2 - Find the Torsional Angle



  1. import math
  2. class Points(object):
  3. def __init__(self, x, y, z):
  4. self.x = x
  5. self.y = y
  6. self.z = z
  7. def __sub__(self, no):
  8. return Points((self.x - no.x), (self.y - no.y), (self.z - no.z))
  9. def dot(self, no):
  10. return (self.x * no.x) + (self.y * no.y) + (self.z * no.z)
  11. def cross(self, no):
  12. return Points((self.y * no.z - self.z * no.y), (self.z * no.x - self.x * no.z), (self.x * no.y - self.y * no.x))
  13. def absolute(self):
  14. return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5)
  15. if __name__ == '__main__':
  16. points = list()
  17. for i in range(4):
  18. a = list(map(float, input().split()))
  19. points.append(a)
  20. a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3])
  21. x = (b - a).cross(c - b)
  22. y = (c - b).cross(d - c)
  23. angle = math.acos(x.dot(y) / (x.absolute() * y.absolute()))
  24. print("%.2f" % math.degrees(angle))




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube