dartterm/lib/algorithms/geometry.dart

67 lines
1.1 KiB
Dart
Raw Normal View History

// Dart has a habit of using double-inclusive bounds,
// and I strongly prefer half-open bounds
//
// So: Here's a reimplementation of the geometry I need
2023-09-21 23:11:34 +00:00
import 'package:dartterm/skreek.dart';
class Size {
final int dx;
final int dy;
Size(this.dx, this.dy) {
assert(dx >= 0);
assert(dy >= 0);
}
2023-09-21 23:11:34 +00:00
@override
String toString() {
return "$dx x $dy";
}
}
class Offset {
final int x;
final int y;
const Offset(this.x, this.y);
2023-09-21 23:11:34 +00:00
@override
String toString() {
return "@($x, $y)";
}
}
class Rect {
final int x0;
final int y0;
final int dx;
final int dy;
int get x1 => x0 + dx;
int get y1 => y0 + dy;
Rect(this.x0, this.y0, this.dx, this.dy) {
assert(dx >= 0);
assert(dy >= 0);
}
2023-09-21 23:11:34 +00:00
Size get size => Size(dx, dy);
bool contains(int x, int y) {
return x0 <= x && x < x1 && y0 <= y && y < y1;
}
bool containsPoint(Offset xy) {
return contains(xy.x, xy.y);
}
bool containsRect(Rect rect) {
return x0 <= rect.x0 && y0 <= rect.y0 && rect.x1 <= x1 && rect.y1 <= y1;
}
2023-09-21 23:11:34 +00:00
@override
String toString() {
return "@($x0, $y0) $size";
}
}