Skip to content

Commit

Permalink
Add an assert (open to other means of handling) to validate isPointIn…
Browse files Browse the repository at this point in the history
…Polygon inputs. Would have caught previous misuse.
  • Loading branch information
barfootsies committed Jun 6, 2024
1 parent 0f01a8b commit bb3f8be
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
27 changes: 14 additions & 13 deletions benchmark/point_in_polygon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ Future<Result> timedRun(String name, dynamic Function() body) async {
return (name: name, duration: watch.elapsed);
}

List<Offset> makeCircle(int points, double radius, double phase) {
final slice = math.pi * 2 / (points - 1);
return List.generate(points, (i) {
// Note the modulo is only there to deal with floating point imprecision
// and ensure first == last.
final angle = slice * (i % (points - 1)) + phase;
return Offset(radius * math.cos(angle), radius * math.sin(angle));
}, growable: false);
}

// NOTE: to have a more prod like comparison, run with:
// $ dart compile exe benchmark/crs.dart && ./benchmark/crs.exe
//
Expand All @@ -35,36 +45,27 @@ Future<void> main() async {

final results = <Result>[];
const N = 3000000;
const points = 1000;

results.add(await timedRun('In circle', () {
final polygon = List.generate(points, (i) {
final angle = math.pi * 2 / points * i;
return Offset(math.cos(angle), math.sin(angle));
});
final circle = makeCircle(1000, 1, 0);

results.add(await timedRun('In circle', () {
const point = math.Point(0, 0);

bool yesPlease = true;
for (int i = 0; i < N; ++i) {
yesPlease = yesPlease && isPointInPolygon(point, polygon);
yesPlease = yesPlease && isPointInPolygon(point, circle);
}

assert(yesPlease, 'should be in circle');
return yesPlease;
}));

results.add(await timedRun('Not in circle', () {
final polygon = List.generate(points, (i) {
final angle = math.pi * 2 / points * i;
return Offset(math.cos(angle), math.sin(angle));
});

const point = math.Point(4, 4);

bool noSir = false;
for (int i = 0; i < N; ++i) {
noSir = noSir || isPointInPolygon(point, polygon);
noSir = noSir || isPointInPolygon(point, circle);
}

assert(!noSir, 'should not be in circle');
Expand Down
8 changes: 6 additions & 2 deletions lib/src/misc/point_in_polygon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import 'dart:ui';

/// Checks whether point [p] is within the specified closed [polygon]
///
/// Uses the even-odd algorithm.
/// Uses the even-odd algorithm and requires closed loop polygons, i.e.
/// `polygon.first == polygon.last`.
bool isPointInPolygon(math.Point p, List<Offset> polygon) {
final len = polygon.length;
assert(len >= 3, 'not a polygon');
assert(polygon.first == polygon.last, 'polygon not closed');
final double px = p.x.toDouble();
final double py = p.y.toDouble();

bool isInPolygon = false;
for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
for (int i = 0, j = len - 1; i < len; j = i++) {
final double poIx = polygon[i].dx;
final double poIy = polygon[i].dy;

Expand Down
5 changes: 4 additions & 1 deletion test/misc/point_in_polygon_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import 'package:flutter_map/src/misc/point_in_polygon.dart';
import 'package:flutter_test/flutter_test.dart';

List<Offset> makeCircle(int points, double radius, double phase) {
final slice = math.pi * 2 / (points - 1);
return List.generate(points, (i) {
final angle = math.pi * 2 / points * i + phase;
// Note the modulo is only there to deal with floating point imprecision
// and ensure first == last.
final angle = slice * (i % (points - 1)) + phase;
return Offset(radius * math.cos(angle), radius * math.sin(angle));
}, growable: false);
}
Expand Down

0 comments on commit bb3f8be

Please sign in to comment.