-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmap_grid.dart
More file actions
433 lines (386 loc) · 13.7 KB
/
Copy pathkmap_grid.dart
File metadata and controls
433 lines (386 loc) · 13.7 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:simplimap/models/implicant.dart';
import 'package:simplimap/state/kmap_provider.dart';
class KMapGrid extends StatelessWidget {
const KMapGrid({super.key});
// Correct Gray Code ordering for 4 variables
static const List<int> _fourVarColOrder = [0, 1, 3, 2];
static const List<int> _fourVarRowOrder = [0, 1, 3, 2];
// Correct Gray Code ordering for 3 variables
static const List<int> _threeVarColOrder = [0, 1, 3, 2];
@override
Widget build(BuildContext context) {
final provider = Provider.of<KMapProvider>(context);
final is3Var = provider.numVariables == 3;
final isDark = Theme.of(context).brightness == Brightness.dark;
final int rows = is3Var ? 2 : 4;
final int cols = is3Var ? 4 : 4;
const panelPadding = 12.0;
const cellGap = 4.0;
final totalColumns = cols + 1;
final textTheme = Theme.of(context).textTheme;
// Labels for the grid
final List<String> rowLabels = is3Var ? ['A', ' '] : ['AB', '00', '01', '11', '10'];
final List<String> colLabels = is3Var ? ['BC', '00', '01', '11', '10'] : ['CD', '00', '01', '11', '10'];
return LayoutBuilder(
builder: (context, constraints) {
final usableWidth = constraints.maxWidth - (panelPadding * 2);
final totalGapWidth = (totalColumns - 1) * cellGap;
final rawCellSize = (usableWidth - totalGapWidth) / totalColumns;
final cellSize = rawCellSize.clamp(24.0, 72.0);
final headerColor = isDark ? const Color(0xFFC8F7E9) : const Color(0xFF1B534D);
final gridBgColor = isDark ? const Color(0xFF0A2F2B) : const Color(0xFFDDF4EE);
final cellOnColor = isDark ? const Color(0xFF19B394) : const Color(0xFF12A58A);
final cellOffColor = isDark ? const Color(0xFF0F3C37) : const Color(0xFFF4FFFB);
final borderColor = isDark ? const Color(0xFF59B8A1) : const Color(0xFF77BCAA);
final valueOnColor = Colors.white;
final valueOffColor = isDark ? Colors.white.withValues(alpha: 0.75) : const Color(0xFF2F5B56);
final mintermColor = isDark ? Colors.white.withValues(alpha: 0.62) : const Color(0xFF4D6F69);
return Container(
decoration: BoxDecoration(
color: gridBgColor.withValues(alpha: isDark ? 0.93 : 0.98),
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: (isDark ? Colors.black : const Color(0xFF2D625A)).withValues(alpha: isDark ? 0.18 : 0.14),
blurRadius: 20,
offset: const Offset(0, 10),
),
],
),
padding: const EdgeInsets.all(panelPadding),
child: Stack(
children: [
Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
for (int index = 0; index < cols + 1; index++) ...[
if (index > 0) const SizedBox(width: cellGap),
SizedBox(
width: cellSize,
height: cellSize,
child: Center(
child: Text(
index == 0 ? (is3Var ? 'A\\BC' : 'AB\\CD') : colLabels[index],
style: textTheme.bodySmall?.copyWith(color: headerColor, fontWeight: FontWeight.w700),
),
),
),
],
],
),
for (int i = 0; i < rows; i++) ...[
if (i > 0) const SizedBox(height: cellGap),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(
width: cellSize,
height: cellSize,
child: Center(
child: Text(
is3Var ? (i == 0 ? '0' : '1') : rowLabels[i + 1],
style: textTheme.bodySmall?.copyWith(color: headerColor, fontWeight: FontWeight.w700),
),
),
),
for (int j = 0; j < cols; j++) ...[
if (j > 0) const SizedBox(width: cellGap),
_buildGridCell(
context,
provider,
i,
j,
is3Var,
cellSize,
cellOnColor,
cellOffColor,
borderColor,
valueOnColor,
valueOffColor,
mintermColor,
),
],
],
),
],
],
),
Positioned.fill(
child: IgnorePointer(
child: CustomPaint(
painter: _KMapOverlayPainter(
implicants: provider.primeImplicants,
is3Var: is3Var,
rows: rows,
cols: cols,
cellSize: cellSize,
cellGap: cellGap,
isDark: isDark,
),
),
),
),
],
),
);
},
);
}
Widget _buildGridCell(
BuildContext context,
KMapProvider provider,
int row,
int col,
bool is3Var,
double cellSize,
Color cellOnColor,
Color cellOffColor,
Color borderColor,
Color valueOnColor,
Color valueOffColor,
Color mintermColor,
) {
final int minterm = _getMintermIndex(row, col, is3Var);
final isDark = Theme.of(context).brightness == Brightness.dark;
final cellValue = minterm < provider.gridState.length ? provider.gridState[minterm] : 0;
final isSelected = cellValue == 1;
final isDontCare = cellValue == 2;
final dontCareColor = isDark ? const Color(0xFFFFA726) : const Color(0xFFFF9500);
return GestureDetector(
onTap: () {
if (minterm < provider.gridState.length) {
provider.cycleCellState(minterm);
}
},
child: Container(
width: cellSize,
height: cellSize,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
border: Border.all(color: borderColor.withValues(alpha: 0.5)),
color: isDontCare
? dontCareColor.withValues(alpha: 0.85)
: (isSelected ? cellOnColor.withValues(alpha: 0.92) : cellOffColor),
),
child: Stack(
children: [
Align(
alignment: Alignment.topLeft,
child: Padding(
padding: const EdgeInsets.all(4),
child: Text(
minterm.toString(),
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: mintermColor,
fontSize: 10,
),
),
),
),
Center(
child: Text(
isDontCare ? 'D' : (isSelected ? '1' : '0'),
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: isDontCare
? Colors.white
: (isSelected ? valueOnColor : valueOffColor),
),
),
),
],
),
),
);
}
int _getMintermIndex(int row, int col, bool is3Var) {
if (is3Var) {
// 3-variable map (2 rows, 4 cols)
final logicalCol = _threeVarColOrder[col];
return (row * 4) + logicalCol;
} else {
// 4-variable map (4 rows, 4 cols)
final logicalRow = _fourVarRowOrder[row];
final logicalCol = _fourVarColOrder[col];
return (logicalRow * 4) + logicalCol;
}
}
}
class _KMapOverlayPainter extends CustomPainter {
_KMapOverlayPainter({
required this.implicants,
required this.is3Var,
required this.rows,
required this.cols,
required this.cellSize,
required this.cellGap,
required this.isDark,
});
final List<Implicant> implicants;
final bool is3Var;
final int rows;
final int cols;
final double cellSize;
final double cellGap;
final bool isDark;
static const List<int> _fourVarColOrder = [0, 1, 3, 2];
static const List<int> _fourVarRowOrder = [0, 1, 3, 2];
static const List<int> _threeVarColOrder = [0, 1, 3, 2];
@override
void paint(Canvas canvas, Size size) {
if (implicants.isEmpty) {
return;
}
final palette = isDark
? <Color>[
const Color(0xFFF9A825),
const Color(0xFF26C6DA),
const Color(0xFFA5D6A7),
const Color(0xFFEF9A9A),
const Color(0xFFCE93D8),
const Color(0xFFFFCC80),
]
: <Color>[
const Color(0xFFE65100),
const Color(0xFF006064),
const Color(0xFF2E7D32),
const Color(0xFFC62828),
const Color(0xFF6A1B9A),
const Color(0xFFAD1457),
];
final sortedImplicants = [...implicants]..sort((a, b) => b.minterms.length.compareTo(a.minterms.length));
for (var i = 0; i < sortedImplicants.length; i++) {
final group = sortedImplicants[i];
final color = palette[i % palette.length];
final strokeWidth = 2.0;
final inset = 2.0 + (i % 3) * 2.0;
final paint = Paint()
..color = color.withValues(alpha: 0.92)
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth;
final coords = <(int, int)>[];
for (final minterm in group.minterms) {
final coord = _mintermToDisplayCell(minterm, is3Var);
if (coord != null) {
coords.add(coord);
}
}
if (coords.isEmpty) {
continue;
}
final rowSet = coords.map((c) => c.$1).toSet();
final colSet = coords.map((c) => c.$2).toSet();
final rowRanges = _findCyclicRanges(rowSet, rows);
final colRanges = _findCyclicRanges(colSet, cols);
// Typical K-map groups are Cartesian products of row and column selections.
final isCartesian = coords.length == rowSet.length * colSet.length;
if (!isCartesian || rowRanges.isEmpty || colRanges.isEmpty) {
for (final coord in coords) {
_drawLoopRect(canvas, paint, inset, coord.$1, coord.$1, coord.$2, coord.$2);
}
continue;
}
for (final rowRange in rowRanges) {
for (final colRange in colRanges) {
_drawLoopRect(canvas, paint, inset, rowRange.$1, rowRange.$2, colRange.$1, colRange.$2);
}
}
}
}
void _drawLoopRect(
Canvas canvas,
Paint paint,
double inset,
int rowStart,
int rowEnd,
int colStart,
int colEnd,
) {
final x = cellSize + colStart * (cellSize + cellGap);
final y = cellSize + rowStart * (cellSize + cellGap);
final colCount = colEnd - colStart + 1;
final rowCount = rowEnd - rowStart + 1;
final width = colCount * cellSize + (colCount - 1) * cellGap;
final height = rowCount * cellSize + (rowCount - 1) * cellGap;
final rect = Rect.fromLTWH(
x + inset,
y + inset,
(width - inset * 2).clamp(6, width),
(height - inset * 2).clamp(6, height),
);
canvas.drawRRect(RRect.fromRectAndRadius(rect, const Radius.circular(12)), paint);
}
List<(int, int)> _findCyclicRanges(Set<int> indexSet, int dimensionSize) {
if (indexSet.isEmpty) {
return [];
}
final sorted = indexSet.toList()..sort();
final k = sorted.length;
if (k == dimensionSize) {
return [(0, dimensionSize - 1)];
}
for (int start = 0; start < dimensionSize; start++) {
final interval = <int>{};
for (int offset = 0; offset < k; offset++) {
interval.add((start + offset) % dimensionSize);
}
if (_sameSet(interval, indexSet)) {
final end = start + k - 1;
if (end < dimensionSize) {
return [(start, end)];
}
final wrappedEnd = end % dimensionSize;
return [
(0, wrappedEnd),
(start, dimensionSize - 1),
];
}
}
// Fallback for non-contiguous selection: draw each index as a separate 1-cell range.
return sorted.map((value) => (value, value)).toList();
}
bool _sameSet(Set<int> a, Set<int> b) {
if (a.length != b.length) {
return false;
}
for (final value in a) {
if (!b.contains(value)) {
return false;
}
}
return true;
}
(int, int)? _mintermToDisplayCell(int minterm, bool is3Var) {
if (is3Var) {
final row = minterm ~/ 4;
final logicalCol = minterm % 4;
final col = _threeVarColOrder.indexOf(logicalCol);
if (col == -1) {
return null;
}
return (row, col);
}
final logicalRow = minterm ~/ 4;
final logicalCol = minterm % 4;
final row = _fourVarRowOrder.indexOf(logicalRow);
final col = _fourVarColOrder.indexOf(logicalCol);
if (row == -1 || col == -1) {
return null;
}
return (row, col);
}
@override
bool shouldRepaint(covariant _KMapOverlayPainter oldDelegate) {
return oldDelegate.implicants != implicants ||
oldDelegate.is3Var != is3Var ||
oldDelegate.rows != rows ||
oldDelegate.cols != cols ||
oldDelegate.cellSize != cellSize ||
oldDelegate.cellGap != cellGap ||
oldDelegate.isDark != isDark;
}
}