forked from ZQPei/Sorting_Visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
117 lines (94 loc) · 3.38 KB
/
Copy pathdata.py
File metadata and controls
117 lines (94 loc) · 3.38 KB
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
import random
import numpy as np
import time
import cv2
class DataSeq:
WHITE = (255,255,255)
RED = (0,0,255)
BLACK = (0,0,0)
YELLOW = (0,127,255)
def __init__(self, Length, time_interval=1, sort_title="Figure", repeatition=False):
self.data = [x for x in range(Length)]
if repeatition:
self.data = random.choices(self.data, k=Length)
else:
self.Shuffle()
self.length = Length
self.SetTimeInterval(time_interval)
self.SetSortType(sort_title)
self.Getfigure()
self.InitTime()
self.Visualize()
def InitTime(self):
self.start=time.time()
self.time=0
self.StopTimer()
def StartTimer(self):
self.start_flag=True
self.start = time.time()
def StopTimer(self):
self.start_flag=False
def GetTime(self):
if self.start_flag:
self.time = time.time()-self.start
def SetTimeInterval(self, time_interval):
self.time_interval=time_interval
def SetSortType(self, sort_title):
self.sort_title=sort_title
def Shuffle(self):
random.shuffle(self.data)
def Getfigure(self):
_bar_width = 5
figure = np.full((self.length*_bar_width,self.length*_bar_width,3), 255,dtype=np.uint8)
for i in range(self.length):
val = self.data[i]
figure[-1-val*_bar_width:, i*_bar_width:i*_bar_width+_bar_width] = self.GetColor(val, self.length)
self._bar_width = _bar_width
self.figure = figure
@staticmethod
def GetColor(val, TOTAL):
return (120+val*255//(2*TOTAL), 255-val*255//(2*TOTAL), 0)
def _set_figure(self, idx, val):
min_col = idx*self._bar_width
max_col = min_col+self._bar_width
min_row = -1-val*self._bar_width
self.figure[ : , min_col:max_col] = self.WHITE
self.figure[ min_row: , min_col:max_col] = self.GetColor(val, self.length)
def SetColor(self, img, marks, color):
for idx in marks:
min_col = idx*self._bar_width
max_col = min_col+self._bar_width
min_row = -1-self.data[idx]*self._bar_width
img[min_row:, min_col:max_col] = color
def Mark(self, img, marks, color):
self.SetColor(img, marks, color)
def SetVal(self, idx, val):
self.data[idx] = val
self._set_figure(idx, val)
self.Visualize((idx,))
def Swap(self, idx1, idx2):
self.data[idx1], self.data[idx2] = self.data[idx2], self.data[idx1]
self._set_figure(idx1, self.data[idx1])
self._set_figure(idx2, self.data[idx2])
self.Visualize((idx1, idx2))
def Visualize(self, mark1=None, mark2=None):
img = self.figure.copy()
if mark2:
self.Mark( img, mark2, self.YELLOW)
if mark1:
self.Mark( img, mark1, self.RED)
if img.shape[1] > 500:
img = cv2.resize(img, (500,500))
self.GetTime()
cv2.putText(img, self.sort_title+" Time:%02.2fs"%self.time, (20,20), cv2.FONT_HERSHEY_PLAIN, 1, self.YELLOW, 1)
cv2.imshow(self.sort_title, img)
cv2.waitKey(self.time_interval)
if __name__ == "__main__":
ds = DataSeq(64)
ds.Visualize()
for i in range(64):
for j in range(i,64):
if ds.data[i]>ds.data[j]:
ds.Swap(i,j)
ds.Visualize( (j,i) )
ds.Visualize()