Skip to content

Commit 5d768cb

Browse files
committed
Explicit edge check
1 parent 49e876c commit 5d768cb

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

pkg/web_app/lib/src/widget/downloads_chart/computations.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ bool isPointOnPathWithTolerance(
132132
/// a polygon defined by a list of vertices. The polygon is assumed to be
133133
/// closed and non-self-intersecting.
134134
///
135-
/// Returns `true` if the point is inside the polygon or exactly on a vertex and
136-
/// `false` otherwise.
135+
/// Returns `true` if the point is inside the polygon or exactly on a vertex or
136+
/// on edge, and `false` otherwise.
137137
bool isPointInPolygon(List<(double, double)> polygon, (double, double) point) {
138138
if (polygon.length < 3) {
139139
return false;
@@ -142,6 +142,11 @@ bool isPointInPolygon(List<(double, double)> polygon, (double, double) point) {
142142
int intersections = 0;
143143
final (px, py) = point;
144144

145+
// Check if the point is on an edge
146+
if (isPointOnPathWithTolerance(polygon, point, 0.001)) {
147+
return true;
148+
}
149+
145150
for (int i = 0; i < polygon.length; i++) {
146151
final (x1, y1) = polygon[i];
147152
final (x2, y2) = polygon[(i + 1) % polygon.length];

pkg/web_app/test/widget/downloads_chart/downloads_chart_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,14 @@ void main() {
212212
final outsidePoint = (2.0, 3.0);
213213
final vertexPoint = (1.0, 1.0);
214214
final edgePoint = (1.0, 3.0);
215+
final edgePoint2 = (3.0, 3.0);
215216
final outsidePointOnEdgeExtension = (3.0, 4.0);
216217

217218
expect(isPointInPolygon(complexPolygon, insidePoint), isTrue);
218219
expect(isPointInPolygon(complexPolygon, outsidePoint), isFalse);
219220
expect(isPointInPolygon(complexPolygon, vertexPoint), isTrue);
220-
expect(isPointInPolygon(complexPolygon, edgePoint), isFalse);
221+
expect(isPointInPolygon(complexPolygon, edgePoint), isTrue);
222+
expect(isPointInPolygon(complexPolygon, edgePoint2), isTrue);
221223
expect(isPointInPolygon(complexPolygon, outsidePointOnEdgeExtension),
222224
isFalse);
223225
});

0 commit comments

Comments
 (0)