fork download
  1. // Клас для моделювання системи частинок
  2. class ParticleSystem {
  3. constructor(numParticles, canvasWidth, canvasHeight) {
  4. this.particles = [];
  5. this.width = canvasWidth;
  6. this.height = canvasHeight;
  7.  
  8. // Ініціалізація частинок
  9. for (let i = 0; i < numParticles; i++) {
  10. this.particles.push({
  11. x: Math.random() * canvasWidth,
  12. y: Math.random() * canvasHeight,
  13. vx: (Math.random() - 0.5) * 2,
  14. vy: (Math.random() - 0.5) * 2,
  15. mass: Math.random() * 5 + 1
  16. });
  17. }
  18. }
  19.  
  20. // Моделювання гравітаційної взаємодії
  21. update(dt = 0.1) {
  22. const G = 0.1; // Гравітаційна константа
  23.  
  24. // Обчислення сил між частинками
  25. for (let i = 0; i < this.particles.length; i++) {
  26. let fx = 0, fy = 0;
  27.  
  28. for (let j = 0; j < this.particles.length; j++) {
  29. if (i !== j) {
  30. const dx = this.particles[j].x - this.particles[i].x;
  31. const dy = this.particles[j].y - this.particles[i].y;
  32. const distance = Math.sqrt(dx*dx + dy*dy) + 1; // +1 для уникнення ділення на нуль
  33.  
  34. const force = G * this.particles[i].mass * this.particles[j].mass / (distance * distance);
  35. fx += force * dx / distance;
  36. fy += force * dy / distance;
  37. }
  38. }
  39.  
  40. // Оновлення швидкості та позиції
  41. this.particles[i].vx += fx / this.particles[i].mass * dt;
  42. this.particles[i].vy += fy / this.particles[i].mass * dt;
  43. this.particles[i].x += this.particles[i].vx * dt;
  44. this.particles[i].y += this.particles[i].vy * dt;
  45. }
  46. }
  47. }
  48.  
  49. // Моделювання клітинного автомата (Гра життя)
  50. class GameOfLife {
  51. constructor(width, height) {
  52. this.width = width;
  53. this.height = height;
  54. this.grid = Array(height).fill().map(() => Array(width).fill(0));
  55. this.randomize();
  56. }
  57.  
  58. randomize() {
  59. for (let y = 0; y < this.height; y++) {
  60. for (let x = 0; x < this.width; x++) {
  61. this.grid[y][x] = Math.random() > 0.7 ? 1 : 0;
  62. }
  63. }
  64. }
  65.  
  66. step() {
  67. const newGrid = this.grid.map(arr => [...arr]);
  68.  
  69. for (let y = 0; y < this.height; y++) {
  70. for (let x = 0; x < this.width; x++) {
  71. const neighbors = this.countNeighbors(x, y);
  72.  
  73. if (this.grid[y][x] === 1) {
  74. newGrid[y][x] = (neighbors === 2 || neighbors === 3) ? 1 : 0;
  75. } else {
  76. newGrid[y][x] = (neighbors === 3) ? 1 : 0;
  77. }
  78. }
  79. }
  80.  
  81. this.grid = newGrid;
  82. }
  83.  
  84. countNeighbors(x, y) {
  85. let count = 0;
  86. for (let dy = -1; dy <= 1; dy++) {
  87. for (let dx = -1; dx <= 1; dx++) {
  88. if (dx === 0 && dy === 0) continue;
  89. const nx = (x + dx + this.width) % this.width;
  90. const ny = (y + dy + this.height) % this.height;
  91. count += this.grid[ny][nx];
  92. }
  93. }
  94. return count;
  95. }
  96. }
Success #stdin #stdout 0.03s 16852KB
stdin
45
stdout
Standard output is empty