-
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 1 commit
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,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 | ||
| /// [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 = | ||
szakarias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (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); | ||
szakarias marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // 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); | ||
|
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); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this more precise?
There was a problem hiding this comment.
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.