A*算法,显示路径

 

None.gif private   void  DrawPath( long  nStart,  long  nEnd)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Cursor.Current 
= Cursors.WaitCursor;
InBlock.gif        
if (nStart < 1)
InBlock.gif            nStart 
= 1;
InBlock.gif        
else if (nStart > mDataList.Num)
InBlock.gif            nStart 
= mDataList.Num;
InBlock.gif        
if (nEnd < 1)
InBlock.gif            nEnd 
= 1;
InBlock.gif        
else if (nEnd > mDataList.Num) 
InBlock.gif            nEnd 
= mDataList.Num;
InBlock.gif        TNodeLink startLink 
= new TNodeLink();
InBlock.gif        startLink.G 
= 0;
InBlock.gif        startLink.Current 
= mDataList[nStart];
InBlock.gif
InBlock.gif        TNodeLink endLink 
= new TNodeLink();
InBlock.gif        endLink.H 
= 0;
InBlock.gif        endLink.Current 
= mDataList[nEnd];
InBlock.gif            
InBlock.gif        
//距离估价函数(合理性?)
InBlock.gif
        startLink.H = THelp.Dist(startLink.Current.Sx, endLink.Current.Sx, startLink.Current.Sy, endLink.Current.Sy);
InBlock.gif        endLink.G 
= startLink.H;
InBlock.gif
InBlock.gif        
//估价函数F(n)=G(n)+H(n)
InBlock.gif
        startLink.F = startLink.G + startLink.H;
InBlock.gif        endLink.F 
= endLink.G + endLink.H;
InBlock.gif
InBlock.gif        OPEN  
= new TNodeColl();
InBlock.gif        CLOSE 
= new TNodeColl();
InBlock.gif        OPEN.AddNode(startLink);
InBlock.gif
InBlock.gif        TNodeLink parent,tmp;
InBlock.gif
InBlock.gif        
//设置坐标系NAD83
InBlock.gif
        MapInfo.Geometry.CoordSys coordSys = MapInfo.Engine.Session.Current.CoordSysFactory.CreateLongLat(DatumID.NAD83);
InBlock.gif
InBlock.gif        
while (!OPEN.IsNull())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            tmp 
= OPEN.FetchBest();//寻找最近节点链表
InBlock.gif
            parent = tmp;
InBlock.gif            OPEN.RemoveNode(tmp);
InBlock.gif            
if(tmp.Current == endLink.Current)//若最佳路线已经找到
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                    
ContractedSubBlock.gifExpandedSubBlockStart.gif                
由CLOSE直接添加线段#region 由CLOSE直接添加线段
InBlock.gif                TNodeLink tmpLink
=CLOSE.Foot;
InBlock.gif
InBlock.gif                
//创建表
InBlock.gif
                MapInfo.Data.TableInfoMemTable ti=new MapInfo.Data.TableInfoMemTable("pathLine");
InBlock.gif                ti.Columns.Add(MapInfo.Data.ColumnFactory.CreateFeatureGeometryColumn(coordSys));
InBlock.gif                ti.Columns.Add(MapInfo.Data.ColumnFactory.CreateStyleColumn());
InBlock.gif                MapInfo.Data.Table  table 
= MapInfo.Engine.Session.Current.Catalog.CreateTable(ti);
InBlock.gif            
InBlock.gif            
InBlock.gif                
//线段创建条数跟踪测试
InBlock.gif
                int segmentTest=1;
InBlock.gif
InBlock.gif                
//计算距离
InBlock.gif
                double tmpDistance;
InBlock.gif
InBlock.gif                MapInfo.Styles.LineWidth lineWidth 
= new MapInfo.Styles.LineWidth(3,MapInfo.Styles.LineWidthUnit.Pixel);
InBlock.gif                MapInfo.Styles.SimpleLineStyle simpleLineStyle 
= new MapInfo.Styles.SimpleLineStyle (lineWidth,2,System.Drawing.Color.Red);
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                
while(null!=CLOSE.Fetch(tmpLink.Current.Parent))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//确定线段
InBlock.gif
                    MapInfo.Geometry.DPoint startDPoint,endDPoint;
InBlock.gif                    startDPoint.x
=tmpLink.Current.Sx;
InBlock.gif                    startDPoint.y
=tmpLink.Current.Sy;
InBlock.gif                    endDPoint.x
=CLOSE.Fetch(tmpLink.Current.Parent).Current.Sx;
InBlock.gif                    endDPoint.y
=CLOSE.Fetch(tmpLink.Current.Parent).Current.Sy;
InBlock.gif
InBlock.gif                    
//旅行距离计算
InBlock.gif
                    tmpDistance=111*Math.Sqrt((startDPoint.x-endDPoint.x)*(startDPoint.x-endDPoint.x)+(startDPoint.y-endDPoint.y)*(startDPoint.y-endDPoint.y));
InBlock.gif                
InBlock.gif
InBlock.gif                    
//创建线段
InBlock.gif
                    MapInfo.Geometry.MultiCurve line=MapInfo.Geometry.MultiCurve.CreateLine(coordSys,startDPoint,endDPoint);
InBlock.gif
InBlock.gif                    
//添加特征
InBlock.gif
                    MapInfo.Data.Feature f=new Feature(line,simpleLineStyle);//单独设置当前feature的样式的方法
InBlock.gif
                    table.InsertFeature(f);
InBlock.gif
InBlock.gif                    tmpLink
=CLOSE.Fetch(tmpLink.Current.Parent);
InBlock.gif                        
InBlock.gif
InBlock.gif                    
//计算总旅行距离
InBlock.gif
                    distance+=tmpDistance;
InBlock.gif
InBlock.gif
InBlock.gif                    
//线段创建条数跟踪测试
InBlock.gif
                    segmentTest+=1;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
//添加到图层并显示
InBlock.gif
                MapInfo.Mapping.IMapLayer layer=new MapInfo.Mapping.FeatureLayer(table);
InBlock.gif                MapControl1.Map.Layers.Insert(
2,layer);
InBlock.gif
ExpandedSubBlockEnd.gif                
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            TNodeLink child 
= null;
InBlock.gif            parent 
= tmp;
InBlock.gif            
for (int i=0; i<tmp.Current.NodeNum; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (OPEN.ExistNode(parent.Current[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    child 
= OPEN.Fetch(parent.Current[i]);
InBlock.gif                    
double mf, mg, mh;
InBlock.gif                    mf 
= mg = mh = 0;
InBlock.gif
InBlock.gif                    
//计算实际代价
InBlock.gif
                    mg = parent.G + THelp.Dist(parent.Current.Sx,parent.Current[i].Sx,parent.Current.Sy,parent.Current[i].Sy);
InBlock.gif
InBlock.gif                    
//计算估计代价,距离算法
InBlock.gif
                    mh = THelp.Dist(parent.Current[i].Sx,endLink.Current.Sx,parent.Current[i].Sy, endLink.Current.Sy);
InBlock.gif
InBlock.gif                    
//估价函数
InBlock.gif
                    mf = mg + mh;
InBlock.gif                    
if (mf < child.F)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        child.G 
= mg;
InBlock.gif                        child.H 
= mh;
InBlock.gif                        child.F 
= mf;
InBlock.gif                        child.Current.Parent 
= parent.Current;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (CLOSE.ExistNode(parent.Current[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    child 
= new TNodeLink();
InBlock.gif                    child.Current 
= parent.Current[i];
InBlock.gif                    child.Current.Parent 
= parent.Current;
InBlock.gif                    child.G 
= parent.G + THelp.Dist(parent.Current.Sx,parent.Current[i].Sx,parent.Current.Sy,parent.Current[i].Sy);
InBlock.gif                    child.H 
= THelp.Dist(parent.Current[i].Sx, endLink.Current.Sx,parent.Current[i].Sy, endLink.Current.Sy);
InBlock.gif                    child.F 
= child.G + child.H;
InBlock.gif                    OPEN.AddNode(child);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            CLOSE.AddNode(parent);
InBlock.gif            
if (tmp.Current == endLink.Current)
InBlock.gif                CLOSE.AddNode(tmp);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        CLOSE.PrintPath();
//测试树,待删!
InBlock.gif            
InBlock.gif        
//计算旅行时间
InBlock.gif
        time=distance/40.0;
InBlock.gif            
InBlock.gif        
//地图更新
InBlock.gif
        TextBox7.Text=distance.ToString("0.00");
InBlock.gif        TextBox8.Text
=time.ToString("0.00");
InBlock.gif        MapControl1.Map.Invalidate();
InBlock.gif
InBlock.gif        
//恢复鼠标状态
InBlock.gif
        Cursor.Current=Cursors.Default;
ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/yuxon/archive/2006/06/28/437534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值