Calculating y-axis values between two points

bacteriaman

Member
I have a tricky math problem that has me stumped.

I need to gradually draw a line one pixel at a time between two points. I know the start and end X/Y coordinate values and the current incremented X value. Using these values, I need to calculate each y-axis to gradually draw a line from the start X/Y to the current X until reaching end X/Y.

For example, suppose we have a 100x100 grid with a start X/Y of 0/0 and end X/Y of 100/100. I also have an incremented X counter that progresses from left to right. That said, the line will not be perfectly straight or diagonal so the y-axis will not be predictable.

The following link illustrates the above:

www.mathportal.org/calculators/analytic-geometry/two-point-form-calculator.php?val1=0&val2=0&val3=100&val4=100&rb1=slop&ch1=expl

The page includes a formula, but I am uncertain how to implement it to calculate the Y dynamically.

Or perhaps there's a different or better approach. Any comments/suggestions is much appreciated.
 
Last edited:

FrostyCat

Member
That link is dead at the moment, but if I guess what you mean correctly, the effect you asked for is trivial for anyone who understands interpolation in general. Here's an easy example with linear interpolation:

Create:
Code:
progress = 0;
progress_rate = 1/(room_speed*4);
x1 = 0;
y1 = 0;
x2 = 100;
y2 = 100;
Step:
Code:
if (progress < 1) {
  progress = min(progress+progress_rate, 1);
}
Draw:
Code:
draw_set_colour(c_black);
draw_line(x1, y1, lerp(x1, x2, progress), lerp(y1, y2, progress));
There is a reason why most people who formally study computer graphics read up on Robert Penner's equations. They can easily achieve effects that amateurs who didn't read up could blunder for months trying to emulate. Anyone who can't use them at will have no real right calling themselves part of the trade.
 
Last edited:

bacteriaman

Member
@FrostyCat, your post is very informative--thank you.

That said, I had to smile and laugh to myself while reading your last sentence. That's like saying a musician who isn't classically trained or doesn’t understand music theory shouldn't bother making music. Paul McCartney can't read or write music, but he's still managed to produce a memorable song or two.
 
Last edited:
Top