Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Updating PointsHelper to return indices instead of values.
Browse files Browse the repository at this point in the history
  • Loading branch information
MattiSG committed May 20, 2011
1 parent e9459b7 commit a1504a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
25 changes: 17 additions & 8 deletions src/PointsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var PointsHelper = {
/**
*@param Point from the point from which the highest point is to be seen
*@param Point[] candidates the *ordered* set of points in which to look for the highest point
*@returns the index of the highest point in the candidates set
*/
highestPointFromIn: function getHighest(from, candidates) {
return this.genericPointFromIn(from,
Expand All @@ -14,6 +15,7 @@ var PointsHelper = {
/**
*@param Point from the point from which the lowest point is to be seen
*@param Point[] candidates the *ordered* set of points in which to look for the lowest point
*@returns the index of the lowest point in the candidates set
*/
lowestPointFromIn: function getLowest(from, candidates) {
return this.genericPointFromIn(from,
Expand All @@ -33,17 +35,24 @@ var PointsHelper = {
*@private
*/
genericPointFromIn: function getGenericSuperlative(from, candidates, shouldReplace) {
var result = candidates[0];
var resultVect = new Vector(from, result);
var result,
best,
bestVect;

candidates.each(function(candidate) {
function setBestTo(index) {
best = candidates[index];
bestVect = new Vector(from, best);
result = index;
}

setBestTo(0);

candidates.each(function(candidate, index) {
var vect = new Vector(from, candidate);
if (shouldReplace(resultVect, vect)) {
result = candidate;
resultVect = new Vector(from, result);
}
if (shouldReplace(bestVect, vect))
setBestTo(index);
});

return result;
return result;
}
}
14 changes: 7 additions & 7 deletions test/PointsHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ describe('PointsHelper', {
},

"Highest point calculation": function() {
value_of(PointsHelper.highestPointFromIn(origin, sortedPoints)).should_be(highestRelativeToOrigin);
value_of(PointsHelper.highestPointFromIn(origin, sortedPoints)).should_be(sortedPoints.indexOf(highestRelativeToOrigin));

value_of(PointsHelper.highestPointFromIn(
{x: 0, y: 20},
[
{x: 1, y: 10},
{x: 1, y: 20},
{x: 1, y: 30},
{x: 1, y: 30}, // expected winner, index 2
{x: 1, y: 5}
])
).should_be({x: 1, y: 30});
).should_be(2);
},


Expand All @@ -41,19 +41,19 @@ describe('PointsHelper', {
{x: 1, y: 10},
{x: 1, y: 20},
{x: 1, y: 30},
{x: 1, y: 5}
{x: 1, y: 5} // expected winner, index 3
])
).should_be({x: 1, y: 5});
).should_be(3);

value_of(PointsHelper.lowestPointFromIn(
{x: 0, y: 20},
[
{x: 1, y: 10},
{x: 1, y: 10}, // expected winner, index 0
{x: 1, y: 20},
{x: 1, y: 30},
{x: 100, y: 5}
])
).should_be({x: 1, y: 10});
).should_be(0);
}
});

Expand Down

0 comments on commit a1504a8

Please sign in to comment.