dartterm/lib/algorithms/regionalize.dart

67 lines
1.8 KiB
Dart
Raw Normal View History

2023-09-21 02:30:03 +00:00
import 'dart:math' as math;
class Region {
final math.Rectangle<int> rect;
final Set<(int, int)> points;
Region(this.rect, this.points);
static 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);
}
}
List<Region> regionalize(
math.Rectangle<int> rect, bool Function(int, int) isAccessible) {
int nextRegion = 0;
Map<(int, int), int> regions = {};
void floodfill(int x, int y, region) {
if (!rect.containsPoint(math.Point(x, y))) {
return;
}
if (regions[(x, y)] != null) {
return;
}
if (!isAccessible(x, y)) {
return;
}
regions[(x, y)] = region;
floodfill(x - 1, y, region);
floodfill(x + 1, y, region);
floodfill(x, y - 1, region);
floodfill(x, y + 1, region);
}
// TODO: This can be done more efficiently with a union/find data structure
// But this is an easy implementation to understand
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;
}
}
}
return _toExplicit(regions);
}
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));
}
return [for (var s in pointsOut) Region.fromNonEmptySet(s)];
}