《校园地图》的剖析

                                               《校园地图》的剖析
1.该项目小地图的功能
          该小地图的主要功能是从下拉列表中选择出发地和目的地,然后地图上可以显示路线。主要的显示方法是通过贴图来显示
2.实现该小地图软件的代码
          typedef struct road_dot{
        int i;   // 该节点的id
        int vistable; //该节点是否可访问
        cv::Point self; //该节点在地图上的位置
        std::vector<size_t> others_id;  // 保存与该节点相连的节点的id
    }Road_node;
      该段代码用来记录所有的路网节点
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(0<x&&x<1480&&0<y&&y<800)  // if on the pic
    {
 
         Road_node new_node;
         new_node.self = Point(x,y);
         new_node.i = 200;  // not a pos
         cv::circle(img,Point(x,y),6,cv::Scalar(255,0,255),-1);
         qimg = Mat2QImage(img);
         ui->pic->setPixmap(QPixmap::fromImage(qimg));
 

         if(!first_node)
         {start_node = new_node;start_set_flag = 1; first_node =2;pre_nodes_id = 0;}
         for(int i = 0;i<point.size();i++)
         {
             if((abs(x-point[i].x) <10 )&& (abs(y-point[i].y)<10))  // if at pos
               {
                 new_node.i = i;
                 qDebug()<<"this is pos "<<i;
                 break;
               }
         }
         road.push_back(new_node);// record this new node ;

       for(int i = 0;i<road.size()-1;i++)
       {
           if((abs(x-road[i].self.x) <10 )&& (abs(y-road[i].self.y)<10))  // if at node
           {  // yes
               road.pop_back();  // drop this node

               if(event->button() == Qt::LeftButton)   // if leftButton
                {
                   start_node = road[i];  // set start node
                   start_set_flag = 1;
                   pre_nodes_id = i;
                }
               else if(event->button() == Qt::RightButton)
               {
                   start_node = road[pre_nodes_id];   // this is start
                   start_set_flag = 1;
                   end_node = road[i];
                   end_set_flag = 1;
                   road[i].others_id.push_back(pre_nodes_id);
                   road[pre_nodes_id].others_id.push_back(i);
                    pre_nodes_id = i;
               }
               qDebug()<<"this node's others.size = "<<road[i].others_id.size();
 
               break;
            }
           else if(i == road.size() - 2)
           {  // no
               if(!start_set_flag) // if have no start_node
               {
                   start_node = road[pre_nodes_id];
                   start_set_flag = 1;
               }
               if(!end_set_flag)
               {
                   end_node = road[road.size()-1];
                   end_set_flag = 1;
               }
               road[pre_nodes_id].others_id.push_back(road.size()-1);  // push now node
               road[road.size()-1].others_id.push_back(pre_nodes_id);
               qDebug()<<"this node's others.size = "<<road[road.size()-1].others_id.size();
               pre_nodes_id = road.size()-1;
               break;
           }
       }
 
       if(end_set_flag && start_set_flag)
       {
           end_set_flag = 0;
           start_set_flag = 0;
           cv::line(img,start_node.self,end_node.self,cv::Scalar(0,0,0),3);
           qimg = Mat2QImage(img);
           ui->pic->setPixmap(QPixmap::fromImage(qimg)); // flush the pic
       }

     qDebug()<<"road.size"<<road.size();
    }

}  // :)
             该段代码用来标记路网并自动储存,主要通过在地图上点击来标记,右键表示一个连续段的结束,每个节点会记录与之相连的其它节点的id号。
int start_id = ui->comboBox->currentIndex();  // get start and end node's id /
    int end_id = ui->comboBox_2->currentIndex();
    for(int i = 0;i<road.size();i++)
    {
        road[i].vistable = 0;  // set all nodes are visitable;
    }
  //  Road_node start_node_temp,end_node_temp;  // temp node
    int road_start_i,road_end_i;
    int road_start_i_temp,road_end_i_temp;

    for(int i = 0;i<road.size();i++) // search which node is on the start/end point;
    {
        if(road[i].i == start_id)
        {
            qDebug()<<"start"<<i;
            start_node = road[i];
            road_start_i = i;  // get the id of start roadnode;
            qDebug()<<"i = "<<start_node.i;
        }
        else if(road[i].i == end_id)
        {
            qDebug()<<"end"<<i;
            end_node = road[i];
            road_end_i = i;  // get the id of end roadnode;
            qDebug()<<"i = "<<end_node.i;
        }
    }
    road_start_i_temp = road_start_i;
    road_end_i_temp = road_start_i;
   // start_node_temp = start_node;
    //end_node_temp = start_node_temp;
    std::vector<int> road_temp;
    int pre_distance = 9000000;
    while(road[road_end_i_temp].i!= end_id)
    //for(int xx = 0;xx<10;xx++)
    {
        int useable_flag = 0;
        for(int i = 0;i<road[road_end_i_temp].others_id.size();i++)   // if the current endpoint to have next point
        {
            if(road[road[road_end_i_temp].others_id[i]].vistable == 0)
                useable_flag = 1;  // have useable point
        }
        if(useable_flag)   // have  useable next node
        {
           // start_node_temp = end_node_temp;
            road_start_i_temp = road_end_i_temp;
            road[road_start_i_temp].vistable = 1; // have visited it;
            //start_node_temp.vistable = 1; // have visited it;
            road_temp.push_back(road_start_i_temp);   // record the walked node
            int temp_min_id;
            for(int i = 0;i<road[road_start_i_temp].others_id.size();i++)  // find the min dis node   // int these next points
            {
                if(road[road[road_start_i_temp].others_id[i]].vistable == 0 )  // if this node have not visited;
                {
             int tempx,tempy;

             tempx = (road[road[road_start_i_temp].others_id[i]].self.x- end_node.self.x)*(road[road[road_start_i_temp].others_id[i]].self.x - end_node.self.x);
             tempy = (road[road[road_start_i_temp].others_id[i]].self.y- end_node.self.y)*(road[road[road_start_i_temp].others_id[i]].self.y - end_node.self.y);
             if(pre_distance<(tempx+tempy))
                {
                 qDebug()<<"pre_distance = "<<pre_distance;
                 qDebug()<<"now distance = "<<tempx+tempy;
                  //  end_node_temp = road[road[road_start_i_temp].others_id[i-1]];
                    road_end_i_temp = road[road_start_i_temp].others_id[temp_min_id];
                }
             else {
                    //end_node_temp = road[start_node_temp.others_id[i]];
                    road_end_i_temp = road[road_start_i_temp].others_id[i];
                    temp_min_id = i;
                    pre_distance = tempx+tempy;
                   }
                }
            }
            pre_distance = 9000000;
 
 
        }
   else // have no useable next node
        {
            road[road_end_i_temp].vistable = 1;
          loop:    for(int i = 0;i<road[road_start_i_temp].others_id.size();i++)  // if have other useable nodes
            {
                if(road[road[road_start_i_temp].others_id[i]].vistable == 0)
                    useable_flag = 1;  // have useable point
            }
            if(useable_flag)
            {
                int temp_min_id;
                for(int i = 0;i<road[road_start_i_temp].others_id.size();i++)  // fine the min dis node
                {
                    if(road[road[road_start_i_temp].others_id[i]].vistable == 0 )  // if this node have not visited;
                    {
                 int tempx,tempy;

                 tempx = (road[road[road_start_i_temp].others_id[i]].self.x- end_node.self.x)*(road[road[road_start_i_temp].others_id[i]].self.x - end_node.self.x);
                 tempy = (road[road[road_start_i_temp].others_id[i]].self.y- end_node.self.y)*(road[road[road_start_i_temp].others_id[i]].self.y - end_node.self.y);
                 if(pre_distance<(tempx+tempy))
                    {
                        //end_node_temp = road[road[road_start_i_temp].others_id[i-1]];
                        road_end_i_temp = road[road_start_i_temp].others_id[temp_min_id];
                    }
                 else {
                        //end_node_temp = road[start_node_temp.others_id[i]];
                        road_end_i_temp = road[road_start_i_temp].others_id[i];
                        temp_min_id = i;
                        pre_distance = tempx+tempy;
                       }
                    }
                }
                pre_distance = 9000000;
            }
            else
            {
                road_temp.pop_back(); // drop this start_temp node
                road_start_i_temp = road_temp[road_temp.size()-1];
                goto loop;
            }
        }
 road_temp.push_back(road_end_i_temp);
    for(int i = 0;i<road_temp.size()-1;i++)
    {
        cv::line(img,road[road_temp[i]].self,road[road_temp[i+1]].self,cv::Scalar(0,0,0),3);
        qimg = Mat2QImage(img);
        ui->pic->setPixmap(QPixmap::fromImage(qimg)); // flush the pic
    }
    qDebug()<<road_temp.size();
}
该代码为寻路算法,通过设立两个哨兵,一个在当前出发节点(哨兵A),另一个在下一个目标出发点(哨兵 B),下一个目标出发点由所有子节点中距离目的地最近的节点确定。当B所在的节点具有可走子节点(不是死胡同)并且还未达到目的地时,A走到B(所有走过的路压栈,方便后退),B继续探路,若走到死胡同就后退,走其他的最近子节点。
3.优缺点
      优点:
           (1)有利于新生和学生父母便于在学校行走,不居于在学校乱走
           (2)能显示学校具体地点的具体信息,有利于了解学校概况
      缺点:
            (1)涉众不多,一旦新生适应了就不会用了,则客户群不稳定
            (2)功能较少,没新鲜点和创新点
4.增加点
             可以和学校的社团,美食街,校团委合作,在校园地图的软件上用图片展示学校的社团的活动,美食,团委消息,赛事等等
5.代码来源
作者:whlook
来源:CSDN
原文:https://blog.csdn.net/whlook/article/details/77076395
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/smalle/p/10469716.html