-
Notifications
You must be signed in to change notification settings - Fork 0
/
z_buffer.py
123 lines (93 loc) · 3.24 KB
/
z_buffer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import tkinter as tk
import numpy as np
import math
class App:
def __init__(self, root):
self.master = root
self.z_buffer = np.full((1366, 768), -100000)
self.color_buffer = np.empty((1366, 768), dtype=object)
self.fill_buffer()
self.canvas = tk.Canvas(self.master, height=768, width=1366)
self.canvas.grid(row=0, column=0)
self.center_x = int(1366/2)
self.center_y = int(768/2)
self.fill_buffer()
self.draw_obj_1()
self.draw_obj_2()
self.draw_obj_3()
self.draw_obj_4()
self.draw_obj_5()
self.draw_color_buffer()
def fill_buffer(self):
for i in range(0, 1366):
for j in range(0, 768):
self.color_buffer[i][j] = '#000000'
def draw_color_buffer(self):
if self.canvas:
self.canvas.delete('all')
self.canvas = None
self.canvas = tk.Canvas(self.master, height=768, width=1366,
background="#000000")
self.canvas.grid(row=0, column=0)
for line in range(len(self.color_buffer)):
for column in range(len(self.color_buffer[line])):
# print(f'{line}, {column} ')
if self.color_buffer[line][column]!='#000000':
self.canvas.create_line(line, column, line+1,column,
fill=self.color_buffer[line][column])
def draw_obj_1(self):
x_points = [i for i in range(10, 31)]
y_points = [i for i in range(20, 41)]
color = '#0000ff'
for x in x_points:
for y in y_points:
z = x*x + y
if self.z_buffer[x+self.center_x][self.center_y-y] < z:
# print(f'x: {x} y:{y}')
self.z_buffer[x+self.center_x][self.center_y-y] = z
self.color_buffer[x+self.center_x][self.center_y-y] = color
def draw_obj_2(self):
x_points = [i for i in range(50, 101)]
y_points = [i for i in range(30, 81)]
color = '#ff0000'
for x in x_points:
for y in y_points:
z = 3*x + 2*y + 5
if self.z_buffer[x+self.center_x][self.center_y-y] < z:
self.z_buffer[x+self.center_x][self.center_y-y] = z
self.color_buffer[x+self.center_x][self.center_y-y] = color
def draw_obj_3(self):
color = '#ffff00'
for t in range(0, 51):
for alpha in np.arange(0, 2*math.pi, 0.01):
# print(alpha)
z = 10+t
x = int(30 + t * math.cos(alpha))
y = int(50 + t*math.sin(alpha))
# print(f'x: {x}, y:{y}')
# print(f'x:{self.center_x}, y:{self.center_y}')
if self.z_buffer[x+self.center_x][self.center_y-y] < z:
self.z_buffer[x+self.center_x][self.center_y-y] = z
self.color_buffer[x+self.center_x][self.center_y-y] = color
def draw_obj_4(self):
color = '#00ff00'
for alpha in np.arange(0, 2*math.pi, .01):
for beta in np.arange(0, 2*math.pi, .01):
z = 20+30*math.sin(alpha)
x = int(100+30*math.cos(alpha)*math.cos(beta))
y = int(50+30*math.cos(alpha)*math.sin(beta))
if (self.z_buffer[self.center_x+x][self.center_y-y] < z):
self.z_buffer[x+self.center_x][self.center_y-y] = z
self.color_buffer[x+self.center_x][self.center_y-y] = color
def draw_obj_5(self):
color = '#ffffff'
for x in range(0,41):
for y in range(0, 41):
for z in range(0, 41):
if(self.z_buffer[x+self.center_x][self.center_y-y] < z):
self.z_buffer[self.center_x+x][self.center_y-y] = z
self.color_buffer[self.center_x+x][self.center_y-y] = color
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
root.mainloop()