static void FloodFillBlob(NMap map, NLocate loc, NLocateSet set, byte valid) { if (!map.WithIn(loc)) { return; } if (map.GetBlock(loc.X, loc.Y) == valid) { map.SetBlock(loc.X, loc.Y, (byte)(255 - valid)); set.Set.Add(loc); foreach (var l in loc.Direction4()) { FloodFillBlob(map, l, set, valid); } } }
public static NBlob Find(NMap map, byte valid) { var res = new NBlob(); var tmp = new NMap(map); for (int i = 0; i < tmp.DataSize(); i++) { if (tmp.GetBlock(i) == valid) { var set = new NLocateSet(); FloodFillBlob(tmp, new NLocate(i % tmp.Width, i / tmp.Width), set, valid); res.Blobs.Add(set); } } return(res); }
public double DistanceTo(NLocateSet set, out NLocate srcLoc, out NLocate dstLoc) { double min = double.MaxValue; srcLoc = new NLocate(0, 0); dstLoc = new NLocate(0, 0); foreach (var l in set.Set) { NLocate nearestLoc; var dis = DistanceTo(l, out nearestLoc); if (dis < min) { min = dis; srcLoc = nearestLoc; dstLoc = l; } } return(min); }