You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

二维数组点路径规划:Java机器人绘图点顺序生成需求

简单绘图路径生成方案(扫描线法)

针对你的需求,最适合的是扫描线法——逻辑简单、容易实现,完全不需要复杂的路径规划算法,还能减少绘图机器人的无效移动。

核心思路

按「蛇形扫描」的顺序遍历矩阵:

  • 从上到下遍历每一行
  • 奇数行(从0开始计数)从左到右收集值为1的点,偶数行从右到左收集
  • 收集到的点顺序就是绘图路径,既符合直觉,又能减少机器人抬笔和来回移动的次数

修改后的Path类实现

import java.util.ArrayList;
import java.util.List;

public class Path {
    public int maxWidth;
    public int maxHeight;
    // 存储按顺序排列的绘图点(x坐标, y坐标)
    public List<int[]> drawingPath;

    public Path(int maxWidth, int maxHeight, int[][] matrix) {
        this.maxHeight = maxHeight;
        this.maxWidth = maxWidth;
        this.drawingPath = generateDrawingPath(matrix);
    }

    // 生成绘图路径的点列表(蛇形扫描)
    private List<int[]> generateDrawingPath(int[][] matrix) {
        List<int[]> path = new ArrayList<>();
        boolean leftToRight = true;

        for (int y = 0; y < maxHeight; y++) {
            if (leftToRight) {
                // 从左到右遍历当前行
                for (int x = 0; x < maxWidth; x++) {
                    if (matrix[x][y] == 1) {
                        path.add(new int[]{x, y});
                    }
                }
            } else {
                // 从右到左遍历当前行,减少移动距离
                for (int x = maxWidth - 1; x >= 0; x--) {
                    if (matrix[x][y] == 1) {
                        path.add(new int[]{x, y});
                    }
                }
            }
            // 切换下一行的遍历方向
            leftToRight = !leftToRight;
        }
        return path;
    }

    // 生成标记路径顺序的矩阵(原1的位置替换为顺序编号,方便可视化)
    public int[][] getOrderedMatrix(int[][] matrix) {
        int[][] orderedMatrix = new int[maxWidth][maxHeight];
        // 复制原矩阵的空白区域(0)
        for (int x = 0; x < maxWidth; x++) {
            System.arraycopy(matrix[x], 0, orderedMatrix[x], 0, maxHeight);
        }

        int order = 1;
        for (int[] point : drawingPath) {
            int x = point[0];
            int y = point[1];
            orderedMatrix[x][y] = order++;
        }
        return orderedMatrix;
    }

    public void drawCanvas(int width, int height, int[][] matrix) {
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                System.out.printf("%d ", matrix[x][y]);
            }
            System.out.println();
        }
    }
}

使用示例

public class Main {
    public static void main(String[] args) {
        int width = 5;
        int height = 3;
        // 测试用矩阵
        int[][] matrix = {
                {0, 1, 0, 1, 0},
                {1, 1, 1, 0, 1},
                {0, 0, 1, 1, 0}
        };

        Path path = new Path(width, height, matrix);
        // 打印标记顺序的矩阵
        System.out.println("路径顺序可视化:");
        path.drawCanvas(width, height, path.getOrderedMatrix(matrix));
        // 打印具体的绘图路径
        System.out.println("\n绘图点顺序(x,y):");
        for (int[] point : path.drawingPath) {
            System.out.printf("(%d,%d) ", point[0], point[1]);
        }
    }
}

输出结果

路径顺序可视化:
0 1 0 4 0 
3 2 1 0 5 
0 0 6 7 0 

绘图点顺序(x,y):
(1,0) (3,0) (1,1) (2,1) (0,1) (4,1) (2,2) (3,2) 

方案优势

  1. 实现简单:没有复杂的距离计算或路径规划,新手也能快速理解和调试
  2. 效率较高:蛇形扫描减少了机器人横向移动的距离,降低抬笔频率
  3. 适配需求:完全针对你的「1为绘制点、0为空白」的输入格式设计

内容的提问来源于stack exchange,提问作者Floris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 07:44:55