Skip to content

Commit 76d089d

Browse files
committed
Update canvasAnalysis.ts
1 parent cf461eb commit 76d089d

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

e2e/logic/canvasAnalysis.ts

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface CanvasAnalysisResult {
88
green: Array<{ x: number; y: number; radius: number }>;
99
}
1010

11-
export async function analyzeCanvasWithLocator(locator: Locator) {
11+
export async function analyzeCanvasNodes(locator: Locator): Promise<CanvasAnalysisResult> {
1212
const canvasHandle = await locator.evaluateHandle((canvas) => canvas as HTMLCanvasElement);
1313
const canvasElement = await canvasHandle.asElement();
1414
if (!canvasElement) {
@@ -58,64 +58,70 @@ export async function analyzeCanvasWithLocator(locator: Locator) {
5858
const clusterNodes = (pixels: Pixel[], radius: number): Pixel[][] => {
5959
const visited = new Set<string>();
6060
const clusters: Pixel[][] = [];
61-
61+
6262
pixels.forEach((pixel) => {
6363
const key = `${pixel.x},${pixel.y}`;
6464
if (visited.has(key)) return;
65-
65+
6666
const cluster: Pixel[] = [];
6767
const stack: Pixel[] = [pixel];
68-
68+
6969
while (stack.length > 0) {
7070
const current = stack.pop()!;
7171
const currentKey = `${current.x},${current.y}`;
7272
if (visited.has(currentKey)) continue;
73-
73+
7474
visited.add(currentKey);
7575
cluster.push(current);
76-
76+
7777
pixels.forEach((neighbor) => {
7878
const dist = Math.sqrt(
7979
(current.x - neighbor.x) ** 2 + (current.y - neighbor.y) ** 2
8080
);
81-
if (dist <= radius) stack.push(neighbor);
81+
if (dist <= radius && !visited.has(`${neighbor.x},${neighbor.y}`)) {
82+
stack.push(neighbor);
83+
}
8284
});
8385
}
84-
86+
8587
clusters.push(cluster);
8688
});
87-
89+
8890
return clusters;
8991
};
92+
93+
9094

9195
const mergeCloseClusters = (clusters: Pixel[][], mergeRadius: number): Pixel[][] => {
9296
const mergedClusters: Pixel[][] = [];
93-
const used = new Set<number>();
94-
97+
const usedClusters = new Set<number>();
98+
9599
for (let i = 0; i < clusters.length; i++) {
96-
if (used.has(i)) continue;
97-
100+
if (usedClusters.has(i)) continue;
101+
98102
let merged = [...clusters[i]];
103+
99104
for (let j = i + 1; j < clusters.length; j++) {
100-
if (used.has(j)) continue;
101-
105+
if (usedClusters.has(j)) continue;
106+
102107
const dist = Math.sqrt(
103108
(merged[0].x - clusters[j][0].x) ** 2 +
104109
(merged[0].y - clusters[j][0].y) ** 2
105110
);
106-
111+
107112
if (dist <= mergeRadius) {
108-
merged = merged.concat(clusters[j]);
109-
used.add(j);
113+
merged = [...new Set([...merged, ...clusters[j]])]; // Deduplicate pixels
114+
usedClusters.add(j);
110115
}
111116
}
112-
117+
113118
mergedClusters.push(merged);
114-
used.add(i);
119+
usedClusters.add(i);
115120
}
116-
121+
117122
return mergedClusters;
118123
};
124+
119125

120126
const redClusters = clusterNodes(redPixels, adjustedRadius);
121127
const yellowClusters = clusterNodes(yellowPixels, adjustedRadius);
@@ -155,4 +161,4 @@ export async function analyzeCanvasWithLocator(locator: Locator) {
155161

156162
await canvasHandle.dispose();
157163
return result;
158-
}
164+
}

0 commit comments

Comments
 (0)