-
Notifications
You must be signed in to change notification settings - Fork 0
fix bug and modernize #1
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
Open
calvinmetcalf
wants to merge
3
commits into
master
Choose a base branch
from
bug-modernize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,41 @@ | ||
| 'use strict'; | ||
| var utils = require('./utils'); | ||
|
|
||
| function midPoint (line) { | ||
| var len = line.length; | ||
| var start = 0; | ||
| var end = len - 1; | ||
| var diff = 0; | ||
| import {getLength, findMidpoint} from './utils.js' | ||
| const midPoint = (line) => { | ||
| let start = 0; | ||
| let end = line.length - 1; | ||
| let diff = 0; | ||
| let lastStart = 0; | ||
| let lastEnd = 0; | ||
| while (start + 1 !== end) { | ||
| if (diff <= 0) { | ||
| diff += utils.getLength(line[start], line[++start]); | ||
| lastStart = getLength(line[start], line[++start]); | ||
| diff += lastStart; | ||
| } else { | ||
| diff -= utils.getLength(line[end], line[--end]); | ||
| lastEnd = getLength(line[end], line[--end]); | ||
| diff -= lastEnd; | ||
| } | ||
| } | ||
|
|
||
| var hyp = utils.getLength(line[start], line[end]); | ||
| return utils.diffThingy(line[start], line[end], hyp, diff); | ||
| let hyp = getLength(line[start], line[end]); | ||
|
|
||
| if (Math.abs(diff) > hyp) { | ||
| // our last segment is smaller than the current difference; | ||
|
|
||
| // to correct this we | ||
| // 1. undo the last one that was too big | ||
| // 2. force it to do the segment at the other end that was previously the midpoint | ||
| // 3. recalculate the distance between the new last two points | ||
| if (diff > 0) { | ||
| diff -= lastStart; // 1. | ||
| start--;// 1. | ||
| diff -= getLength(line[end], line[--end]); // 2. | ||
| } else { | ||
| diff += lastEnd;// 1. | ||
| end++;// 1. | ||
| diff += getLength(line[start], line[++start]); // 2. | ||
| } | ||
| hyp = getLength(line[start], line[end]); // 3. | ||
| } | ||
| return findMidpoint(line[start], line[end], hyp, diff); | ||
| } | ||
| module.exports = midPoint; | ||
| export default midPoint; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,53 @@ | ||
| 'use strict'; | ||
|
|
||
| var test = require('tape'); | ||
| var utils = require('./utils'); | ||
| var midPoint = require('./index'); | ||
| import test from 'tape'; | ||
| import {getLength} from './utils.js' | ||
| import midPoint from './index.js' | ||
|
|
||
| test('lengths', function (t) { | ||
| test('lengths', (t) => { | ||
| var a = [0, 0]; | ||
| var b = [1, 1]; | ||
| var c = [-1, -1]; | ||
| var root2 = Math.sqrt(2); | ||
| var root8 = Math.sqrt(8); | ||
| t.plan(6); | ||
| t.equals(utils.getLength(a, b), root2); | ||
| t.equals(utils.getLength(a, c), root2); | ||
| t.equals(utils.getLength(b, c), root8); | ||
| t.equals(utils.getLength(b, a), root2); | ||
| t.equals(utils.getLength(c, a), root2); | ||
| t.equals(utils.getLength(c, b), root8); | ||
| t.equals(getLength(a, b), root2); | ||
| t.equals(getLength(a, c), root2); | ||
| t.equals(getLength(b, c), root8); | ||
| t.equals(getLength(b, a), root2); | ||
| t.equals(getLength(c, a), root2); | ||
| t.equals(getLength(c, b), root8); | ||
| }); | ||
| test('mid points', function (t) { | ||
| t.deepEquals(midPoint([[0,0], [1,1], [2,2], [3,3]]), [1.5, 1.5], 'correct mid point'); | ||
| t.deepEquals(midPoint([[0,0], [1,1], [3,3]]), [1.5, 1.5], 'correct mid point'); | ||
| t.deepEquals(midPoint([[0,0], [1,1],[2,2]]), [1, 1], 'correct mid point'); | ||
| t.deepEquals(midPoint([[0,0], [1,1], [2,2], [3,3]]), [1.5, 1.5], 'correct mid point 1'); | ||
| t.deepEquals(midPoint([[0,0], [1,1], [3,3]]), [1.5, 1.5], 'correct mid point, 2'); | ||
| t.deepEquals(midPoint([[0,0], [1,1],[2,2]]), [1, 1], 'correct mid point, 3'); | ||
| t.deepEquals(midPoint([[0,0], [1, 0],[1,1],[7,1]]), [3, 1], 'correct mid point, 4'); | ||
| t.deepEquals(midPoint([[7,1], [1,1],[1,0],[0,0]]), [3, 1], 'correct mid point, 5'); | ||
|
|
||
| t.end(); | ||
| }); | ||
|
|
||
| test('weird', (t) => { | ||
| const path = [ | ||
| [ | ||
| -93.58900674667849, | ||
| 34.72896579327973 | ||
| ], | ||
| [ | ||
| -93.52014326672906, | ||
| 34.88244373300708 | ||
| ], | ||
| [ | ||
| -93.31354710277073, | ||
| 34.9711667901214 | ||
| ], | ||
| [ | ||
| -89.36857824131185, | ||
| 35.22873356176132 | ||
| ] | ||
| ] | ||
| t.deepEquals(midPoint(path), [-91.53717547300896, 35.08714598336179], 'correct forward') | ||
| t.deepEquals(midPoint(path.toReversed()), [-91.53717547300896, 35.08714598336179 ], 'correct backwards') | ||
| t.end(); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,22 @@ | ||
| 'use strict'; | ||
|
|
||
| function getLength (p1, p2) { | ||
| var dx = p2[0] - p1[0]; | ||
| var dy = p2[1] - p1[1]; | ||
| export const getLength = (p1, p2) => { | ||
| const dx = p2[0] - p1[0]; | ||
| const dy = p2[1] - p1[1]; | ||
| return Math.sqrt(dx * dx + dy * dy); | ||
| } | ||
| exports.getLength = getLength; | ||
|
|
||
| function getDiff (p1, p2) { | ||
| var dx = p2[0] - p1[0]; | ||
| var dy = p2[1] - p1[1]; | ||
| const getDiff = (p1, p2) => { | ||
| const dx = p2[0] - p1[0]; | ||
| const dy = p2[1] - p1[1]; | ||
| return [dx, dy]; | ||
| } | ||
|
|
||
| function diffThingy(p1, p2, hyp, diff) { | ||
| var delta = getDiff(p1, p2); | ||
| var ratio = ((hyp - diff) / 2) / hyp; | ||
| return p1.map(function (p, i) { | ||
| return p + delta[i] * ratio; | ||
| }); | ||
| export const findMidpoint = (p1, p2, hyp, diff) => { | ||
| const delta = getDiff(p1, p2); | ||
| const ratio = ((hyp - diff) / 2) / hyp; | ||
| return [ | ||
| p1[0] + delta[0] * ratio, | ||
| p1[1] + delta[1] * ratio | ||
| ] | ||
| } | ||
| exports.diffThingy = diffThingy; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.