|
| 1 | +import math |
| 2 | + |
| 3 | + |
1 | 4 | '''
|
2 | 5 | Title : Class 2 - Find the Torsional Angle
|
3 | 6 | Subdomain : Classes
|
|
6 | 9 | Created : 15 July 2016
|
7 | 10 | Problem : https://www.hackerrank.com/challenges/class-2-find-the-torsional-angle/problem
|
8 | 11 | '''
|
9 |
| -# Enter your code here. Read input from STDIN. Print output to STDOUT |
10 |
| - |
11 |
| -import math |
12 |
| -def custom_diff(a,b): |
13 |
| - res0 = a[0] - b[0] |
14 |
| - res1 = a[1] - b[1] |
15 |
| - res2 = a[2] - b[2] |
16 |
| - return [res0,res1,res2] |
17 |
| - |
18 |
| -def dot_product(a,b): |
19 |
| - return a[0]*b[0]+a[1]*b[1]+a[2]*b[2] |
20 |
| - |
21 |
| -def abs_val(a): |
22 |
| - tmp_val=a[0]*a[0]+a[1]*a[1]+a[2]*a[2] |
23 |
| - return math.sqrt(tmp_val) |
24 |
| - |
25 | 12 |
|
| 13 | +class Points: |
| 14 | + def __init__(self, x, y, z): |
| 15 | + self.x = x |
| 16 | + self.y = y |
| 17 | + self.z = z |
26 | 18 |
|
27 |
| -def cross(a, b): |
28 |
| - c = [a[1]*b[2] - a[2]*b[1], |
29 |
| - a[2]*b[0] - a[0]*b[2], |
30 |
| - a[0]*b[1] - a[1]*b[0]] |
| 19 | + def __sub__(self, other): |
| 20 | + return Points(self.x - other.x, self.y - other.y, self.z - other.z) |
31 | 21 |
|
32 |
| - return c |
| 22 | + def dot(self, other): |
| 23 | + return self.x * other.x + self.y * other.y + self.z * other.z |
| 24 | + |
| 25 | + def absolute(self): |
| 26 | + return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z) |
33 | 27 |
|
34 |
| -a = list(map(float, input().split())) |
35 |
| -b = list(map(float, input().split())) |
36 |
| -c = list(map(float, input().split())) |
37 |
| -d = list(map(float, input().split())) |
| 28 | + def cross(self, other): |
| 29 | + return Points(self.y * other.z - self.z * other.y, |
| 30 | + self.z * other.x - self.x * other.z, |
| 31 | + self.x * other.y - self.y * other.x) |
38 | 32 |
|
39 |
| -ab=custom_diff(b,a) |
40 |
| -bc=custom_diff(c,b) |
41 |
| -cd=custom_diff(d,c) |
42 | 33 |
|
43 |
| -x=cross(ab,bc) |
44 |
| -y=cross(bc,cd) |
45 |
| - |
46 |
| -cosphi_top=dot_product(x,y) |
47 |
| -cosphi_bottom=abs_val(x)*abs_val(y) |
48 |
| -cosphi=cosphi_top/cosphi_bottom |
| 34 | +if __name__ == '__main__': |
| 35 | + points = list() |
| 36 | + for i in range(4): |
| 37 | + a = list(map(float, input().split())) |
| 38 | + points.append(a) |
49 | 39 |
|
50 |
| -res=math.degrees(math.acos(cosphi)) |
| 40 | + a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3]) |
| 41 | + x = (b - a).cross(c - b) |
| 42 | + y = (c - b).cross(d - c) |
| 43 | + angle = math.acos(x.dot(y) / (x.absolute() * y.absolute())) |
51 | 44 |
|
52 |
| -print("%.2f" %res) |
| 45 | + print("%.2f" % math.degrees(angle)) |
0 commit comments