feat: 开始开发专注力系列

This commit is contained in:
R524809
2025-12-11 17:47:22 +08:00
parent 3c0e2af10d
commit 3648ccc494
61 changed files with 1348 additions and 34 deletions
@@ -0,0 +1,108 @@
/**
* 格子仿画工具函数
* 提供对称图案生成等功能
*/
import { GridCell, GridConfig, ShapeTemplate } from '../types/gridTypes';
/**
* 移除重复的格子(保留最后一个)
*/
function removeDuplicateCells(cells: GridCell[]): GridCell[] {
const cellMap = new Map<string, GridCell>();
// 从后往前遍历,保留最后出现的格子
for (let i = cells.length - 1; i >= 0; i--) {
const cell = cells[i];
const key = `${cell.x},${cell.y}`;
if (!cellMap.has(key)) {
cellMap.set(key, cell);
}
}
return Array.from(cellMap.values());
}
/**
* 根据对称轴类型,将半部分图案补全为完整图案
* @param halfCells 半部分格子的数据(通常是左半部分或上半部分)
* @param config 网格配置
* @param symmetryType 对称类型
* @returns 完整的格子数据
*/
export function generateSymmetricalPattern(
halfCells: GridCell[],
config: GridConfig,
symmetryType: 'vertical' | 'horizontal' | 'both',
): GridCell[] {
const result: GridCell[] = [...halfCells];
const { rows, cols } = config;
if (symmetryType === 'vertical' || symmetryType === 'both') {
// 垂直对称(关于中间列对称)
const verticalAxis = Math.floor(cols / 2);
const cellsToAdd: GridCell[] = [];
for (const cell of halfCells) {
// 如果格子不在对称轴上,生成对称的格子
if (cell.x < verticalAxis) {
const symmetricX = cols - 1 - cell.x;
cellsToAdd.push({
x: symmetricX,
y: cell.y,
color: cell.color,
});
}
}
result.push(...cellsToAdd);
}
if (symmetryType === 'horizontal' || symmetryType === 'both') {
// 水平对称(关于中间行对称)
const horizontalAxis = Math.floor(rows / 2);
const cellsToAdd: GridCell[] = [];
for (const cell of result) {
// 如果格子不在对称轴上,生成对称的格子
if (cell.y < horizontalAxis) {
const symmetricY = rows - 1 - cell.y;
cellsToAdd.push({
x: cell.x,
y: symmetricY,
color: cell.color,
});
}
}
result.push(...cellsToAdd);
}
// 去重(避免对称轴上的格子重复)
return removeDuplicateCells(result);
}
/**
* 从形状模板生成完整的格子数据
* @param template 形状模板
* @returns 完整的格子数据
*/
export function generateCompletePattern(template: ShapeTemplate): GridCell[] {
if (template.isComplete) {
// 如果已经是完整图案,直接返回
return [...template.cells];
}
if (template.supportSymmetry && template.symmetryType) {
// 使用对称生成完整图案
return generateSymmetricalPattern(
template.cells,
{
rows: template.recommendedSize.rows,
cols: template.recommendedSize.cols,
},
template.symmetryType,
);
}
// 如果不支持对称,返回原数据
return [...template.cells];
}