-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
198 lines (159 loc) · 6.67 KB
/
Copy pathvisualizer.py
File metadata and controls
198 lines (159 loc) · 6.67 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""
Array visualization component for displaying algorithm states
"""
# Core GUI imports - tk needed for Canvas (no ttk equivalent)
import tkinter as tk
from tkinter import ttk
class ArrayVisualizer:
def __init__(self, parent, width=800, height=400, colors=None):
"""
Initialize the array visualizer
Args:
parent: Parent tkinter widget
width: Canvas width
height: Canvas height
colors: Dictionary of color scheme
"""
self.parent = parent
self.width = width
self.height = height
self.colors = colors or {
'default': '#3498db',
'comparing': '#e74c3c',
'pivot': '#f39c12',
'sorted': '#27ae60',
'found': '#2ecc71',
'searching': '#9b59b6',
'merging': '#e67e22',
'partitioning': '#16a085'
}
# Create canvas with dark theme styling
self.canvas = tk.Canvas(parent, width=width, height=height, bg='#1e1e1e',
highlightthickness=1, highlightbackground='#404040')
# Array display parameters
self.array = []
self.bars = []
self.bar_width = 0
self.bar_spacing = 2
self.margin = 20
def display_array(self, array, highlights=None):
"""
Display the array as vertical bars
Args:
array: List of values to display
highlights: Dictionary mapping indices to color keys
"""
if not array:
return
self.array = array
highlights = highlights or {}
# Clear previous display
self.canvas.delete("all")
self.bars = []
# Calculate bar dimensions
n = len(array)
available_width = self.width - 2 * self.margin
self.bar_width = max(10, (available_width - (n - 1) * self.bar_spacing) // n)
# If bars are too wide, adjust
if self.bar_width > 50:
self.bar_width = 50
total_width = n * self.bar_width + (n - 1) * self.bar_spacing
x_offset = (self.width - total_width) // 2
# Find max value for scaling
max_val = max(array) if array else 1
min_val = min(array) if array else 0
value_range = max_val - min_val if max_val != min_val else 1
# Maximum bar height (leaving some margin)
max_bar_height = self.height - 2 * self.margin - 40 # Leave space for text
# Draw bars
for i, value in enumerate(array):
# Calculate bar position and height
x = x_offset + i * (self.bar_width + self.bar_spacing)
# Normalize height
normalized_value = (value - min_val) / value_range
bar_height = max(20, normalized_value * max_bar_height) # Minimum height of 20
y = self.height - self.margin - bar_height
# Determine color
if i in highlights:
color = self.colors.get(highlights[i], self.colors['default'])
else:
color = self.colors['default']
# Draw bar with dark theme styling
bar = self.canvas.create_rectangle(
x, y, x + self.bar_width, self.height - self.margin,
fill=color, outline='#555555', width=1
)
self.bars.append(bar)
# Draw value text with theme-appropriate colors
text_y = y - 5 if bar_height > 30 else self.height - self.margin + 15
# Determine text color based on canvas background
canvas_bg = self.canvas['bg']
if canvas_bg == '#1e1e1e': # Dark theme
text_color = '#ffffff' if bar_height > 30 else '#cccccc'
index_color = '#888888'
else: # Light theme
text_color = '#000000' if bar_height > 30 else '#333333'
index_color = '#666666'
# For small arrays, show values
if n <= 50:
self.canvas.create_text(
x + self.bar_width // 2, text_y,
text=str(value), font=('Segoe UI', 8 if n <= 30 else 7 if n <= 40 else 6),
fill=text_color, anchor='s' if bar_height > 30 else 'n'
)
# Draw index for small arrays
if n <= 40:
self.canvas.create_text(
x + self.bar_width // 2, self.height - 5,
text=str(i), font=('Segoe UI', 8),
fill=index_color
)
# Update canvas
self.canvas.update_idletasks()
def resize_canvas_for_array(self, array_size):
"""
Dynamically resize canvas based on array size
Args:
array_size: Number of elements in the array
"""
# Calculate optimal width for the array
min_bar_width = 15 # Minimum readable bar width
bar_spacing = 2
margin = 40 # Total horizontal margin
optimal_width = array_size * min_bar_width + (array_size - 1) * bar_spacing + margin
# Ensure minimum and reasonable maximum width
new_width = max(800, min(optimal_width, 2000))
if new_width != self.width:
self.width = new_width
self.canvas.config(width=new_width)
def highlight_bars(self, indices, color_key='comparing'):
"""
Highlight specific bars
Args:
indices: List of indices to highlight
color_key: Color key from the colors dictionary
"""
if not self.bars:
return
color = self.colors.get(color_key, self.colors['default'])
for i in indices:
if 0 <= i < len(self.bars):
self.canvas.itemconfig(self.bars[i], fill=color)
self.canvas.update_idletasks()
def reset_colors(self):
"""Reset all bars to default color"""
for bar in self.bars:
self.canvas.itemconfig(bar, fill=self.colors['default'])
self.canvas.update_idletasks()
def mark_sorted(self, indices=None):
"""
Mark bars as sorted
Args:
indices: List of indices to mark as sorted (None for all)
"""
if indices is None:
indices = range(len(self.bars))
for i in indices:
if 0 <= i < len(self.bars):
self.canvas.itemconfig(self.bars[i], fill=self.colors['sorted'])
self.canvas.update_idletasks()