-
Notifications
You must be signed in to change notification settings - Fork 14
/
cubic-ufo.py
52 lines (45 loc) · 1.5 KB
/
cubic-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
49
50
51
52
# Copyright (c) 2018 kamyu. All rights reserved.
#
# Google Code Jam 2018 Qualification Round - Problem D. Cubic UFO
# https://codingcompetitions.withgoogle.com/codejam/round/00000000000000cb/00000000000079cc
#
# Time: O(1)
# Space: O(1)
#
import math
def matrix_multi(A, B):
result = [[0.0 for _ in xrange(len(B[0]))] for _ in xrange(len(A))]
for i in xrange(len(A)):
for k in xrange(len(A[0])):
if A[i][k] == 0.0:
continue
for j in xrange(len(B[0])):
result[i][j] += A[i][k] * B[k][j]
return result
def rotate_y(matrix, cosx):
sinx = math.sqrt(1.0-cosx**2)
Ry = [[cosx, 0.0, -sinx],
[ 0.0, 1.0, 0.0],
[sinx, 0.0, cosx]]
return matrix_multi(matrix, Ry)
def rotate_x(matrix, cosx):
sinx = math.sqrt(1.0-cosx**2)
Rx = [[1.0, 0.0, 0.0],
[0.0, cosx, sinx],
[0.0, -sinx, cosx]]
return matrix_multi(matrix, Rx)
def cubic_ufo():
A = float(input())
matrix = [[0.5, 0.0, 0.0],
[0.0, 0.5, 0.0],
[0.0, 0.0, 0.5]]
matrix = rotate_y(matrix, 1.0/math.sqrt(2)) # rotate y by 45 degrees
# rotate x to make shadows = rectangle projection part + two triangle projection part on xz plane
# => A = sqrt(2)*sinx + cosx
cosx = (A + math.sqrt(2*(3-A**2)))/3
matrix = rotate_x(matrix, cosx)
return matrix
for case in xrange(input()):
print 'Case #%d:' % (case+1)
for center in cubic_ufo():
print " ".join(map(str, center))