Well, it's sort of working

This commit is contained in:
2023-09-20 20:56:30 -07:00
parent b5466919e6
commit c4c2b26653
13 changed files with 217 additions and 115 deletions

View File

@ -4,17 +4,24 @@ class Region {
final math.Rectangle<int> rect;
final Set<(int, int)> points;
bool get isRectangle => points.length == rect.width * rect.height;
Region(this.rect, this.points);
static fromNonEmptySet(Set<(int, int)> s) {
static Region fromNonEmptySet(Set<(int, int)> s) {
assert(s.isNotEmpty);
int xMin = s.map<int>((xy) => xy.$1).reduce(math.min);
int yMin = s.map<int>((xy) => xy.$2).reduce(math.min);
int xMax = s.map<int>((xy) => xy.$1).reduce(math.max);
int yMax = s.map<int>((xy) => xy.$2).reduce(math.max);
var rect = math.Rectangle.fromPoints(
math.Point(xMin, yMin), math.Point(xMax, yMax));
Region(rect, s);
math.Point(xMin, yMin), math.Point(xMax + 1, yMax + 1));
return Region(rect, s);
}
@override
String toString() {
return "Region($rect, $points)";
}
}
@ -23,22 +30,25 @@ List<Region> regionalize(
int nextRegion = 0;
Map<(int, int), int> regions = {};
void floodfill(int x, int y, region) {
int floodfill(int x, int y) {
int workDone = 0;
if (!rect.containsPoint(math.Point(x, y))) {
return;
return workDone;
}
if (regions[(x, y)] != null) {
return;
return workDone;
}
if (!isAccessible(x, y)) {
return;
return workDone;
}
regions[(x, y)] = region;
floodfill(x - 1, y, region);
floodfill(x + 1, y, region);
floodfill(x, y - 1, region);
floodfill(x, y + 1, region);
regions[(x, y)] = nextRegion;
workDone += 1;
workDone += floodfill(x - 1, y);
workDone += floodfill(x + 1, y);
workDone += floodfill(x, y - 1);
workDone += floodfill(x, y + 1);
return workDone;
}
// TODO: This can be done more efficiently with a union/find data structure
@ -46,21 +56,20 @@ List<Region> regionalize(
for (var y = rect.top; y < rect.bottom; y++) {
for (var x = rect.left; x < rect.right; x++) {
if (regions[(x, y)] == null) {
floodfill(x, y, nextRegion);
nextRegion += 1;
if (floodfill(x, y) > 0) {
nextRegion += 1;
}
}
}
}
return _toExplicit(regions);
return _toExplicit(regions, nextRegion);
}
List<Region> _toExplicit(Map<(int, int), int> regions) {
List<Set<(int, int)>> pointsOut = [
for (var i = 0; i < regions.length; i++) Set()
];
for (var MapEntry(key: (x, y), value: id_) in regions.entries) {
pointsOut[id_].add((x, y));
List<Region> _toExplicit(Map<(int, int), int> pointRegions, int nRegions) {
List<Set<(int, int)>> regionPoints = [for (var i = 0; i < nRegions; i++) {}];
for (var MapEntry(key: (x, y), value: id_) in pointRegions.entries) {
regionPoints[id_].add((x, y));
}
return [for (var s in pointsOut) Region.fromNonEmptySet(s)];
return [for (var s in regionPoints) Region.fromNonEmptySet(s)];
}