2023-09-21 02:30:03 +00:00
|
|
|
import 'dart:math' as math;
|
|
|
|
|
2023-09-21 22:37:12 +00:00
|
|
|
import 'package:dartterm/algorithms/geometry.dart' as geo;
|
|
|
|
|
2023-09-21 02:30:03 +00:00
|
|
|
class Region {
|
2023-09-21 23:11:34 +00:00
|
|
|
final geo.Rect rect;
|
2023-09-21 02:30:03 +00:00
|
|
|
final Set<(int, int)> points;
|
|
|
|
|
2023-09-21 23:11:34 +00:00
|
|
|
bool get isRectangle => points.length == rect.dx * rect.dy;
|
2023-09-21 03:56:30 +00:00
|
|
|
|
2023-09-21 02:30:03 +00:00
|
|
|
Region(this.rect, this.points);
|
|
|
|
|
2023-09-21 03:56:30 +00:00
|
|
|
static Region fromNonEmptySet(Set<(int, int)> s) {
|
2023-09-21 02:30:03 +00:00
|
|
|
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);
|
2023-09-21 23:11:34 +00:00
|
|
|
var rect = geo.Rect(xMin, yMin, xMax - xMin + 1, yMax - yMin + 1);
|
2023-09-21 03:56:30 +00:00
|
|
|
return Region(rect, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return "Region($rect, $points)";
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 02:29:34 +00:00
|
|
|
(List<Region>, Map<(int, int), int>) regionalize(
|
|
|
|
geo.Rect rect, bool Function(int, int) isAccessible) {
|
2023-09-21 02:30:03 +00:00
|
|
|
int nextRegion = 0;
|
|
|
|
Map<(int, int), int> regions = {};
|
|
|
|
|
2023-09-21 03:56:30 +00:00
|
|
|
int floodfill(int x, int y) {
|
|
|
|
int workDone = 0;
|
2023-09-21 22:37:12 +00:00
|
|
|
if (!rect.containsPoint(geo.Offset(x, y))) {
|
2023-09-21 03:56:30 +00:00
|
|
|
return workDone;
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
if (regions[(x, y)] != null) {
|
2023-09-21 03:56:30 +00:00
|
|
|
return workDone;
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
if (!isAccessible(x, y)) {
|
2023-09-21 03:56:30 +00:00
|
|
|
return workDone;
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 03:56:30 +00:00
|
|
|
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;
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This can be done more efficiently with a union/find data structure
|
|
|
|
// But this is an easy implementation to understand
|
2023-09-21 22:37:12 +00:00
|
|
|
for (var y = rect.y0; y < rect.y1; y++) {
|
|
|
|
for (var x = rect.x0; x < rect.x1; x++) {
|
2023-09-21 02:30:03 +00:00
|
|
|
if (regions[(x, y)] == null) {
|
2023-09-21 03:56:30 +00:00
|
|
|
if (floodfill(x, y) > 0) {
|
|
|
|
nextRegion += 1;
|
|
|
|
}
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 02:29:34 +00:00
|
|
|
return (_toExplicit(regions, nextRegion), regions);
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 03:56:30 +00:00
|
|
|
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));
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 03:56:30 +00:00
|
|
|
return [for (var s in regionPoints) Region.fromNonEmptySet(s)];
|
2023-09-21 02:30:03 +00:00
|
|
|
}
|