createZigZag static method

List<FreeHexagon> createZigZag({
  1. required FreeHexagon start,
  2. required int count,
  3. double? size,
  4. double gap = 0.0,
  5. int zigWidth = 3,
  6. int zigHeight = 2,
})

创建Z字形排列

start 起始位置 count 六边形总数 size 六边形大小 gap 间隙 zigWidth Z字的宽度(每行多少个) zigHeight Z字的高度(向下多少个后转向)

Implementation

static List<FreeHexagon> createZigZag({
  required FreeHexagon start,
  required int count,
  double? size,
  double gap = 0.0,
  int zigWidth = 3,
  int zigHeight = 2,
}) {
  if (count <= 0) return [];

  final hexSize = size ?? start.size;
  final result = <FreeHexagon>[];

  var currentHex = start;
  var horizontalDirection = 0; // 向右
  var verticalDirection = 2; // 向下右
  var isGoingRight = true;
  var horizontalCount = 0;
  var verticalCount = 0;
  var phase = 0; // 0=横向, 1=纵向

  for (var i = 0; i < count; i++) {
    int direction;

    if (phase == 0) {
      // 横向移动
      direction = isGoingRight
          ? horizontalDirection
          : (horizontalDirection + 3) % 6;
      horizontalCount++;

      if (horizontalCount >= zigWidth) {
        phase = 1;
        horizontalCount = 0;
        verticalCount = 0;
      }
    } else {
      // 纵向移动
      direction = verticalDirection;
      verticalCount++;

      if (verticalCount >= zigHeight) {
        phase = 0;
        isGoingRight = !isGoingRight;
      }
    }

    final nextCenter = HexagonLayoutHelper.calculateAdjacentCenter(
      sourceHex: currentHex,
      sourceEdge: direction,
      targetSize: hexSize,
      gap: gap,
    );

    final nextHex = FreeHexagon(
      id: '${start.id}_zigzag_$i',
      center: nextCenter,
      size: hexSize,
      orientation: start.orientation,
    );

    result.add(nextHex);
    currentHex = nextHex;
  }

  return result;
}