《校园地图》的剖析
《校園地圖》的剖析
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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
轉載于:https://www.cnblogs.com/smalle/p/10469716.html
總結
- 上一篇: String 和Integer、int之
- 下一篇: 面试题之在字符串中查找出第一个只出现一次