Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.