Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- from numpy import dot
- from math import cos, sin, radians
- def main():
- t = 1
- input_file = 'entrada.txt'
- output_file = 'saida2.txt'
- f = open(input_file, 'r').read().split('\n')
- f = f[:-1:]
- # Escreva a quantidade de vetores 4x1 na saida.
- with open(output_file, 'a') as o:
- o.write(f[0])
- o.write('\n')
- # Definir as funcoes.
- def Translate(x, y, z):
- matrix = [[1, 0, 0, x], [0, 1, 0, y], [0, 0, 1, z], [0, 0, 0, 1]]
- return matrix
- def Scale(x, y, z):
- matrix = [[x, 0, 0, 0], [0, y, 0, 0], [0, 0, z, 0], [0, 0, 0, 1]]
- return matrix
- def Rotate(axis, angle):
- if axis == 'X':
- matrix = [[1, 0, 0, 0], [0, cos(radians(angle)), -sin(radians(angle)), 0], [0,
- sin(radians(angle)), cos(radians(angle)), 0], [0, 0, 0, 1]]
- return matrix
- elif axis == 'Y':
- matrix = [[cos(radians(angle)), 0, -sin(radians(angle)), 0], [0, 1, 0, 0],
- [sin(radians(angle)), 0, cos(radians(angle)), 0], [0, 0, 0, 1]]
- return matrix
- elif axis == 'Z':
- matrix = [[cos(radians(angle)), -sin(radians(angle)), 0, 0], [sin(radians(angle)),
- cos(radians(angle)), 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
- return matrix
- # Faca a pre-multiplicacao.
- flag = 0
- for i in range(int(f[0]) + 1, len(f)):
- num = f[i].split(' ')
- key = num[:1]
- num = num[1:]
- if key[0] == 'T':
- temp = Translate(float(num[0]), float(num[1]), float(num[2]))
- if flag == 0:
- matrix = temp
- else:
- matrix = dot(matrix, temp)
- flag = 1
- elif key[0] == 'S':
- temp = Scale(float(num[0]), float(num[1]), float(num[2]))
- if flag == 0:
- matrix = temp
- else:
- matrix = dot(matrix, temp)
- flag = 1
- elif key[0] == 'R':
- temp = Rotate(num[0], float(num[1]))
- if flag == 0:
- matrix = temp
- else:
- matrix = dot(matrix, temp)
- flag = 1
- # Multiplique e grave.
- f = f[1:int(f[0])]
- for i in range(0, len(f)):
- f[i] = f[i].split(' ')
- for j in range(0, len(f[i])):
- f[i][j] = float(f[i][j])
- f[i].append(t)
- output = dot(matrix, f[i])
- output = list(output)
- output = output[:-1:]
- for j in range(0,len(output)):
- if j != len(output) - 1:
- if float(output[j]) - int(output[j]) != 0:
- print("%.3f" % output[j], file=open(output_file,"a"),end=' ')
- else:
- print("%i" % int(output[j]), file=open(output_file,"a"),end=' ')
- else:
- if float(output[j]) - int(output[j]) != 0:
- print("%.3f" % output[j], file=open(output_file,"a"),end='\n')
- else:
- print("%i" % int(output[j]), file=open(output_file,"a"),end='\n')
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment