Skip to content

Commit b2964e7

Browse files
authored
Update Class2FindtheTorsionalAngle.py
Created Points class to meet the stab code
1 parent c205ed9 commit b2964e7

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed
Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import math
2+
3+
14
'''
25
Title : Class 2 - Find the Torsional Angle
36
Subdomain : Classes
@@ -6,47 +9,37 @@
69
Created : 15 July 2016
710
Problem : https://www.hackerrank.com/challenges/class-2-find-the-torsional-angle/problem
811
'''
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-
2512

13+
class Points:
14+
def __init__(self, x, y, z):
15+
self.x = x
16+
self.y = y
17+
self.z = z
2618

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)
3121

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)
3327

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)
3832

39-
ab=custom_diff(b,a)
40-
bc=custom_diff(c,b)
41-
cd=custom_diff(d,c)
4233

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)
4939

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()))
5144

52-
print("%.2f" %res)
45+
print("%.2f" % math.degrees(angle))

0 commit comments

Comments
 (0)