-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
94 lines (82 loc) · 2.8 KB
/
display.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import subprocess
from os import remove, execlp
#constants
XRES = 500
YRES = 500
MAX_COLOR = 255
RED = 0
GREEN = 1
BLUE = 2
DEFAULT_COLOR = [0, 0, 0]
def new_screen( width = XRES, height = YRES ):
screen = []
for y in range( height ):
row = []
screen.append( row )
for x in range( width ):
screen[y].append( DEFAULT_COLOR[:] )
return screen
def new_zbuffer( width = XRES, height = YRES ):
zb = []
for y in range( height ):
row = [ float('-inf') for x in range(width) ]
zb.append( row )
return zb
def plot( screen, zbuffer, color, x, y, z ):
newy = YRES - 1 - y
z = int((z * 1000)) / 1000.0
if ( x >= 0 and x < XRES and newy >= 0 and newy < YRES and zbuffer[newy][x] <= z):
screen[newy][x] = color[:]
zbuffer[newy][x] = z
def clear_screen( screen ):
for y in range( len(screen) ):
for x in range( len(screen[y]) ):
screen[y][x] = DEFAULT_COLOR[:]
def clear_zbuffer( zb ):
for y in range( len(zb) ):
for x in range( len(zb[y]) ):
zb[y][x] = float('-inf')
def save_ppm( screen, fname ):
f = open( fname, 'w' )
ppm = 'P3\n' + str(len(screen[0])) +' '+ str(len(screen)) +' '+ str(MAX_COLOR) +'\n'
for y in range( len(screen) ):
row = ''
for x in range( len(screen[y]) ):
pixel = screen[y][x]
row+= str( pixel[ RED ] ) + ' '
row+= str( pixel[ GREEN ] ) + ' '
row+= str( pixel[ BLUE ] ) + ' '
ppm+= row + '\n'
f.write( ppm )
f.close()
# def save_ppm( screen, fname ):
# f = open( fname, 'w' )
# ppm = 'P3\n' + str(len(screen[0])) +' '+ str(len(screen)) +' '+ str(MAX_COLOR) +'\n'
# rows = []
# for y in range( len(screen) ):
# row = []
# for x in range( len(screen[y]) ):
# pixel = screen[y][x]
# row.append(' '.join([str(x) for x in pixel]))
# rows.append(' '.join(row))
# ppm+= '\n'.join(rows)
# print ppm
# f.write( ppm )
# f.close()
def save_extension( screen, fname ):
ppm_name = fname[:fname.find('.')] + '.ppm'
save_ppm( screen, ppm_name )
p = subprocess.Popen( ['magick', 'convert', ppm_name, fname ], stdin=subprocess.PIPE, stdout = subprocess.PIPE )
p.communicate()
remove(ppm_name)
def display( screen ):
ppm_name = 'pic.ppm'
save_ppm( screen, ppm_name )
p = subprocess.Popen( ['imdisplay', ppm_name], stdin=subprocess.PIPE, stdout = subprocess.PIPE )
p.communicate()
remove(ppm_name)
def make_animation( name ):
name_arg = 'anim/' + name + '*'
name = name + '.gif'
print('Saving animation as ' + name)
subprocess.call(["magick", "convert", "-delay", "1.7", name_arg, name])