// 纵轴方向 for (double y = gap / 2; y <= size.height; y += gap) { line = []; // 横轴方向 for (double x = gap / 4; x <= size.width; x += gap) { line.add({"x": x, "y": y}); // 绘制圆,参数分别是圆点位置,圆的半径,绘制属性 canvas.drawCircle(Offset(x, y), 1, paint); } lines.add(line); } } }
for (double y = gap / 2; y <= size.height; y += gap) { // 每次添加圆点之前对 odd 取反 odd = !odd; line = [];
for (double x = gap / 4; x <= size.width; x += gap) { Point point = Point(); // 赋值圆点横轴位置时根据 odd 变量判断是否需要增加距离 point.x = x + (odd ? gap / 2 : 0); point.y = y; // 将点添加到 line 数组 line.add(point); canvas.drawCircle(Offset(point.x, point.y), 1, paint); }
for (int y = 0; y < lines.length - 1; y++) { odd = !odd; dotLine = []; for (var i = 0; i < lines[y].length; i++) { dotLine.add(odd ? lines[y][i] : lines[y + 1][i]); dotLine.add(odd ? lines[y + 1][i] : lines[y][i]); }
for (int i = 0; i < dotLine.length - 2; i++) { _drawTriangle(canvas, dotLine[i], dotLine[i + 1], dotLine[i + 2]); } } }
创建圆点时加入随机量,形成不规则的三角形。
1 2 3 4 5 6 7
for (double x = gap / 4; x <= size.width; x += gap) { Point point = Point(); double random = (Random().nextDouble() * .8 - .4) * gap; point.x = x + random + (odd ? gap / 2 : 0); point.y = y + (Random().nextDouble() * .8 - .4) * gap; line.add(point); }