All files / src/core/particles particle-system.ts

21.26% Statements 101/475
44.44% Branches 8/18
23.52% Functions 4/17
21.26% Lines 101/475

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 6501x                           1x 3x 3x 3x   3x 3x 3x   3x                                                                                       3x                                         3x                   3x                                                                                       3x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x 100x   100x   100x           100x 100x   100x   100x 600x 600x 100x   100x         100x   100x 100x   100x 500x 500x 200x 200x 500x     500x   100x 100x 100x       100x       100x                                                                                                                                                                                                                                                             100x 100x   100x 100x   100x       100x   100x 100x 100x   100x 100x     100x 100x       100x   3x 500x 500x     500x 500x     500x 500x     500x 500x   500x 500x     500x 500x 500x   500x 500x   500x 500x   500x 500x 500x   500x 500x 500x   500x 500x     3x                                                                                                                                             3x                                                                         3x                                                 3x                                                                                                                                                   3x                 3x                       3x           3x       3x                 3x  
import { Graphics, Container } from 'pixi.js';
import { Point, EdgeInfo, EdgeSelectionRule, OrbitAlgorithm } from '../../types';
import { edgesEqual, getOtherPoint, pointsClose } from '../utils/geometry';
import { Grid } from '../grid';
 
export interface Particle {
  x: number;
  y: number;
  currentEdge: EdgeInfo;
  direction: number; // 1 = toward points[1], -1 = toward points[0]
  progress: number; // 0 to 1 along current edge
  previousEdge: EdgeInfo | null;
}
 
export class ParticleSystem {
  private particles: Particle[] = [];
  private particleContainer: Container;
  private particleGraphics: Map<Particle, Graphics> = new Map();
 
  constructor(container: Container) {
    this.particleContainer = container;
  }
 
  spawnParticle(edge: EdgeInfo, clickX: number, clickY: number): void {
    const p1 = edge.points[0];
    const p2 = edge.points[1];
    
    // Calculate distance to each vertex
    const dist1 = Math.sqrt((clickX - p1.x) ** 2 + (clickY - p1.y) ** 2);
    const dist2 = Math.sqrt((clickX - p2.x) ** 2 + (clickY - p2.y) ** 2);
    
    // Determine direction (toward closer vertex)
    const direction = dist1 < dist2 ? -1 : 1;
    
    // Calculate initial progress along edge
    const edgeLength = Math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2);
    let progress: number;
    if (direction === -1) {
      // Moving toward p1
      const distFromP1 = dist1;
      progress = 1 - (distFromP1 / edgeLength);
    } else {
      // Moving toward p2
      const distFromP2 = dist2;
      progress = distFromP2 / edgeLength;
    }
    
    // Clamp progress
    progress = Math.max(0, Math.min(1, progress));
    
    // Calculate initial position
    const x = p1.x + (p2.x - p1.x) * progress;
    const y = p1.y + (p2.y - p1.y) * progress;
    
    const particle: Particle = {
      x,
      y,
      currentEdge: edge,
      direction,
      progress,
      previousEdge: null,
    };
    
    this.particles.push(particle);
    this.createParticleGraphics(particle);
  }
 
  spawnParticleOnEdge(edge: EdgeInfo, progress: number, direction: number): void {
    const p1 = edge.points[0];
    const p2 = edge.points[1];
    const clampedProgress = Math.max(0, Math.min(1, progress));
 
    const x = p1.x + (p2.x - p1.x) * clampedProgress;
    const y = p1.y + (p2.y - p1.y) * clampedProgress;
 
    const particle: Particle = {
      x,
      y,
      currentEdge: edge,
      direction: direction >= 0 ? 1 : -1,
      progress: clampedProgress,
      previousEdge: null,
    };
 
    this.particles.push(particle);
    this.createParticleGraphics(particle);
  }
 
  private createParticleGraphics(particle: Particle): void {
    const graphics = new Graphics();
    graphics.circle(0, 0, 4);
    graphics.fill({ color: 0x00ff00 });
    graphics.x = particle.x;
    graphics.y = particle.y;
    this.particleContainer.addChild(graphics);
    this.particleGraphics.set(particle, graphics);
  }
 
  update(deltaTime: number, particleSpeed: number, gridWidth: number, gridHeight: number, grid: Grid, edgeSelectionRule: EdgeSelectionRule, mouseX: number, mouseY: number, cellStates: number[][], orbitDistance: number, orbitAlgorithm: OrbitAlgorithm, orbitEpsilon: number): void {
    const distancePerSecond = particleSpeed; // units per second
    const distanceThisFrame = (distancePerSecond * deltaTime) / 1000; // convert ms to seconds
    
    for (let i = this.particles.length - 1; i >= 0; i--) {
      const particle = this.particles[i];
      const edge = particle.currentEdge;
      const p1 = edge.points[0];
      const p2 = edge.points[1];
      
      // Calculate edge length
      const edgeLength = Math.sqrt((p2.x - p1.x) ** 2 + (p2.y - p1.y) ** 2);
      
      // Move particle along edge
      if (particle.direction === 1) {
        // Moving toward p2
        particle.progress += distanceThisFrame / edgeLength;
      } else {
        // Moving toward p1
        particle.progress -= distanceThisFrame / edgeLength;
      }
      
      // Check if reached vertex
      if (particle.progress >= 1) {
        // Reached p2
        this.handleVertexArrival(particle, p2, grid, gridWidth, gridHeight, edgeSelectionRule, mouseX, mouseY, cellStates, orbitDistance, orbitAlgorithm, orbitEpsilon);
      } else if (particle.progress <= 0) {
        // Reached p1
        this.handleVertexArrival(particle, p1, grid, gridWidth, gridHeight, edgeSelectionRule, mouseX, mouseY, cellStates, orbitDistance, orbitAlgorithm, orbitEpsilon);
      } else {
        // Update position along edge
        particle.x = p1.x + (p2.x - p1.x) * particle.progress;
        particle.y = p1.y + (p2.y - p1.y) * particle.progress;
        
        // Update graphics
        const graphics = this.particleGraphics.get(particle);
        if (graphics) {
          graphics.x = particle.x;
          graphics.y = particle.y;
        }
      }
    }
  }
 
  private handleVertexArrival(
    particle: Particle,
    vertex: Point,
    grid: Grid,
    gridWidth: number,
    gridHeight: number,
    edgeSelectionRule: EdgeSelectionRule,
    mouseX: number,
    mouseY: number,
    cellStates: number[][],
    orbitDistance: number,
    orbitAlgorithm: OrbitAlgorithm,
    orbitEpsilon: number
  ): void {
    // Find all edges connected to this vertex
    const connectedEdges = grid.getEdgesAtVertex(vertex, gridWidth, gridHeight);
    
    if (connectedEdges.length === 0) {
      // No edges found, remove particle
      this.removeParticle(particle);
      return;
    }
    
    let availableEdges: EdgeInfo[];
    let nextEdge: EdgeInfo;
    
    if (edgeSelectionRule === 'randomNoBacktrack' || edgeSelectionRule === 'highestEdgeDelta') {
      // Filter out the edge we're currently on (the one we just arrived from)
      availableEdges = connectedEdges.filter(edge => {
        if (!particle.currentEdge) return true;
        return !edgesEqual(particle.currentEdge, edge);
      });
      
      if (availableEdges.length === 0) {
        this.removeParticle(particle);
        return;
      }
      
      if (edgeSelectionRule === 'highestEdgeDelta') {
        // Find edges with highest delta
        let maxDelta = -1;
        let bestEdges: EdgeInfo[] = [];
        
        for (const edge of availableEdges) {
          const delta = this.getEdgeDelta(edge, grid, cellStates, gridWidth, gridHeight);
          if (delta > maxDelta) {
            maxDelta = delta;
            bestEdges = [edge];
          } else if (delta === maxDelta) {
            bestEdges.push(edge);
          }
        }
        
        if (bestEdges.length > 0) {
          nextEdge = bestEdges[Math.floor(Math.random() * bestEdges.length)];
        } else {
          // Should not happen if availableEdges > 0
          nextEdge = availableEdges[Math.floor(Math.random() * availableEdges.length)];
        }
      } else {
        // Choose random edge
        nextEdge = availableEdges[Math.floor(Math.random() * availableEdges.length)];
      }
    } else if (edgeSelectionRule === 'randomWithBacktrack') {
      // Allow all edges including the one we came from
      if (connectedEdges.length === 0) {
        this.removeParticle(particle);
        return;
      }
      nextEdge = connectedEdges[Math.floor(Math.random() * connectedEdges.length)];
    } else if (edgeSelectionRule === 'clockwise' || edgeSelectionRule === 'counterClockwise') {
      // Find the edge that is clockwise or counter-clockwise from the current edge
      if (!particle.currentEdge) {
        // If no current edge, fall back to random
        if (connectedEdges.length === 0) {
          this.removeParticle(particle);
          return;
        }
        nextEdge = connectedEdges[Math.floor(Math.random() * connectedEdges.length)];
      } else {
        const foundEdge = this.findTurnEdge(
          particle.currentEdge,
          vertex,
          connectedEdges,
          edgeSelectionRule === 'clockwise'
        );
        
        if (!foundEdge) {
          // No valid turn found, remove particle
          this.removeParticle(particle);
          return;
        }
        nextEdge = foundEdge;
      }
    } else if (edgeSelectionRule === 'followCursor' || edgeSelectionRule === 'avoidCursor') {
      // Find the edge that minimizes or maximizes distance to cursor
      if (connectedEdges.length === 0) {
        this.removeParticle(particle);
        return;
      }
      
      // Filter out current edge for follow/avoid cursor rules
      const availableEdges = connectedEdges.filter(edge => {
        if (!particle.currentEdge) return true;
        return !edgesEqual(particle.currentEdge, edge);
      });
      
      if (availableEdges.length === 0) {
        this.removeParticle(particle);
        return;
      }
      
      const foundEdge = this.findCursorEdge(
        vertex,
        availableEdges,
        mouseX,
        mouseY,
        edgeSelectionRule === 'followCursor'
      );
      
      if (!foundEdge) {
        this.removeParticle(particle);
        return;
      }
      nextEdge = foundEdge;
    } else if (edgeSelectionRule === 'orbitCursor') {
      // Find the edge that best matches the orbit distance
      if (connectedEdges.length === 0) {
        this.removeParticle(particle);
        return;
      }
 
      const availableEdges = connectedEdges.filter(edge => {
        if (!particle.currentEdge) return true;
        return !edgesEqual(particle.currentEdge, edge);
      });
 
      if (availableEdges.length === 0) {
        this.removeParticle(particle);
        return;
      }
 
      let foundEdge: EdgeInfo | null = null;
      if (orbitAlgorithm === 'distanceToEndpoint') {
        foundEdge = this.findOrbitEndpointEdge(
          vertex,
          availableEdges,
          mouseX,
          mouseY,
          orbitDistance
        );
      } else {
        let pathLength = 1;
        if (orbitAlgorithm === 'twoStepGradient') {
          pathLength = 2;
        } else if (orbitAlgorithm === 'threeStepGradient') {
          pathLength = 3;
        } else if (orbitAlgorithm === 'fourStepGradient') {
          pathLength = 4;
        }
        foundEdge = this.findOrbitEdgeByPathLength(
          vertex,
          availableEdges,
          grid,
          gridWidth,
          gridHeight,
          mouseX,
          mouseY,
          orbitDistance,
          orbitEpsilon,
          pathLength
        );
      }
 
      if (!foundEdge) {
        this.removeParticle(particle);
        return;
      }
      nextEdge = foundEdge;
    } else {
      // Fallback to random
      if (connectedEdges.length === 0) {
        this.removeParticle(particle);
        return;
      }
      nextEdge = connectedEdges[Math.floor(Math.random() * connectedEdges.length)];
    }
    
    // Determine direction on new edge
    // We're at a vertex, so we need to move away from it along the new edge
    const nextP1 = nextEdge.points[0];
    const vertexIsP1 = pointsClose(vertex, nextP1, 0.01);
    
    particle.previousEdge = particle.currentEdge;
    particle.currentEdge = nextEdge;
    
    if (vertexIsP1) {
      // Vertex is at p1, so move toward p2 (positive direction)
      particle.direction = 1;
      particle.progress = 0; // Start at p1
    } else {
      // Vertex is at p2, so move toward p1 (negative direction)
      particle.direction = -1;
      particle.progress = 1; // Start at p2
    }
    
    particle.x = vertex.x;
    particle.y = vertex.y;
    
    // Update graphics
    const graphics = this.particleGraphics.get(particle);
    if (graphics) {
      graphics.x = particle.x;
      graphics.y = particle.y;
    }
  }
 
  private getEdgeDelta(edge: EdgeInfo, grid: Grid, cellStates: number[][], gridWidth: number, gridHeight: number): number {
    const p1 = edge.points[0];
    const p2 = edge.points[1];
    
    // Midpoint
    const midX = (p1.x + p2.x) / 2;
    const midY = (p1.y + p2.y) / 2;
    
    // Vector
    const dx = p2.x - p1.x;
    const dy = p2.y - p1.y;
    
    // Normal vector (normalized)
    const length = Math.sqrt(dx * dx + dy * dy);
    if (length === 0) return 0;
    
    const nx = -dy / length;
    const ny = dx / length;
    
    // Sample points on both sides
    const epsilon = 2; // Small offset
    const sample1 = { x: midX + nx * epsilon, y: midY + ny * epsilon };
    const sample2 = { x: midX - nx * epsilon, y: midY - ny * epsilon };
    
    const cell1 = grid.pixelToCell(sample1);
    const cell2 = grid.pixelToCell(sample2);
    
    let state1 = 0;
    let state2 = 0;
    
    if (cell1 && cell1.col >= 0 && cell1.col < gridWidth && cell1.row >= 0 && cell1.row < gridHeight) {
      state1 = cellStates[cell1.row][cell1.col];
    }
    
    if (cell2 && cell2.col >= 0 && cell2.col < gridWidth && cell2.row >= 0 && cell2.row < gridHeight) {
      state2 = cellStates[cell2.row][cell2.col];
    }
    
    return Math.abs(state1 - state2);
  }
 
 
  private findTurnEdge(
    currentEdge: EdgeInfo,
    vertex: Point,
    connectedEdges: EdgeInfo[],
    clockwise: boolean
  ): EdgeInfo | null {
    // Calculate the angle of the incoming edge (from vertex, pointing away from vertex)
    const otherPoint = getOtherPoint(currentEdge, vertex);
    
    // Calculate angle of incoming edge (from vertex to other point)
    const incomingAngle = Math.atan2(otherPoint.y - vertex.y, otherPoint.x - vertex.x);
    
    // Calculate angles of all outgoing edges
    const edgeAngles: Array<{ edge: EdgeInfo; angle: number; relativeAngle: number }> = [];
    
    for (const edge of connectedEdges) {
      // Skip the current edge
      const isCurrentEdge = edgesEqual(currentEdge, edge);
      
      if (isCurrentEdge) continue;
      
      // Find the other endpoint (not the vertex)
      const edgeOtherPoint = getOtherPoint(edge, vertex);
      
      // Calculate angle from vertex to other point
      const angle = Math.atan2(edgeOtherPoint.y - vertex.y, edgeOtherPoint.x - vertex.x);
      
      // Calculate relative angle (difference from incoming angle, normalized to -π to π)
      let relativeAngle = angle - incomingAngle;
      
      // Normalize to -π to π
      while (relativeAngle > Math.PI) relativeAngle -= 2 * Math.PI;
      while (relativeAngle < -Math.PI) relativeAngle += 2 * Math.PI;
      
      edgeAngles.push({ edge, angle, relativeAngle });
    }
    
    if (edgeAngles.length === 0) return null;
    
    // Sort by relative angle
    if (clockwise) {
      // Clockwise: find the edge with the largest negative relative angle
      // If none, wrap around to the most positive (which is actually the next clockwise)
      edgeAngles.sort((a, b) => {
        // Prefer negative angles (clockwise in screen coordinates where y increases downward)
        if (a.relativeAngle < 0 && b.relativeAngle >= 0) return 1;
        if (a.relativeAngle >= 0 && b.relativeAngle < 0) return -1;
        // Both same sign, sort by magnitude (reverse for clockwise)
        return b.relativeAngle - a.relativeAngle;
      });
      
      // Find first negative angle, or if none, use the most positive (wraps around)
      const negativeAngle = edgeAngles.find(e => e.relativeAngle < 0);
      return negativeAngle ? negativeAngle.edge : edgeAngles[edgeAngles.length - 1].edge;
    } else {
      // Counter-clockwise: find the edge with the smallest positive relative angle
      // If none, wrap around to the most negative (which is actually the next counter-clockwise)
      edgeAngles.sort((a, b) => {
        // Prefer positive angles (counter-clockwise in screen coordinates where y increases downward)
        if (a.relativeAngle > 0 && b.relativeAngle <= 0) return -1;
        if (a.relativeAngle <= 0 && b.relativeAngle > 0) return 1;
        // Both same sign, sort by magnitude
        return a.relativeAngle - b.relativeAngle;
      });
      
      // Find first positive angle, or if none, use the most negative (wraps around)
      const positiveAngle = edgeAngles.find(e => e.relativeAngle > 0);
      return positiveAngle ? positiveAngle.edge : edgeAngles[edgeAngles.length - 1].edge;
    }
  }
 
  private findCursorEdge(
    vertex: Point,
    connectedEdges: EdgeInfo[],
    mouseX: number,
    mouseY: number,
    follow: boolean
  ): EdgeInfo | null {
    if (connectedEdges.length === 0) return null;
    
    let bestEdge: EdgeInfo | null = null;
    let bestDist = follow ? Infinity : -Infinity;
    
    for (const edge of connectedEdges) {
      // Find the other endpoint of the edge (not the vertex)
      const otherPoint = getOtherPoint(edge, vertex);
      
      // Calculate distance from the other endpoint to cursor
      const distToCursor = Math.sqrt((mouseX - otherPoint.x) ** 2 + (mouseY - otherPoint.y) ** 2);
      
      // For follow: choose edge that minimizes distance to cursor
      // For avoid: choose edge that maximizes distance to cursor
      if (follow) {
        if (distToCursor < bestDist) {
          bestDist = distToCursor;
          bestEdge = edge;
        }
      } else {
        if (distToCursor > bestDist) {
          bestDist = distToCursor;
          bestEdge = edge;
        }
      }
    }
    
    return bestEdge;
  }
 
  private findOrbitEndpointEdge(
    vertex: Point,
    connectedEdges: EdgeInfo[],
    mouseX: number,
    mouseY: number,
    orbitDistance: number
  ): EdgeInfo | null {
    if (connectedEdges.length === 0) return null;
    
    let bestEdge: EdgeInfo | null = null;
    let bestDelta = Infinity;
    
    for (const edge of connectedEdges) {
      const otherPoint = getOtherPoint(edge, vertex);
      const distToCursor = Math.sqrt((mouseX - otherPoint.x) ** 2 + (mouseY - otherPoint.y) ** 2);
      const delta = Math.abs(distToCursor - orbitDistance);
      if (delta < bestDelta) {
        bestDelta = delta;
        bestEdge = edge;
      }
    }
    
    return bestEdge;
  }
 
  private findOrbitEdgeByPathLength(
    vertex: Point,
    connectedEdges: EdgeInfo[],
    grid: Grid,
    gridWidth: number,
    gridHeight: number,
    mouseX: number,
    mouseY: number,
    orbitDistance: number,
    orbitEpsilon: number,
    pathLength: number
  ): EdgeInfo | null {
    if (connectedEdges.length === 0 || pathLength < 1) return null;
 
    let bestEdge: EdgeInfo | null = null;
    let bestDelta = Infinity;
 
    type PathState = {
      currentVertex: Point;
      previousEdge: EdgeInfo | null;
      firstEdge: EdgeInfo;
      depth: number;
      lastFrom: Point;
      lastTo: Point;
    };
 
    const stack: PathState[] = [];
    for (const edge of connectedEdges) {
      const nextVertex = getOtherPoint(edge, vertex);
      stack.push({
        currentVertex: nextVertex,
        previousEdge: edge,
        firstEdge: edge,
        depth: 1,
        lastFrom: vertex,
        lastTo: nextVertex,
      });
    }
 
    while (stack.length > 0) {
      const state = stack.pop();
      if (!state) break;
 
      if (state.depth === pathLength) {
        const targetPoint = this.getOrbitGradientPoint(state.lastFrom, state.lastTo, orbitEpsilon);
        const distToCursor = Math.sqrt((mouseX - targetPoint.x) ** 2 + (mouseY - targetPoint.y) ** 2);
        const delta = Math.abs(distToCursor - orbitDistance);
        if (delta < bestDelta) {
          bestDelta = delta;
          bestEdge = state.firstEdge;
        }
        continue;
      }
 
      const nextEdges = grid.getEdgesAtVertex(state.currentVertex, gridWidth, gridHeight);
      for (const edge of nextEdges) {
        if (state.previousEdge && edgesEqual(state.previousEdge, edge)) {
          continue;
        }
        const nextVertex = getOtherPoint(edge, state.currentVertex);
        stack.push({
          currentVertex: nextVertex,
          previousEdge: edge,
          firstEdge: state.firstEdge,
          depth: state.depth + 1,
          lastFrom: state.currentVertex,
          lastTo: nextVertex,
        });
      }
    }
 
    return bestEdge;
  }
 
  private getOrbitGradientPoint(start: Point, end: Point, epsilon: number): Point {
    const dx = end.x - start.x;
    const dy = end.y - start.y;
    return {
      x: start.x + dx * epsilon,
      y: start.y + dy * epsilon,
    };
  }
 
  private removeParticle(particle: Particle): void {
    const graphics = this.particleGraphics.get(particle);
    if (graphics) {
      this.particleContainer.removeChild(graphics);
      this.particleGraphics.delete(particle);
    }
    const index = this.particles.indexOf(particle);
    if (index > -1) {
      this.particles.splice(index, 1);
    }
  }
 
  clear(): void {
    for (const particle of this.particles) {
      this.removeParticle(particle);
    }
  }
 
  removeParticlesOnEdge(edge: EdgeInfo): void {
    this.removeParticlesOnEdges([edge]);
  }
 
  removeParticlesOnEdges(edges: EdgeInfo[]): void {
    for (let i = this.particles.length - 1; i >= 0; i--) {
      const particle = this.particles[i];
      const matches = edges.some((edge) => edgesEqual(particle.currentEdge, edge));
      if (matches) {
        this.removeParticle(particle);
      }
    }
  }
}