SF2 DrainageMonoDirr() { SF2 res = new SF2(this, 1); List <SP2> fieldcopy = getFieldCopy(); fieldcopy.Sort((x, y) => y.CompareTo(x)); for (int i = 0; i < fieldcopy.Count(); i++) { ResultCheckSlope result = CheckSlope(fieldcopy[i]); if (result.nb > 0) { float sp = res.at(fieldcopy[i].pos.x, fieldcopy[i].pos.y); for (int ind = 0; ind < result.nb; ind++) { float curr = res.at(result.q[ind].pos.x, result.q[ind].pos.y); float set = curr + (sp * result.avSlope[ind]); set = Mathf.Clamp(set, 0, 1000); res.setAt(result.q[ind].pos.x, result.q[ind].pos.y, set); if (res.min > set) { res.min = set; } if (res.max < set) { res.max = set; } } } } return(res); }
public SF2 getSlopeMap() { SF2 res = new SF2(this, 0); for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { float slopeij = slope(i, j); res.setAt(i, j, slopeij); if (res.min > slopeij) { res.min = slopeij; } if (res.max < slopeij) { res.max = slopeij; } } } SlopeMapUptoDate = true; return(res); }
public SF2 CalculateRoad() { if (!needCalculateRoad) { return(this.Road); } needCalculateRoad = false; var watch = System.Diagnostics.Stopwatch.StartNew(); SF2 res = new SF2(this, 0); Graph graph = new Graph(nx * ny); for (int i = 0; i < nx; i++) { for (int j = 0; j < ny; j++) { float slopeij = SlopeMap.at(i, j); for (int k = -1; k < 2; k++) { for (int l = -1; l < 2; l++) { int ind1 = index(i, j); int ind2 = index(i + k, j + l); if (k == 0 && l == 0) { continue; } if ((i + k) <= 0 || (i + k) >= nx - 1 || (j + l) <= 0 || (j + l) >= ny - 1) { continue; } //cost = 1.2^DeltaSlope * dist float deltaSlope = slopeij - SlopeMap.at(i + k, j + l); deltaSlope *= 8; float cost; float baseLength; if ((l + k) % 2 != 0) { baseLength = 1f; } else { baseLength = Mathf.Sqrt(2); } cost = baseLength * Mathf.Pow(10f, deltaSlope); bool isok = graph.AddEdge(ind1, ind2, cost); } } } } int startP = index(1000, 10); int dest = index(10, 1000); var path = graph.FindPath(startP); //Debug.Log(path[dest].distance); List <(int index, float linkcost)> resultPath = Path(startP, dest); List <(int index, float linkcost)> Path(int start, int destination) { List <(int index, float linkcost)> pathlist = new List <(int index, float linkcost)>(); pathlist.Add((destination, (float)(path[destination].distance - path[path[destination].prev].distance))); for (int i = destination; i != start; i = path[i].prev) { float linkcost = (float)(path[path[i].prev].distance - path[path[path[i].prev].prev].distance); pathlist.Add((path[i].prev, linkcost)); } return(pathlist); } res.max = resultPath[0].linkcost; res.min = resultPath[0].linkcost; for (int i = 0; i < resultPath.Count; i++) { int rx; int ry; var revind = reverseIndex(resultPath[i].index); rx = revind.x; ry = revind.y; res.setAt(rx, ry, resultPath[i].linkcost); if (res.max < resultPath[i].linkcost) { res.max = resultPath[i].linkcost; } if (res.min > resultPath[i].linkcost) { res.min = resultPath[i].linkcost; } } watch.Stop(); long elapsedMs = watch.ElapsedMilliseconds; Debug.Log(elapsedMs); return(res); }