Guest User

Untitled

a guest
Nov 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from numpy import dot
  5. from math import cos, sin, radians
  6.  
  7. def main():
  8. t = 1
  9. input_file = 'entrada.txt'
  10. output_file = 'saida2.txt'
  11.  
  12. f = open(input_file, 'r').read().split('\n')
  13. f = f[:-1:]
  14.  
  15. # Escreva a quantidade de vetores 4x1 na saida.
  16.  
  17. with open(output_file, 'a') as o:
  18. o.write(f[0])
  19. o.write('\n')
  20.  
  21. # Definir as funcoes.
  22.  
  23. def Translate(x, y, z):
  24. matrix = [[1, 0, 0, x], [0, 1, 0, y], [0, 0, 1, z], [0, 0, 0, 1]]
  25. return matrix
  26.  
  27.  
  28. def Scale(x, y, z):
  29. matrix = [[x, 0, 0, 0], [0, y, 0, 0], [0, 0, z, 0], [0, 0, 0, 1]]
  30. return matrix
  31.  
  32.  
  33. def Rotate(axis, angle):
  34. if axis == 'X':
  35. matrix = [[1, 0, 0, 0], [0, cos(radians(angle)), -sin(radians(angle)), 0], [0,
  36. sin(radians(angle)), cos(radians(angle)), 0], [0, 0, 0, 1]]
  37. return matrix
  38. elif axis == 'Y':
  39. matrix = [[cos(radians(angle)), 0, -sin(radians(angle)), 0], [0, 1, 0, 0],
  40. [sin(radians(angle)), 0, cos(radians(angle)), 0], [0, 0, 0, 1]]
  41. return matrix
  42. elif axis == 'Z':
  43. matrix = [[cos(radians(angle)), -sin(radians(angle)), 0, 0], [sin(radians(angle)),
  44. cos(radians(angle)), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
  45. return matrix
  46.  
  47.  
  48. # Faca a pre-multiplicacao.
  49.  
  50. flag = 0
  51. for i in range(int(f[0]) + 1, len(f)):
  52. num = f[i].split(' ')
  53. key = num[:1]
  54. num = num[1:]
  55. if key[0] == 'T':
  56. temp = Translate(float(num[0]), float(num[1]), float(num[2]))
  57. if flag == 0:
  58. matrix = temp
  59. else:
  60. matrix = dot(matrix, temp)
  61. flag = 1
  62. elif key[0] == 'S':
  63. temp = Scale(float(num[0]), float(num[1]), float(num[2]))
  64. if flag == 0:
  65. matrix = temp
  66. else:
  67. matrix = dot(matrix, temp)
  68. flag = 1
  69. elif key[0] == 'R':
  70. temp = Rotate(num[0], float(num[1]))
  71. if flag == 0:
  72. matrix = temp
  73. else:
  74. matrix = dot(matrix, temp)
  75. flag = 1
  76.  
  77. # Multiplique e grave.
  78.  
  79. f = f[1:int(f[0])]
  80. for i in range(0, len(f)):
  81. f[i] = f[i].split(' ')
  82. for j in range(0, len(f[i])):
  83. f[i][j] = float(f[i][j])
  84. f[i].append(t)
  85. output = dot(matrix, f[i])
  86. output = list(output)
  87. output = output[:-1:]
  88. for j in range(0,len(output)):
  89. if j != len(output) - 1:
  90. if float(output[j]) - int(output[j]) != 0:
  91. print("%.3f" % output[j], file=open(output_file,"a"),end=' ')
  92. else:
  93. print("%i" % int(output[j]), file=open(output_file,"a"),end=' ')
  94. else:
  95. if float(output[j]) - int(output[j]) != 0:
  96. print("%.3f" % output[j], file=open(output_file,"a"),end='\n')
  97. else:
  98. print("%i" % int(output[j]), file=open(output_file,"a"),end='\n')
  99.  
  100. if __name__ == "__main__":
  101. main()
Add Comment
Please, Sign In to add comment