1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Calculating y-axis values between two points

Discussion in 'Advanced Programming Discussion' started by bacteriaman, May 2, 2019.

  1. bacteriaman

    bacteriaman Member

    Joined:
    Jun 20, 2016
    Posts:
    23
    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: May 2, 2019
  2. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,612
    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: May 2, 2019
    Sabnock likes this.
  3. bacteriaman

    bacteriaman Member

    Joined:
    Jun 20, 2016
    Posts:
    23
    @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: May 3, 2019
    Sabnock likes this.
  4. vdweller

    vdweller Member

    Joined:
    Jun 24, 2016
    Posts:
    135
    Martin Linklater likes this.
  5. bacteriaman

    bacteriaman Member

    Joined:
    Jun 20, 2016
    Posts:
    23
    Awe, yes, this is great. I actually played with the slope formula, but didn't know how to apply it. Thank you!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice