-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmp
167 lines (145 loc) · 6.77 KB
/
tmp
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# !usr/bin/python
import numpy as np
import time
import sys
if sys.version_info.major == 2:
import Tkinter as tk
else:
import tkinter as tk
# set the block SIZE
POINT_SIZE = 4
SAMPLE_SIZE = 20
BLOCK_SIZE = 8
WIDTH_NUM = 5
HEIGHT_NUM = 5
SAMPLE_SIZE_THR = 16
BLOCK_LINE_WIDTH = 2
SUB_BLOCK_LINE_WIDTH = 1
class GUI(tk.Tk, object):
def __init__(self, vertexs):
super(GUI, self).__init__()
self.sample_size = SAMPLE_SIZE
self.width_num = WIDTH_NUM
self.height_num = HEIGHT_NUM
self.origin_vertex = vertexs
self.h_offset = 0
self.v_offset = 0
self.title('Triangle Viewer')
self._build_gui()
self.bind('<Key>', self.key_drawback)
self._update()
def _update(self):
self.canvas.delete('all')
self.geometry('{0}x{1}'.format(self.width_num * BLOCK_SIZE * self.sample_size,
self.height_num * BLOCK_SIZE * self.sample_size))
self._build_triangle(self.origin_vertex)
self._draw_blocks()
self._draw_triangle()
self.update()
def key_drawback(self, event):
if( event.char is '+'):
if( self.width_num >=2 and self.height_num >= 2):
self.sample_size = int(self.sample_size * 2)
self.width_num = int(self.width_num/2)
self.height_num = int(self.height_num/2)
elif( event.char is '-'):
if(self.sample_size >= 4) :
self.sample_size = int(self.sample_size / 2)
self.width_num = int(self.width_num * 2)
self.height_num = int(self.height_num * 2)
elif( event.char is 'd'):
self.h_offset = self.h_offset + 1
elif( event.char is 'a'):
self.h_offset = self.h_offset - 1
elif( event.char is 's'):
self.v_offset = self.v_offset + 1
elif( event.char is 'w'):
self.v_offset = self.v_offset - 1
self._update()
print(event.char)
def _build_gui(self):
self.canvas = tk.Canvas(self, bg='white',
height=self.height_num * BLOCK_SIZE * self.sample_size,
width=self.width_num * BLOCK_SIZE * self.sample_size)
# pack all
self.canvas.pack()
## sample is 1x1
def _build_sample(self, sample_x, sample_y, color):
l_x = sample_x + self.sample_size/2 - POINT_SIZE/3
r_x = sample_x + self.sample_size/2 + POINT_SIZE/2
l_y = sample_y + self.sample_size/2 - POINT_SIZE/2
r_y = sample_y + self.sample_size/2 + POINT_SIZE/2
self.canvas.create_oval(l_x, l_y, r_x, r_y, fill = color)
## quad is 2x2
def _build_quad(self, quad_x, quad_y):
self._build_sample(quad_x, quad_y, 'red')
self._build_sample(quad_x + self.sample_size, quad_y, 'green')
self._build_sample(quad_x, quad_y + self.sample_size, 'blue')
self._build_sample(quad_x + self.sample_size, quad_y + self.sample_size, 'yellow')
## sub_block is 4x4
def _build_sub_block(self, sub_b_x, sub_b_y):
if self.sample_size >= SAMPLE_SIZE_THR:
quad_offset = 2 * self.sample_size
self._build_quad(sub_b_x, sub_b_y)
self._build_quad(sub_b_x + quad_offset, sub_b_y)
self._build_quad(sub_b_x, sub_b_y + quad_offset)
self._build_quad(sub_b_x + quad_offset, sub_b_y + quad_offset)
else:
sub_b_offset = 4 * self.sample_size
self.canvas.create_line(sub_b_x, sub_b_y, sub_b_x + sub_b_offset, sub_b_y, width = SUB_BLOCK_LINE_WIDTH)
self.canvas.create_line(sub_b_x, sub_b_y, sub_b_x, sub_b_y + sub_b_offset, width = SUB_BLOCK_LINE_WIDTH)
self.canvas.create_line(sub_b_x + sub_b_offset, sub_b_y + sub_b_offset, sub_b_x + sub_b_offset, sub_b_y, width = SUB_BLOCK_LINE_WIDTH)
self.canvas.create_line(sub_b_x + sub_b_offset, sub_b_y + sub_b_offset, sub_b_x, sub_b_y + sub_b_offset, width = SUB_BLOCK_LINE_WIDTH)
## block is 8x8
def _build_block(self, b_x, b_y):
sub_b_offset = 4 * self.sample_size
self._build_sub_block(b_x, b_y)
self._build_sub_block(b_x + sub_b_offset, b_y)
self._build_sub_block(b_x, b_y + sub_b_offset)
self._build_sub_block(b_x + sub_b_offset, b_y + sub_b_offset)
b_offset = 8 * self.sample_size
self.canvas.create_line(b_x, b_y, b_x + b_offset, b_y, width = BLOCK_LINE_WIDTH)
self.canvas.create_line(b_x, b_y, b_x, b_y + b_offset, width = BLOCK_LINE_WIDTH)
self.canvas.create_line(b_x + b_offset, b_y + b_offset, b_x + b_offset, b_y, width = BLOCK_LINE_WIDTH)
self.canvas.create_line(b_x + b_offset, b_y + b_offset, b_x, b_y + b_offset, width = BLOCK_LINE_WIDTH)
def _draw_triangle(self):
coord_x = [self.vertexs[2*i] + self.offset_x for i in range(3)]
coord_y = [self.vertexs[2*i+1] + self.offset_y for i in range(3)]
self.canvas.create_line(coord_x[0], coord_y[0], coord_x[1], coord_y[1], fill = 'red', width=2)
self.canvas.create_line(coord_x[1], coord_y[1], coord_x[2], coord_y[2], fill = 'red', width=2)
self.canvas.create_line(coord_x[2], coord_y[2], coord_x[0], coord_y[0], fill = 'red', width=2)
def _draw_blocks(self):
## draw block one by one
x0 = int((0 - self.offset_x)/BLOCK_SIZE/self.sample_size) - 1
y0 = int((0 - self.offset_y)/BLOCK_SIZE/self.sample_size) - 1
x1 = self.width_num - int(self.offset_x/BLOCK_SIZE/self.sample_size) + 1
y1 = self.height_num - int(self.offset_y/BLOCK_SIZE/self.sample_size) + 1
print(x0, y0, x1, y1)
for i in range(x0, x1):
block_x = i * 8 * self.sample_size + self.offset_x
for j in range(y0, y1):
block_y = j * 8 * self.sample_size + self.offset_y
self._build_block(block_x, block_y)
def _build_triangle(self, vertexs):
self.vertexs = [ i * self.sample_size for i in vertexs]
self._triangle_center()
self._triangle_offset()
def _triangle_center(self):
list_x = [self.vertexs[2*i] for i in range(3)]
list_y = [self.vertexs[2*i+1] for i in range(3)]
self.center_x = (min(list_x) + max(list_x))/2
self.center_y = (min(list_y) + max(list_y))/2
print(self.center_x, self.center_y)
def _triangle_offset(self):
self.offset_x = (self.width_num + self.h_offset) * BLOCK_SIZE * self.sample_size/2 - self.center_x
self.offset_y = (self.height_num + self.v_offset) * BLOCK_SIZE * self.sample_size/2 - self.center_y
print(self.offset_x, self.offset_y)
if __name__ == '__main__':
v0_x = 10.45
v0_y = 12.78
v1_x = 34.8
v1_y = 27.23
v2_x = 14.21
v2_y = 45.2
env = GUI([v0_x, v0_y, v1_x, v1_y, v2_x, v2_y])
env.mainloop()