Say i have two points (x,y), (0,0) and (10,10). Now i wan´t to get coordinates along the line by stepping through values of x and y. I thought i solved it with the following functions:

 fy = startY + (x - startX) * ((destY-startY)/(destX-startX));
 fx = (y + startY) / ((destY-startY)/(destX-startX)) + startX;

taken from http://en.wikipedia.org/wiki/Linear_interpolation

However, it seems that im getting a problem when destX and startX is the same value, so you get division by zero.

Is there a better way of getting coordinates along a line when knowing the start and endpoint of the line?


1 Answer 1


represent the distance you travel as a number between 0 and 1. The problem with your code is that you don't have any notion of "how far am I between the two endpoints."

function lerp(start, dest, dist) { 
    var x = start.x * (1 - dist) + dest.x * dist;
    var y = start.y * (1 - dist) + dest.y * dist;
    return [x,y];

lerp(start, dest, 0) -> start
lerp(start, dest, 0.5) -> halfway in between 
lerp(start, dest, 1) -> dest

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .