Skip to content
the poor coder

Hackerrank Class 2 - Find the Torsional Angle Solution

Poor Man
Published
Updated
Reading time
8 min read

You are given four points  and  in a 3-dimensional Cartesian coordinate system. You are required to print the angle between the plane made by the points  and  in degrees(not radians). Let the angle be .

where  x  and  x .

Here,  means the dot product of  and , and  x  means the cross product of vectors  and . Also, .

Input Format

One line of input containing the space separated floating number values of the  and  coordinates of a point.

Output Format

Output the angle correct up to two decimal places.

Sample Input

Text
0 4 5
1 7 6
0 5 9
1 7 2

Sample Output

Text
8.19

Solution in python3#

Approach 1.

Python
from math import acos, pi
p = []
for i in range(4):
    p.append(list(map(float, input().split())))
def cross(x, y):
    return [x[1] * y[2] - x[2] * y[1], x[2] * y[0] - x[0] * y[2],  x[0] * y[1] - x[1] * y[0]]
def dot(x, y):
    return sum([x[i] * y[i] for i in range(len(x))])
def length(x):
    return sum([a * a for a in x]) ** 0.5
def minus(x, y):
    return [x[i] - y[i] for i in range(len(x))]
X = cross(minus(p[1], p[0]), minus(p[2], p[1]))
Y = cross(minus(p[2], p[1]), minus(p[3], p[2]))
print("%.2f" % (acos(dot(X, Y) / length(X) / length(Y)) / pi * 180))

Approach 2.

Python
from math import *
points = []
def cross(a, b):
    return [a[1] * b[2] - b[1] * a[2], a[2] * b[0] - b[2] * a[0], a[0] * b[1] - b[0] * a[1]]
def dot(a, b):
    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
def length(a):
    return sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2])
def minus(a, b):
    return [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
for i in range(4):
    points.append(list(map(float, input().split())))
X = cross(minus(points[0], points[1]), minus(points[1], points[2]))
Y = cross(minus(points[1], points[2]), minus(points[2], points[3]))
cosphi = dot(X, Y) / (length(X) * length(Y))
print("%.2f" % degrees(acos(cosphi)))

Approach 3.

Python
import operator
import math
def cross(p1, p2):
  return [p1[1]*p2[2]-p1[2]*p2[1], p1[2]*p2[0]-p1[0]*p2[2], p1[0]*p2[1]-p1[1]*p2[0]]
def dot(p1, p2):
  return sum(map(operator.mul, p1, p2))
def subtract(a, b):
  return list(map(operator.sub, a, b))
def length(a):
  return math.sqrt(dot(a,a))
def parsePoint(str):
  components = str.split()
  return list(map(float, components))
A = parsePoint(input())
B = parsePoint(input())
C = parsePoint(input())
D = parsePoint(input())
AB = subtract(A,B)
BC = subtract(B,C)
CD = subtract(C,D)
X = cross(AB,BC)
Y = cross(BC,CD)
PHI = math.degrees(math.acos(dot(X,Y)/(length(X)*length(Y))))
print("{0:.2f}".format(PHI))

Solution in python#

Approach 1.

Python
from math import sqrt, acos, pi
A = [float(x) for x in raw_input().split()]
B = [float(x) for x in raw_input().split()]
C = [float(x) for x in raw_input().split()]
D = [float(x) for x in raw_input().split()]
def dot(X, Y):
    res = 0
    for i in range(3):
        res += X[i] * Y[i]
    return res
def cross(X, Y):
    return [X[1]*Y[2]-X[2]*Y[1], 
            -X[0]*Y[2]+X[2]*Y[0],
            X[0]*Y[1]-X[1]*Y[0]]
def norm(X):
    return sqrt(X[0]**2 + X[1]**2 + X[2]**2)
def vec(X, Y):
    res = [0, 0, 0]
    for i in range(3):
        res[i] = Y[i] - X[i]
    return res
AB = vec(A, B)
BC = vec(B, C)
CD = vec(C, D)
X = cross(AB, BC)
Y = cross(BC, CD)
cosPHI = dot(X, Y)/norm(X)/norm(Y)
PHI = 180 * acos(cosPHI) / pi
print "%0.2f" % PHI

Approach 2.

Python
from math import acos
a = map(float,raw_input().split(' '))
b = map(float,raw_input().split(' '))
c = map(float,raw_input().split(' '))
d = map(float,raw_input().split(' '))
def dot_product(a,b):
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]
def length(a):
    return (a[0]**2 + a[1]**2 + a[2]**2)**0.5
def cross_product(a,b):
    return [ a[1]*b[2] - a[2]*b[1], a[0]*b[2] - a[2]*b[0], a[0]*b[1] - a[1]*b[0]] 
def diff(a,b):
    return [ a[0]-b[0], a[1]-b[1], a[2]-b[2]]
#a = [0, 4, 5]
#b = [1, 7, 6]
#c = [0, 5, 9]
#d = [1, 7, 2]
ab = diff(a,b)
bc = diff(b,c)
cd = diff(c,d)
x = cross_product(ab,bc)
y = cross_product(bc,cd)
cos_phi = dot_product(x,y) / length(x)/length(y)
phi_rad = acos(cos_phi)
pi = 3.141592653589793
phi = phi_rad*360/2/ pi
print("%.2f" % phi)

Approach 3.

Python
# Enter your code here. Read input from STDIN. Print output to STDOUT
import math
class points(object):
  def __init__(self,x,y,z):
    self.x = x
    self.y = y
    self.z = z
def distance(A,B):
  C = points(0,0,0)
  C.x = A.x - B.x
  C.y = A.y - B.y
  C.z = A.z - B.z
  return C
def crossProduct(AB,BC):
  E = points(0,0,0)
  E.x = AB.y*BC.z - AB.z*BC.y
  E.y = AB.z*BC.x - AB.x*BC.z
  E.z = AB.x*BC.y - AB.y*BC.x
  return E
def dotProduct(X,Y):
  return X.x*Y.x + X.y*Y.y + X.z*Y.z
def modulus(X,Y):
  return math.sqrt((X.x**2+X.y**2+X.z**2)*(Y.x**2+Y.y**2+Y.z**2))
[p,q,r] = [float(i) for i in raw_input().split()]
A = points(p,q,r)
[p,q,r] = [float(i) for i in raw_input().split()]
B = points(p,q,r)
[p,q,r] = [float(i) for i in raw_input().split()]
C = points(p,q,r)
[p,q,r] = [float(i) for i in raw_input().split()]
D = points(p,q,r)
AB = distance(A,B)
BC = distance(B,C)
CD = distance(C,D)
X = crossProduct(AB,BC)
Y = crossProduct(BC,CD)
print "%.2f"%math.degrees(math.acos(dotProduct(X,Y)/modulus(X,Y)))

Keep going

2020

Hackerrank Virtual Functions Solution

This problem is to get you familiar with virtual functions. Create three classes Person, Professor and Student. The class Person should have data members name and age. The classes Professor and Student should inherit…

2020

Hackerrank Exceptional Server Solution

Your friend set up a small computational server that performs complex calculations. It has a function that takes large numbers as its input and returns a numeric result. Unfortunately, there are various exceptions that…

ESC