当前位置: 首页 > article >正文

广度优先寻路算法(一)

文章目录

  • 原始数据
    • 数据属性解释:
    • 数据
    • 效果图(没有标记点名称)
  • 寻路效果
    • 程序运行效果![在这里插入图片描述](https://img-blog.csdnimg.cn/3d89199c47734eb795744af822652b69.png)
    • 源代码
  • 思路
    • private List<RoadPoint> CalcPath(RoadPoint end, List<RoadPoint> points, List<List<RoadPoint>> paths)

ps:写在前面,当时只是在一个纵横交错的库房内寻找到从A到B的路径,并没有最短最优的要求。

原始数据

这是一段json转存在C#代码中的结构,

数据属性解释:

  • Id:标识
  • PointName:点位名称
  • PointX:x坐标
  • PointY:y坐标
  • NeighborIdArr:数组,能够到达的邻居节点ID
  • IsWorlplacePoint:是否是工位节点(true表示工位节点,不能穿过,只能作为终点到达)

数据

    #region JsonString
            json = @"[{
	""Id"": 1,
	""PointName"": ""A0001"",
	""PointX"": 0,
	""PointY"": 0,
	""NeighborIdArr"": [2, 6],
	""IsWorkplacePoint"": false
}, {
	""Id"": 2,
	""PointName"": ""A0002"",
	""PointX"": 0,
	""PointY"": 200,
	""NeighborIdArr"": [1, 3, 7],
	""IsWorkplacePoint"": false
}, {
	""Id"": 3,
	""PointName"": ""A0003"",
	""PointX"": 0,
	""PointY"": 700,
	""NeighborIdArr"": [2, 4, 8],
	""IsWorkplacePoint"": false
}, {
	""Id"": 4,
	""PointName"": ""A0004"",
	""PointX"": 0,
	""PointY"": 900,
	""NeighborIdArr"": [3, 5, 9],
	""IsWorkplacePoint"": false
}, {
	""Id"": 5,
	""PointName"": ""A0005"",
	""PointX"": 0,
	""PointY"": 1400,
	""NeighborIdArr"": [4, 10],
	""IsWorkplacePoint"": false
}, {
	""Id"": 6,
	""PointName"": ""A0006"",
	""PointX"": 200,
	""PointY"": 0,
	""NeighborIdArr"": [1, 7, 11],
	""IsWorkplacePoint"": false
}, {
	""Id"": 7,
	""PointName"": ""A0007"",
	""PointX"": 200,
	""PointY"": 200,
	""NeighborIdArr"": [2, 6, 12, 8],
	""IsWorkplacePoint"": false
}, {
	""Id"": 8,
	""PointName"": ""A0008"",
	""PointX"": 200,
	""PointY"": 700,
	""NeighborIdArr"": [3, 7, 13, 9],
	""IsWorkplacePoint"": false
}, {
	""Id"": 9,
	""PointName"": ""A0009"",
	""PointX"": 200,
	""PointY"": 900,
	""NeighborIdArr"": [4, 8, 14, 10],
	""IsWorkplacePoint"": false
}, {
	""Id"": 10,
	""PointName"": ""A0010"",
	""PointX"": 200,
	""PointY"": 1400,
	""NeighborIdArr"": [5, 9, 15],
	""IsWorkplacePoint"": false
}, {
	""Id"": 11,
	""PointName"": ""A0011"",
	""PointX"": 600,
	""PointY"": 0,
	""NeighborIdArr"": [6, 12, 16],
	""IsWorkplacePoint"": false
}, {
	""Id"": 12,
	""PointName"": ""A0012"",
	""PointX"": 600,
	""PointY"": 200,
	""NeighborIdArr"": [7, 13, 17, 11],
	""IsWorkplacePoint"": false
}, {
	""Id"": 13,
	""PointName"": ""A0013"",
	""PointX"": 600,
	""PointY"": 700,
	""NeighborIdArr"": [8, 14, 18, 12],
	""IsWorkplacePoint"": false
}, {
	""Id"": 14,
	""PointName"": ""A0014"",
	""PointX"": 600,
	""PointY"": 900,
	""NeighborIdArr"": [9, 15, 19, 13],
	""IsWorkplacePoint"": false
}, {
	""Id"": 15,
	""PointName"": ""A0015"",
	""PointX"": 600,
	""PointY"": 1400,
	""NeighborIdArr"": [10, 20, 14],
	""IsWorkplacePoint"": false
}, {
	""Id"": 16,
	""PointName"": ""A0016"",
	""PointX"": 800,
	""PointY"": 0,
	""NeighborIdArr"": [11, 21, 17],
	""IsWorkplacePoint"": false
}, {
	""Id"": 17,
	""PointName"": ""A0017"",
	""PointX"": 800,
	""PointY"": 200,
	""NeighborIdArr"": [12, 16, 22, 18],
	""IsWorkplacePoint"": false
}, {
	""Id"": 18,
	""PointName"": ""A0018"",
	""PointX"": 800,
	""PointY"": 700,
	""NeighborIdArr"": [13, 17, 23, 19],
	""IsWorkplacePoint"": false
}, {
	""Id"": 19,
	""PointName"": ""A0019"",
	""PointX"": 800,
	""PointY"": 900,
	""NeighborIdArr"": [14, 18, 24, 20],
	""IsWorkplacePoint"": false
}, {
	""Id"": 20,
	""PointName"": ""A0020"",
	""PointX"": 800,
	""PointY"": 1400,
	""NeighborIdArr"": [15, 19, 25],
	""IsWorkplacePoint"": false
}, {
	""Id"": 21,
	""PointName"": ""A0021"",
	""PointX"": 1200,
	""PointY"": 0,
	""NeighborIdArr"": [16, 26, 22],
	""IsWorkplacePoint"": false
}, {
	""Id"": 22,
	""PointName"": ""A0022"",
	""PointX"": 1200,
	""PointY"": 200,
	""NeighborIdArr"": [17, 21, 27, 23],
	""IsWorkplacePoint"": false
}, {
	""Id"": 23,
	""PointName"": ""A0023"",
	""PointX"": 1200,
	""PointY"": 700,
	""NeighborIdArr"": [18, 22, 28, 24],
	""IsWorkplacePoint"": false
}, {
	""Id"": 24,
	""PointName"": ""A0024"",
	""PointX"": 1200,
	""PointY"": 900,
	""NeighborIdArr"": [19, 23, 29, 25],
	""IsWorkplacePoint"": false
}, {
	""Id"": 25,
	""PointName"": ""A0025"",
	""PointX"": 1200,
	""PointY"": 1400,
	""NeighborIdArr"": [20, 24, 30],
	""IsWorkplacePoint"": false
}, {
	""Id"": 26,
	""PointName"": ""A0026"",
	""PointX"": 1700,
	""PointY"": 0,
	""NeighborIdArr"": [21, 27],
	""IsWorkplacePoint"": false
}, {
	""Id"": 27,
	""PointName"": ""A0027"",
	""PointX"": 1700,
	""PointY"": 200,
	""NeighborIdArr"": [22, 26, 28],
	""IsWorkplacePoint"": false
}, {
	""Id"": 28,
	""PointName"": ""A0028"",
	""PointX"": 1700,
	""PointY"": 700,
	""NeighborIdArr"": [23, 27, 29],
	""IsWorkplacePoint"": false
}, {
	""Id"": 29,
	""PointName"": ""A0029"",
	""PointX"": 1700,
	""PointY"": 900,
	""NeighborIdArr"": [24, 28, 30],
	""IsWorkplacePoint"": false
}, {
	""Id"": 30,
	""PointName"": ""A0030"",
	""PointX"": 1700,
	""PointY"": 1400,
	""NeighborIdArr"": [25, 29],
	""IsWorkplacePoint"": false
}, {
	""Id"": 31,
	""PointName"": ""P0001"",
	""PointX"": 100,
	""PointY"": 100,
	""NeighborIdArr"": [1, 2, 6, 7],
	""IsWorkplacePoint"": true
}, {
	""Id"": 32,
	""PointName"": ""P0002"",
	""PointX"": 100,
	""PointY"": 450,
	""NeighborIdArr"": [2, 3, 7, 8],
	""IsWorkplacePoint"": true
}, {
	""Id"": 33,
	""PointName"": ""P0003"",
	""PointX"": 100,
	""PointY"": 800,
	""NeighborIdArr"": [3, 4, 8, 9],
	""IsWorkplacePoint"": true
}, {
	""Id"": 34,
	""PointName"": ""P0004"",
	""PointX"": 100,
	""PointY"": 1150,
	""NeighborIdArr"": [4, 5, 9, 10],
	""IsWorkplacePoint"": true
}, {
	""Id"": 35,
	""PointName"": ""P0005"",
	""PointX"": 400,
	""PointY"": 100,
	""NeighborIdArr"": [6, 7, 11, 12],
	""IsWorkplacePoint"": true
}, {
	""Id"": 36,
	""PointName"": ""P0006"",
	""PointX"": 400,
	""PointY"": 450,
	""NeighborIdArr"": [7, 8, 12, 13],
	""IsWorkplacePoint"": true
}, {
	""Id"": 37,
	""PointName"": ""P0007"",
	""PointX"": 400,
	""PointY"": 800,
	""NeighborIdArr"": [8, 9, 13, 14],
	""IsWorkplacePoint"": true
}, {
	""Id"": 38,
	""PointName"": ""P0008"",
	""PointX"": 400,
	""PointY"": 1150,
	""NeighborIdArr"": [9, 10, 14, 15],
	""IsWorkplacePoint"": true
}, {
	""Id"": 39,
	""PointName"": ""P0009"",
	""PointX"": 700,
	""PointY"": 100,
	""NeighborIdArr"": [11, 12, 16, 17],
	""IsWorkplacePoint"": true
}, {
	""Id"": 40,
	""PointName"": ""P0010"",
	""PointX"": 700,
	""PointY"": 450,
	""NeighborIdArr"": [12, 13, 17, 18],
	""IsWorkplacePoint"": true
}, {
	""Id"": 41,
	""PointName"": ""P0011"",
	""PointX"": 700,
	""PointY"": 800,
	""NeighborIdArr"": [13, 14, 18, 19],
	""IsWorkplacePoint"": true
}, {
	""Id"": 42,
	""PointName"": ""P0012"",
	""PointX"": 700,
	""PointY"": 1150,
	""NeighborIdArr"": [14, 15, 19, 20],
	""IsWorkplacePoint"": true
}, {
	""Id"": 43,
	""PointName"": ""P0013"",
	""PointX"": 1000,
	""PointY"": 100,
	""NeighborIdArr"": [16, 17, 21, 22],
	""IsWorkplacePoint"": true
}, {
	""Id"": 44,
	""PointName"": ""P0014"",
	""PointX"": 1000,
	""PointY"": 450,
	""NeighborIdArr"": [17, 18, 22, 23],
	""IsWorkplacePoint"": true
}, {
	""Id"": 45,
	""PointName"": ""P0015"",
	""PointX"": 1000,
	""PointY"": 800,
	""NeighborIdArr"": [18, 19, 23, 24],
	""IsWorkplacePoint"": true
}, {
	""Id"": 46,
	""PointName"": ""P0016"",
	""PointX"": 1000,
	""PointY"": 1150,
	""NeighborIdArr"": [19, 20, 24, 25],
	""IsWorkplacePoint"": true
}, {
	""Id"": 47,
	""PointName"": ""P0017"",
	""PointX"": 1450,
	""PointY"": 100,
	""NeighborIdArr"": [21, 22, 26, 27],
	""IsWorkplacePoint"": true
}, {
	""Id"": 48,
	""PointName"": ""P0018"",
	""PointX"": 1450,
	""PointY"": 450,
	""NeighborIdArr"": [22, 23, 27, 28],
	""IsWorkplacePoint"": true
}, {
	""Id"": 49,
	""PointName"": ""P0019"",
	""PointX"": 1450,
	""PointY"": 800,
	""NeighborIdArr"": [23, 24, 28, 29],
	""IsWorkplacePoint"": true
}, {
	""Id"": 50,
	""PointName"": ""P0020"",
	""PointX"": 1450,
	""PointY"": 1150,
	""NeighborIdArr"": [24, 25, 29, 30],
	""IsWorkplacePoint"": true
}]";
            #endregion JsonString 

效果图(没有标记点名称)

在这里插入图片描述

寻路效果

程序运行效果在这里插入图片描述

源代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 测试RFID读写工具
{
    public partial class Form2 : Form
    {
        string json = "";
        List<RoadPoint> Points;
        int canvasWidth;
        int canvasHeight;
        float maxX;
        float maxY;

        float rx;
        float ry;

		[ThreadStatic]
		public static string A;
        public Form2()
        {
            InitializeComponent();
            this.Load += Form2_Load;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            #region JsonString
            json = @"[{
	""Id"": 1,
	""PointName"": ""A0001"",
	""PointX"": 0,
	""PointY"": 0,
	""NeighborIdArr"": [2, 6],
	""IsWorkplacePoint"": false
}, {
	""Id"": 2,
	""PointName"": ""A0002"",
	""PointX"": 0,
	""PointY"": 200,
	""NeighborIdArr"": [1, 3, 7],
	""IsWorkplacePoint"": false
}, {
	""Id"": 3,
	""PointName"": ""A0003"",
	""PointX"": 0,
	""PointY"": 700,
	""NeighborIdArr"": [2, 4, 8],
	""IsWorkplacePoint"": false
}, {
	""Id"": 4,
	""PointName"": ""A0004"",
	""PointX"": 0,
	""PointY"": 900,
	""NeighborIdArr"": [3, 5, 9],
	""IsWorkplacePoint"": false
}, {
	""Id"": 5,
	""PointName"": ""A0005"",
	""PointX"": 0,
	""PointY"": 1400,
	""NeighborIdArr"": [4, 10],
	""IsWorkplacePoint"": false
}, {
	""Id"": 6,
	""PointName"": ""A0006"",
	""PointX"": 200,
	""PointY"": 0,
	""NeighborIdArr"": [1, 7, 11],
	""IsWorkplacePoint"": false
}, {
	""Id"": 7,
	""PointName"": ""A0007"",
	""PointX"": 200,
	""PointY"": 200,
	""NeighborIdArr"": [2, 6, 12, 8],
	""IsWorkplacePoint"": false
}, {
	""Id"": 8,
	""PointName"": ""A0008"",
	""PointX"": 200,
	""PointY"": 700,
	""NeighborIdArr"": [3, 7, 13, 9],
	""IsWorkplacePoint"": false
}, {
	""Id"": 9,
	""PointName"": ""A0009"",
	""PointX"": 200,
	""PointY"": 900,
	""NeighborIdArr"": [4, 8, 14, 10],
	""IsWorkplacePoint"": false
}, {
	""Id"": 10,
	""PointName"": ""A0010"",
	""PointX"": 200,
	""PointY"": 1400,
	""NeighborIdArr"": [5, 9, 15],
	""IsWorkplacePoint"": false
}, {
	""Id"": 11,
	""PointName"": ""A0011"",
	""PointX"": 600,
	""PointY"": 0,
	""NeighborIdArr"": [6, 12, 16],
	""IsWorkplacePoint"": false
}, {
	""Id"": 12,
	""PointName"": ""A0012"",
	""PointX"": 600,
	""PointY"": 200,
	""NeighborIdArr"": [7, 13, 17, 11],
	""IsWorkplacePoint"": false
}, {
	""Id"": 13,
	""PointName"": ""A0013"",
	""PointX"": 600,
	""PointY"": 700,
	""NeighborIdArr"": [8, 14, 18, 12],
	""IsWorkplacePoint"": false
}, {
	""Id"": 14,
	""PointName"": ""A0014"",
	""PointX"": 600,
	""PointY"": 900,
	""NeighborIdArr"": [9, 15, 19, 13],
	""IsWorkplacePoint"": false
}, {
	""Id"": 15,
	""PointName"": ""A0015"",
	""PointX"": 600,
	""PointY"": 1400,
	""NeighborIdArr"": [10, 20, 14],
	""IsWorkplacePoint"": false
}, {
	""Id"": 16,
	""PointName"": ""A0016"",
	""PointX"": 800,
	""PointY"": 0,
	""NeighborIdArr"": [11, 21, 17],
	""IsWorkplacePoint"": false
}, {
	""Id"": 17,
	""PointName"": ""A0017"",
	""PointX"": 800,
	""PointY"": 200,
	""NeighborIdArr"": [12, 16, 22, 18],
	""IsWorkplacePoint"": false
}, {
	""Id"": 18,
	""PointName"": ""A0018"",
	""PointX"": 800,
	""PointY"": 700,
	""NeighborIdArr"": [13, 17, 23, 19],
	""IsWorkplacePoint"": false
}, {
	""Id"": 19,
	""PointName"": ""A0019"",
	""PointX"": 800,
	""PointY"": 900,
	""NeighborIdArr"": [14, 18, 24, 20],
	""IsWorkplacePoint"": false
}, {
	""Id"": 20,
	""PointName"": ""A0020"",
	""PointX"": 800,
	""PointY"": 1400,
	""NeighborIdArr"": [15, 19, 25],
	""IsWorkplacePoint"": false
}, {
	""Id"": 21,
	""PointName"": ""A0021"",
	""PointX"": 1200,
	""PointY"": 0,
	""NeighborIdArr"": [16, 26, 22],
	""IsWorkplacePoint"": false
}, {
	""Id"": 22,
	""PointName"": ""A0022"",
	""PointX"": 1200,
	""PointY"": 200,
	""NeighborIdArr"": [17, 21, 27, 23],
	""IsWorkplacePoint"": false
}, {
	""Id"": 23,
	""PointName"": ""A0023"",
	""PointX"": 1200,
	""PointY"": 700,
	""NeighborIdArr"": [18, 22, 28, 24],
	""IsWorkplacePoint"": false
}, {
	""Id"": 24,
	""PointName"": ""A0024"",
	""PointX"": 1200,
	""PointY"": 900,
	""NeighborIdArr"": [19, 23, 29, 25],
	""IsWorkplacePoint"": false
}, {
	""Id"": 25,
	""PointName"": ""A0025"",
	""PointX"": 1200,
	""PointY"": 1400,
	""NeighborIdArr"": [20, 24, 30],
	""IsWorkplacePoint"": false
}, {
	""Id"": 26,
	""PointName"": ""A0026"",
	""PointX"": 1700,
	""PointY"": 0,
	""NeighborIdArr"": [21, 27],
	""IsWorkplacePoint"": false
}, {
	""Id"": 27,
	""PointName"": ""A0027"",
	""PointX"": 1700,
	""PointY"": 200,
	""NeighborIdArr"": [22, 26, 28],
	""IsWorkplacePoint"": false
}, {
	""Id"": 28,
	""PointName"": ""A0028"",
	""PointX"": 1700,
	""PointY"": 700,
	""NeighborIdArr"": [23, 27, 29],
	""IsWorkplacePoint"": false
}, {
	""Id"": 29,
	""PointName"": ""A0029"",
	""PointX"": 1700,
	""PointY"": 900,
	""NeighborIdArr"": [24, 28, 30],
	""IsWorkplacePoint"": false
}, {
	""Id"": 30,
	""PointName"": ""A0030"",
	""PointX"": 1700,
	""PointY"": 1400,
	""NeighborIdArr"": [25, 29],
	""IsWorkplacePoint"": false
}, {
	""Id"": 31,
	""PointName"": ""P0001"",
	""PointX"": 100,
	""PointY"": 100,
	""NeighborIdArr"": [1, 2, 6, 7],
	""IsWorkplacePoint"": true
}, {
	""Id"": 32,
	""PointName"": ""P0002"",
	""PointX"": 100,
	""PointY"": 450,
	""NeighborIdArr"": [2, 3, 7, 8],
	""IsWorkplacePoint"": true
}, {
	""Id"": 33,
	""PointName"": ""P0003"",
	""PointX"": 100,
	""PointY"": 800,
	""NeighborIdArr"": [3, 4, 8, 9],
	""IsWorkplacePoint"": true
}, {
	""Id"": 34,
	""PointName"": ""P0004"",
	""PointX"": 100,
	""PointY"": 1150,
	""NeighborIdArr"": [4, 5, 9, 10],
	""IsWorkplacePoint"": true
}, {
	""Id"": 35,
	""PointName"": ""P0005"",
	""PointX"": 400,
	""PointY"": 100,
	""NeighborIdArr"": [6, 7, 11, 12],
	""IsWorkplacePoint"": true
}, {
	""Id"": 36,
	""PointName"": ""P0006"",
	""PointX"": 400,
	""PointY"": 450,
	""NeighborIdArr"": [7, 8, 12, 13],
	""IsWorkplacePoint"": true
}, {
	""Id"": 37,
	""PointName"": ""P0007"",
	""PointX"": 400,
	""PointY"": 800,
	""NeighborIdArr"": [8, 9, 13, 14],
	""IsWorkplacePoint"": true
}, {
	""Id"": 38,
	""PointName"": ""P0008"",
	""PointX"": 400,
	""PointY"": 1150,
	""NeighborIdArr"": [9, 10, 14, 15],
	""IsWorkplacePoint"": true
}, {
	""Id"": 39,
	""PointName"": ""P0009"",
	""PointX"": 700,
	""PointY"": 100,
	""NeighborIdArr"": [11, 12, 16, 17],
	""IsWorkplacePoint"": true
}, {
	""Id"": 40,
	""PointName"": ""P0010"",
	""PointX"": 700,
	""PointY"": 450,
	""NeighborIdArr"": [12, 13, 17, 18],
	""IsWorkplacePoint"": true
}, {
	""Id"": 41,
	""PointName"": ""P0011"",
	""PointX"": 700,
	""PointY"": 800,
	""NeighborIdArr"": [13, 14, 18, 19],
	""IsWorkplacePoint"": true
}, {
	""Id"": 42,
	""PointName"": ""P0012"",
	""PointX"": 700,
	""PointY"": 1150,
	""NeighborIdArr"": [14, 15, 19, 20],
	""IsWorkplacePoint"": true
}, {
	""Id"": 43,
	""PointName"": ""P0013"",
	""PointX"": 1000,
	""PointY"": 100,
	""NeighborIdArr"": [16, 17, 21, 22],
	""IsWorkplacePoint"": true
}, {
	""Id"": 44,
	""PointName"": ""P0014"",
	""PointX"": 1000,
	""PointY"": 450,
	""NeighborIdArr"": [17, 18, 22, 23],
	""IsWorkplacePoint"": true
}, {
	""Id"": 45,
	""PointName"": ""P0015"",
	""PointX"": 1000,
	""PointY"": 800,
	""NeighborIdArr"": [18, 19, 23, 24],
	""IsWorkplacePoint"": true
}, {
	""Id"": 46,
	""PointName"": ""P0016"",
	""PointX"": 1000,
	""PointY"": 1150,
	""NeighborIdArr"": [19, 20, 24, 25],
	""IsWorkplacePoint"": true
}, {
	""Id"": 47,
	""PointName"": ""P0017"",
	""PointX"": 1450,
	""PointY"": 100,
	""NeighborIdArr"": [21, 22, 26, 27],
	""IsWorkplacePoint"": true
}, {
	""Id"": 48,
	""PointName"": ""P0018"",
	""PointX"": 1450,
	""PointY"": 450,
	""NeighborIdArr"": [22, 23, 27, 28],
	""IsWorkplacePoint"": true
}, {
	""Id"": 49,
	""PointName"": ""P0019"",
	""PointX"": 1450,
	""PointY"": 800,
	""NeighborIdArr"": [23, 24, 28, 29],
	""IsWorkplacePoint"": true
}, {
	""Id"": 50,
	""PointName"": ""P0020"",
	""PointX"": 1450,
	""PointY"": 1150,
	""NeighborIdArr"": [24, 25, 29, 30],
	""IsWorkplacePoint"": true
}]";
            #endregion JsonString 

            Points = Newtonsoft.Json.JsonConvert.DeserializeObject<List<RoadPoint>>(json);


            canvasWidth = pictureBox1.Width;
            canvasHeight = pictureBox1.Height;
            maxX = Points.Max(t => t.PointX) * 1f;
            maxY = Points.Max(t => t.PointY) * 1f;

            rx = canvasWidth / maxX;
            ry = canvasHeight / maxY;


            listStart.DisplayMember = "PointName";
            listEnd.DisplayMember = "PointName";
            Points.ForEach(p =>
            {
                listStart.Items.Add(p);
                listEnd.Items.Add(p);
            });

            Task.Run<Bitmap>(() => { return DrawPoints(Points); }).ContinueWith(t =>
            {

                if (t.IsCompleted && t.Result != null)
                {
                    this.Invoke(new Action(() =>
                    {
                        pictureBox1.Image = t.Result;
                    }));
                }

            });
        }

        //全局变量存储最大最小值,作为绘制区域
        // Graphics g = this.Map.CreateGraphics();
        static Color color = Color.Red;
        static Pen pen = new Pen(color, 4) { DashCap = DashCap.Round, DashStyle = DashStyle.Dot };

        private Bitmap DrawPoints(List<RoadPoint> points)
        {
            Bitmap temp = new Bitmap(canvasWidth, canvasHeight);
            Graphics g = Graphics.FromImage(temp);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

            foreach (var point in points)
            {
                var x = (int)Math.Floor(point.PointX * rx);
                var y = (int)Math.Floor(point.PointY * ry);
                if (x > canvasWidth - 15) x = canvasWidth - 15;
                if (y > canvasHeight - 15) y = canvasHeight - 15;

                Console.WriteLine($"{point.Id},{x},{y}");
                if (point.IsWorkplacePoint)
                    g.FillEllipse(Brushes.Green, x, y, 10, 10);

                else
                    g.FillEllipse(Brushes.Red, x, y, 10, 10);

            }

            g.ResetTransform();
            g.Dispose();

            temp.Save("point_image.png");

            return temp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listStart.SelectedIndex == -1)
            {
                labPath.Text = "请选择起点!";
                return;
            }
            if (listEnd.SelectedIndex == -1)
            {
                labPath.Text = "请选择终点!";
                return;
            }
            if (listStart.SelectedIndex == listEnd.SelectedIndex)
            {
                labPath.Text = "起点和终点不能相同!";
                return;
            }

            var start = listStart.SelectedItem as RoadPoint;
            var end = listEnd.SelectedItem as RoadPoint;

            CalcPath(start, end, Points);
        }

        private void CalcPath(RoadPoint start, RoadPoint end, List<RoadPoint> points)
        {
            var paths = new List<List<RoadPoint>>();
            foreach (var to in start.NeighborIdArr)
            {
                var path = new List<RoadPoint>() { start, points.FirstOrDefault(t => t.Id == to) };
                paths.Add(path);
            }

            var targetPath = CalcPath(end, points, paths);
            if (targetPath == null || targetPath.Count == 0)
            {
                labPath.Text = "没有检测到有效路径!";
                return;
            }

            labPath.Text = string.Join(" --> ", targetPath.Select(t => t.Id));

            Graphics g = Graphics.FromImage(pictureBox1.Image);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

            for (var i = 0; i < targetPath.Count - 1; i++)
            {
                var point1 = targetPath[i];
                var point2 = targetPath[i + 1];

                g.DrawLine(pen,
                    new Point((int)Math.Floor(point1.PointX * rx), (int)Math.Floor(point1.PointY * ry)),
                    new Point((int)Math.Floor(point2.PointX * rx), (int)Math.Floor(point2.PointY * ry)));
            }

            g.ResetTransform();
            g.Dispose();

            pictureBox1.Refresh();

        }

        private List<RoadPoint> CalcPath(RoadPoint end, List<RoadPoint> points, List<List<RoadPoint>> paths)
        {
            if (paths == null) return null;

            List<List<RoadPoint>> newPaths = new List<List<RoadPoint>>();
            foreach (var path in paths)
            {
                var currStart = path.Last();
                if (currStart == end)
                    return path;
                foreach (var to in currStart.NeighborIdArr)
                {
                    if (path.Any(t => t.Id == to)) continue;
                    var newPath = path.Concat(points.Where(t => t.Id == to)).ToList();
                    newPaths.Add(newPath);
                }
            }
            return CalcPath(end, points, newPaths);
        }
    }

    /// <summary>
    /// 道路点位
    /// </summary>
    public class RoadPoint
    {
        public int Id { get; set; }
        public string PointName { get; set; }

        public int PointX { get; set; }
        public int PointY { get; set; }

        public List<int> NeighborIdArr { get; set; } = new List<int>();

        public bool IsWorkplacePoint { get; set; }
    }
}

思路

广度优先,轮询遍历,当找到第一条通往终点的路径后就停止继续寻找了。

private List CalcPath(RoadPoint end, List points, List<List> paths)

一个轮询递归的方法


http://www.kler.cn/a/3991.html

相关文章:

  • 差异基因富集分析(R语言——GOKEGGGSEA)
  • Java 8 Optional类
  • 寒假1.18
  • Docker部署Redis
  • FPGA车牌识别
  • STM32的集成开发环境STM32CubeIDE安装
  • STM32输出PWM波控制电机转速,红外循迹避障智能车+L298N的详细使用手册、接线方法及工作原理,有代码
  • UniApp + SpringBoot 实现接入支付宝支付功能和退款功能
  • 【面试题系列】K8S面试题(二)
  • Java基础 -- 枚举类Enum
  • 走进小程序【一】什么是小程序?
  • 【蓝桥杯】​蓝桥杯——每日四道编程题(两道真题+两道模拟)​| 第 二 天
  • 【VB6|第17期】16进制颜色值与RGB值互相转换(含源码)
  • Node.js学习笔记——Node.js模块化
  • 一文彻底搞懂为什么OpenCV用GPU/cuda跑得比用CPU慢?
  • Python 十大开源Python库,看看你熟悉几个?
  • cpu报警
  • 耐心排序之最长递增子序列(LIS)
  • 数仓必备概念
  • 电子拣货标签13代系统简介
  • 【洛谷 P2249】【深基13.例1】查找(向量+二分查找+递归)
  • ThreadLocal原理 、什么是内存泄漏
  • 大量产品“GPT 化”,开源大模型 AI 应用开发框架发布
  • STM32——IIC总线(MPU6050应用)
  • C++中的HTTP协议问题
  • JAVA开发与运维(云安全产品)