Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions pkg/web_app/lib/src/widget/downloads_chart/computations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,40 @@ Iterable<String> prepareRanges(List<VersionRangeCount> rangeDownloads) {

return (ranges: ranges, weekLists: result.reversed.toList());
}

/// Calculates closest point from [point] to line defined by [startPoint] and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this more precise?

Suggested change
/// Calculates closest point from [point] to line defined by [startPoint] and
/// Calculates closest point to [point] on the line segment between[startPoint] and

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the comment a to something more similiar.

/// [endPoint].
///
/// This assumes that the closest point is on the line, that is within the
/// two endpoints. Returns `(double.maxFinite, double.maxFinite)` if this is
/// not the case.
(num, num) closestPointOnLine(
(num, num) startPoint, (num, num) endPoint, (num, num) point) {
final directionVector =
(endPoint.$1 - startPoint.$1, endPoint.$2 - startPoint.$2);

if (directionVector.$1 == 0 && directionVector.$2 == 0) {
return startPoint;
}

final vector = (point.$1 - startPoint.$1, point.$2 - startPoint.$2);

// The dot product ((v · d) / (d · d)) where v = vector and d = directionVector
final t = ((vector.$1 * directionVector.$1 + vector.$2 * directionVector.$2) /
(directionVector.$1 * directionVector.$1 +
directionVector.$2 * directionVector.$2));

if (t < 0 || t > 1) {
// Closest point is before or after the line. This should not happen in
// our use case.
return (double.maxFinite, double.maxFinite);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it should not happen perhaps it is better to throw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not mathematically invalid, just not relevant in our use case. I updated the documentation a bit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't (double.maxFinite, double.maxFinite) the wrong answer in that case?

}

// t * d
final projectionVOntoD = (t * directionVector.$1, t * directionVector.$2);
final closestPoint = (
startPoint.$1 + projectionVOntoD.$1,
startPoint.$2 + projectionVOntoD.$2
);
return (closestPoint.$1, closestPoint.$2);
}
74 changes: 74 additions & 0 deletions pkg/web_app/test/widget/downloads_chart/downloads_chart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,78 @@ void main() {
expect(w3[i], [0, 0, 0, 0, 0, 0]);
}
});

group('closestPointOnLine tests', () {
test('point on the line', () {
final lineStart = (0, 0);
final lineEnd = (10, 10);
final point = (5, 5);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (5.0, 5.0));
});

test('point before the line', () {
final lineStart = (0, 0);
final lineEnd = (10, 10);
final point = (-2, -5);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (double.maxFinite, double.maxFinite));
});

test('point after the line', () {
final lineStart = (0, 0);
final lineEnd = (10, 10);
final point = (15, 15);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (double.maxFinite, double.maxFinite));
});

test('point off the line', () {
final lineStart = (0, 0);
final lineEnd = (10, 10);
final point = (5, 3);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (4.0, 4.0));
});

test('vertical line', () {
final lineStart = (1, 2);
final lineEnd = (1, 10);
final point = (5, 5);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (1.0, 5.0));
});

test('horizontal line', () {
final lineStart = (2, 1);
final lineEnd = (10, 1);
final point = (5, 5);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (5.0, 1.0));
});

test('same start and end points', () {
final lineStart = (5, 5);
final lineEnd = (5, 5);
final point = (10, 10);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (5.0, 5.0));
});

test('line with negative coordinates', () {
final lineStart = (-5, -5);
final lineEnd = (5, 5);
final point = (0, 10);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (5.0, 5.0));
});

test('line with negative and positive coordinates', () {
final lineStart = (-5, 5);
final lineEnd = (5, -5);
final point = (0, 0);
final closest = closestPointOnLine(lineStart, lineEnd, point);
expect(closest, (0.0, 0.0));
});
});
}
Loading