Skip to content

Commit fa2f500

Browse files
author
Oskar Widmark
committed
feat: spiral visualization
1 parent eb6c5f3 commit fa2f500

File tree

3 files changed

+69
-13
lines changed

3 files changed

+69
-13
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ Fixes:
1515
- [x] Cleaner draw logic with no gaps
1616
- [x] Handle resizing to smaller height correctly
1717
- [ ] Make parallel highlight work for recursive sorting networks
18-
- [ ] Separate highlight types for parallel highlighting to work properly
1918
- [ ] Optimize current 2D color matrix visualization
19+
- [ ] Optimize spiral visualization

src/canvas-controller.ts

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,8 @@ export class CanvasController {
181181
drawIteration?: number,
182182
) => {
183183
if (drawIteration == null || drawIteration !== this.currentDrawIteration) {
184-
if (this.highlightIndices.length) {
185-
for (const idx of this.highlightIndices) {
186-
if (this.context.visualizationType === VisualizationType.Matrix) {
187-
this.redrawCellRow(arr, idx);
188-
this.redrawCellColumn(arr, idx);
189-
continue;
190-
}
191-
this.redrawColumn(arr, idx);
192-
}
193-
}
184+
this.clearHighlights(arr);
194185
this.currentDrawIteration = drawIteration ?? null;
195-
this.highlightIndices = [];
196186
}
197187
this.highlightIndices.push(...indices);
198188

@@ -202,11 +192,20 @@ export class CanvasController {
202192
this.redrawCellColumn(arr, idx, this.getHighlightColor(type));
203193
continue;
204194
}
195+
if (this.context.visualizationType === VisualizationType.Spiral) {
196+
this.redrawCircleSector(arr, idx, this.getHighlightColor(type));
197+
continue;
198+
}
205199
this.redrawColumn(arr, idx, this.getHighlightColor(type));
206200
}
207201
};
208202

209203
redraw = (arr: SortValue[], indices: number[]) => {
204+
// TODO: Optimize
205+
if (this.context.visualizationType === VisualizationType.Spiral) {
206+
this.redrawAll(arr);
207+
return;
208+
}
210209
for (const idx of indices) {
211210
if (this.context.visualizationType === VisualizationType.Matrix) {
212211
this.redrawCellRow(arr, idx);
@@ -312,6 +311,15 @@ export class CanvasController {
312311
this.drawColumn(arr, i, color);
313312
};
314313

314+
private redrawCircleSector = (
315+
arr: SortValue[],
316+
i: number,
317+
color?: string,
318+
) => {
319+
this.drawCircleSector(arr, i, this.context.backgroundColor);
320+
this.drawCircleSector(arr, i, color);
321+
};
322+
315323
private redrawCellColumn = (arr: SortValue[], i: number, color?: string) => {
316324
for (let j = 0; j < this.context.columnNbr; j++) {
317325
this.redrawCell(arr, i, j, color);
@@ -344,6 +352,13 @@ export class CanvasController {
344352
return;
345353
}
346354

355+
if (this.context.visualizationType === VisualizationType.Spiral) {
356+
for (let i = 0; i < arr.length; i++) {
357+
this.drawCircleSector(arr, i);
358+
}
359+
return;
360+
}
361+
347362
for (let i = 0; i < arr.length; i++) {
348363
this.drawColumn(arr, i);
349364
}
@@ -374,6 +389,30 @@ export class CanvasController {
374389
this.fillRect(startX, startY, width, height);
375390
};
376391

392+
private drawCircleSector = (arr: SortValue[], i: number, color?: string) => {
393+
const centerX = this.width / 2;
394+
const centerY = this.height / 2;
395+
const maxRadius = Math.min(centerX, centerY);
396+
const anglePerColumn = (2 * Math.PI) / this.context.columnNbr;
397+
const radius =
398+
(maxRadius / (this.context.columnNbr + 1)) * (arr[i].value + 1);
399+
const startAngle = anglePerColumn * i;
400+
const endAngle = anglePerColumn * (i + 1);
401+
402+
this.canvas2dCtx.fillStyle = color || this.getColumnColor(arr[i].value);
403+
this.canvas2dCtx.beginPath();
404+
this.canvas2dCtx.arc(
405+
centerX,
406+
centerY,
407+
this.snap(radius),
408+
startAngle,
409+
endAngle,
410+
);
411+
this.canvas2dCtx.lineTo(centerX, centerY);
412+
this.canvas2dCtx.closePath();
413+
this.canvas2dCtx.fill();
414+
};
415+
377416
private drawColumn = (arr: SortValue[], i: number, color?: string) => {
378417
const width = this.width / this.context.columnNbr;
379418
const height = this.getColumnHeight(arr[i].value);
@@ -453,9 +492,25 @@ export class CanvasController {
453492
};
454493

455494
endDraw = () => {
456-
console.log('endDraw');
457495
this.isDrawing = false;
458496
this.prevDrawIndex = null;
459497
this.prevDrawHeight = null;
460498
};
499+
500+
private clearHighlights(arr: SortValue[]) {
501+
// TODO: Optimize
502+
if (VisualizationType.Spiral === this.context.visualizationType) {
503+
this.redrawAll(arr);
504+
return;
505+
}
506+
for (const idx of this.highlightIndices) {
507+
if (this.context.visualizationType === VisualizationType.Matrix) {
508+
this.redrawCellRow(arr, idx);
509+
this.redrawCellColumn(arr, idx);
510+
continue;
511+
}
512+
this.redrawColumn(arr, idx);
513+
}
514+
this.highlightIndices = [];
515+
}
461516
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export enum VisualizationType {
5454
Dots = 'Dots',
5555
Colors = 'Colors',
5656
Matrix = 'Matrix',
57+
Spiral = 'Spiral',
5758
}
5859

5960
export enum DisplayType {

0 commit comments

Comments
 (0)