Skip to content

Commit

Permalink
Merge pull request #1633 from leoncvlt/dev
Browse files Browse the repository at this point in the history
Add FlxMath.remapToRange()
  • Loading branch information
Gama11 committed Oct 18, 2015
2 parents 898376c + cd409a5 commit d0d1122
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions flixel/math/FlxMath.hx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ class FlxMath

return output;
}

/**
* Re-maps a number from one range to another.
*
* @param value The incoming value to be converted
* @param start1 Lower bound of the value's current range
* @param stop1 Upper bound of the value's current range
* @param start2 Lower bound of the value's target range
* @param stop2 Upper bound of the value's target range
* @return The wrapped value
*/
public static function remapToRange(value:Float, start1:Float, stop1:Float, start2:Float, stop2:Float)
{
return start2 + (value - start1) * ((stop2 - start2) / (stop1 - start1));
}

/**
* Finds the dot product value of two vectors
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/src/flixel/math/FlxMathTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ class FlxMathTest extends FlxTest
Assert.isTrue(FlxMath.sameSign(-5, -30));
Assert.isFalse(FlxMath.sameSign(-5, 1));
}

@Test
function testRemapToRange()
{
Assert.areEqual(35, FlxMath.remapToRange(5,0,10,25,45));
Assert.areEqual(23, FlxMath.remapToRange(-1,0,10,25,45));
Assert.areEqual(-2, FlxMath.remapToRange(2,10,1,-10,-1));
}

@Test
function testFastTrig()
Expand Down

0 comments on commit d0d1122

Please sign in to comment.