Skip to content

Commit

Permalink
Update LinearAnimation.cs
Browse files Browse the repository at this point in the history
fix #2 issue
  • Loading branch information
keith-hall committed Apr 23, 2015
1 parent d8bdfe4 commit c7a2ef8
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/LinearAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ public struct Position<T> {
public Point DesiredPosition;
}

public static int GetPositionOneStepCloserToDestination (int currentPos, int targetPos, int speed) {
speed = Math.Abs(speed);
if (currentPos == targetPos) // if it is already at the desired position
return targetPos; // return it
var distance = currentPos - targetPos; // determine the distance between the current position and the desired position
if (Math.Abs(distance) < speed) // if it is less than the speed
return targetPos; // return the desired position
return currentPos + ((currentPos > targetPos) ? -speed : speed); // return the value after the speed (towards the desired position) has been applied
}

private static Position<T> GetNewPosition<T> (T objectToAnimate, Func<T, Point> currentPosition, Point desiredPosition, int speed) {
// NOTE: assumes that speed is a positive number
Func<int, int, int> getNewPos = (f, t) => {
if (f == t) // if it is already at the desired position
return t; // return it
var d = f - t; // determine the distance between the current position and the desired position
if (Math.Abs(d) < speed) // if it is less than the speed
return t; // return the desired position
return f + ((f > t) ? -speed : speed); // return the value after the speed (towards the desired position) has been applied
};
var c = currentPosition(objectToAnimate);
var p = new Position<T> {
ObjectToAnimate = objectToAnimate,
DesiredPosition = desiredPosition,
NewPosition = new Point(getNewPos(c.X, desiredPosition.X), getNewPos(c.Y, desiredPosition.Y))
NewPosition = new Point(GetPositionOneStepCloserToDestination(c.X, desiredPosition.X, speed), GetPositionOneStepCloserToDestination(c.Y, desiredPosition.Y, speed))
};
return p;
}
Expand Down

1 comment on commit c7a2ef8

@keith-hall
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*fix #3 issue

Please sign in to comment.