/
raperd
/
Chelovechki
Обзор
Документация
Войти
/
raperd
/
Chelovechki
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/border.js
103 строки
3 KB
raperd
Create: 1
04 июл 2026, 23:12
Верифицирован
04 июл 2026, 23:12
064c1f8
Код
Авторство
О чём код?
// border.js � ��������� � ���������� ��������� import { GAME_CONFIG, BORDER_RADIUS } from './constants.js'; import { convexHull, polygonsIntersect, polygonsTouch, isPointInPolygon } from './geometry.js'; function smoothPolygon(points) { if (points.length < 3) return points; const smoothed = []; for (let i = 0; i < points.length; i++) { const prev = points[(i - 1 + points.length) % points.length]; const curr = points[i]; const next = points[(i + 1) % points.length]; smoothed.push({ x: (prev.x + curr.x * 2 + next.x) / 4, y: (prev.y + curr.y * 2 + next.y) / 4, }); } return smoothed; } export function generateBorderAround(x, y) { const minPoints = GAME_CONFIG.BORDER.GENERATION_POINTS_MIN; const maxPoints = GAME_CONFIG.BORDER.GENERATION_POINTS_MAX; const numPoints = Math.floor(Math.random() * (maxPoints - minPoints + 1)) + minPoints; const minFactor = GAME_CONFIG.BORDER.RADIUS_VARIATION_MIN; const maxFactor = GAME_CONFIG.BORDER.RADIUS_VARIATION_MAX; const points = []; for (let i = 0; i < numPoints; i++) { const angle = (i / numPoints) * Math.PI * 2; const rFactor = minFactor + Math.random() * (maxFactor - minFactor); const r = BORDER_RADIUS * rFactor; points.push({ x: x + Math.cos(angle) * r, y: y + Math.sin(angle) * r }); } let result = points; for (let iter = 0; iter < GAME_CONFIG.BORDER.SMOOTH_ITERATIONS; iter++) { result = smoothPolygon(result); } return result; } export function buildBorderGroups(humans) { if (!humans.length) return []; // ����������, ��� � ���� ���� ������� for (const h of humans) { if (!h.borderPoints || !h.borderPoints.length) { h.borderPoints = generateBorderAround(h.x, h.y); } } // ��������� ������ (������ ������� � ����� ������) let groups = humans.map((_, i) => [i]); let changed = true; while (changed) { changed = false; for (let i = 0; i < groups.length; i++) { for (let j = i + 1; j < groups.length; j++) { let intersect = false; for (const idxI of groups[i]) { for (const idxJ of groups[j]) { const poly1 = humans[idxI].borderPoints; const poly2 = humans[idxJ].borderPoints; if (polygonsIntersect(poly1, poly2) || polygonsTouch(poly1, poly2, GAME_CONFIG.BORDER.TOUCH_THRESHOLD)) { intersect = true; break; } } if (intersect) break; } if (intersect) { groups[i] = [...groups[i], ...groups[j]]; groups.splice(j, 1); changed = true; break; } } if (changed) break; } } // ������ ������� ��� ������ ������ return groups.map(indices => { const allPoints = indices.flatMap(idx => humans[idx].borderPoints); return { indices, border: convexHull(allPoints) }; }); } export function findGroupContainingPoint(borderGroups, x, y) { return borderGroups.find(group => group.border && isPointInPolygon(x, y, group.border)); } export function findIntersectingGroup(borderGroups, newBorder) { return borderGroups.find(group => group.border && ( polygonsIntersect(group.border, newBorder) || polygonsTouch(group.border, newBorder, GAME_CONFIG.BORDER.TOUCH_THRESHOLD) ) ); }