/// <summary> /// 获取某一个城市到某个网格点的直线距离所需的是时间 /// </summary> /// <param name="i"></param> /// <param name="j"></param> /// <param name="cityName"></param> /// <returns></returns> public float ShortestTimeCost(int i, int j,Postion cityPos) { double x = Math.Abs(i - cityPos.XIndex)*Info.XCellSize; double y = Math.Abs(j - cityPos.YIndex)*Info.YCellSize; double distance = Math.Sqrt(x*x + y*y)/1000; return (float)(distance/Speed*60); }
private void InitializeStart(Postion startPostion,RasterPositionValue[,] cost) { //startPostion.HasValue = true; //startPostion.RasterValue = 0; //startPostion.Visited = true; cost[startPostion.XIndex, startPostion.YIndex].HasValue = true; cost[startPostion.XIndex, startPostion.YIndex].Visited = true; cost[startPostion.XIndex, startPostion.YIndex].RasterValue = 0; }
public IEnumerable<Postion> Sourround(RasterPositionValue[,] cost, Postion postion) { if (VisistedValid(cost, postion.LeftTop())) yield return postion.LeftTop(); if (VisistedValid(cost, postion.Top())) yield return postion.Top(); if (VisistedValid(cost, postion.RightTop())) yield return postion.RightTop(); if (VisistedValid(cost, postion.Right())) yield return postion.Right(); if (VisistedValid(cost, postion.RightButtom())) yield return postion.RightButtom(); if (VisistedValid(cost, postion.Buttom())) yield return postion.Buttom(); if (VisistedValid(cost, postion.LeftButtom())) yield return postion.LeftButtom(); if (VisistedValid(cost, postion.Left())) yield return postion.Left(); }
private bool VisistedValid(RasterPositionValue[,] cost,Postion pos) { //return ValueValid(pos.XIndex, pos.YIndex) // &&!cost[pos.XIndex,pos.YIndex].Visited; return pos.XIndex >= 0 && pos.XIndex < Width && pos.YIndex >= 0 && pos.YIndex < Height && _raster[pos.XIndex, pos.YIndex].HasValue && cost[pos.XIndex, pos.YIndex].Visited == false; }
public RasterOp Calculator(Postion startPostion) { if (!Read(startPostion.XIndex,startPostion.YIndex).HasValue) throw new ArgumentOutOfRangeException("起始位置不含有数据"); RasterPositionValue[,] cost=new RasterPositionValue[Width,Height]; //初始化操作 InitializeValues(cost); InitializeStart(startPostion,cost); //使用orderbag,是的每次取出的最小值 OrderedBag<RasterPositionValue> bag=new OrderedBag<RasterPositionValue>(); bag.Add(cost[startPostion.XIndex,startPostion.YIndex]); while (bag.Count!=0) { RasterPositionValue pos = bag.RemoveFirst(); var postions = Sourround(cost, pos); foreach (var postion in postions) { double relativeCost = Read(postion.XIndex, postion.YIndex).GetValueOrDefault() * 0.5 + Read(postion.XIndex, postion.YIndex).GetValueOrDefault()*0.5; if (pos.XIndex!=postion.XIndex&&pos.YIndex!=postion.YIndex) { relativeCost *= Math.Sqrt(2); } cost[postion.XIndex, postion.YIndex].Visited = true; cost[postion.XIndex, postion.YIndex].HasValue = true; cost[postion.XIndex, postion.YIndex].RasterValue = (float) relativeCost +cost[pos.XIndex,pos.YIndex].RasterValue; bag.Add(cost[postion.XIndex, postion.YIndex]); } } return Result(cost); }