-
Notifications
You must be signed in to change notification settings - Fork 166
Downloads chart: Add helper function for computing closest point on line #8584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,3 +41,41 @@ Iterable<String> prepareRanges(List<VersionRangeCount> rangeDownloads) { | |
|
|
||
| return (ranges: ranges, weekLists: result.reversed.toList()); | ||
| } | ||
|
|
||
| /// Calculates the closest point on the line segment between [startPoint] | ||
| /// and [endPoint] to a given [point]. | ||
| /// | ||
| /// If [startPoint] and [endPoint] are the same, [startPoint] is returned. | ||
| /// | ||
| /// If [point] is outside the line segment, that is the closest point would not | ||
| /// be within the thwo endpoints, `(double.maxFinite, double.maxFinite)` | ||
| /// is returned. | ||
| (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 v = (point.$1 - startPoint.$1, point.$2 - startPoint.$2); | ||
|
|
||
| // The dot product ((v · d) / (d · d)) | ||
| final t = ((v.$1 * directionVector.$1 + v.$2 * directionVector.$2) / | ||
| (directionVector.$1 * directionVector.$1 + | ||
| directionVector.$2 * directionVector.$2)); | ||
|
|
||
| if (t < 0 || t > 1) { | ||
| // Closest point is before or after the line. | ||
| return (double.maxFinite, double.maxFinite); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it should not happen perhaps it is better to throw?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But isn't |
||
| } | ||
|
|
||
| // 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); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.