# How to get xy coordinates along a given path

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;
``````

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?

-

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
``````
-
great, ill try this out! Thank you. – netbrain Jul 9 '12 at 19:43