forked from marcosfede/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube_ufo.py
48 lines (40 loc) · 1.15 KB
/
cube_ufo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from math import atan, sqrt, sin, cos
def dot(matrix, vector):
sol = []
for row in matrix:
total = 0
for left, right in zip(row, vector):
total += left * right
sol.append(total)
return sol
def solve(A):
# vectors to the faces of the cube
p1 = (0.5, 0, 0)
p2 = (0, 0.5, 0)
p3 = (0, 0, 0.5)
sqrt2 = sqrt(2)
# u is the unitary along rotation axis (-0.5, 0, 0.5)
ux = -1 * 0.5 * sqrt2
uz = 0.5 * sqrt2
# inverse of the relation between area and angle
theta = 2 * atan((sqrt2 - sqrt(3 - A ** 2)) / (A + 1))
c, s = cos(theta), sin(theta)
# R is the rotation matrix
R = [
[c + ux ** 2 * (1 - c), -uz * s, ux * uz * (1 - c)],
[uz * s, c, -ux * s],
[ux * uz * (1 - c), ux * s, c + uz ** 2 * (1 - c)]
]
p1r = dot(R, p1)
p2r = dot(R, p2)
p3r = dot(R, p3)
return [p1r, p2r, p3r]
def read_input():
ncases = int(input())
for case in range(1, ncases + 1):
area = float(input())
sol = solve(area)
print("Case #{}:".format(case))
for row in sol:
print(" ".join(map(str, row)))
read_input()