Search code examples
javafxcoordinatesformulapoint

How can i get x or y between two points?


My question is about points is javafx:

I have two points for example:

Point p1 = new Point(2,2);
Point p2 = new Point(10,8);

And i draw a imaginary line between them, i wanna know if i say x = 8 what y is on the imaginary line or otherwise is i know y, what x is on the imaginary line.

I found a formula

x= x1 + blend * (x2 - x1), 

and blend is the percentage on the line. But i wanna have this kind of formula where i can give a Y.

It there some kind of formula to solve this problem?

Thanks in advance!


Solution

  • Since your imaginary line is an equation in the format y = ax+b, you can solve it like this http://www.wikihow.com/Calculate-Slope-and-Intercepts-of-a-Line

    given
    
    10,8
    
    and
    
    2,2
    
    y = ax + b
    
    so
    
    8 = 10a + b
    -
    2 =  2a + b
    ------------
    6 =  8a
    
    a = 6/8 = 0.75
    
    and
    
    8 = 10a + b
    8 = 10*(0.75) + b
    8 = 7.5 + b
    8 - 7.5 = b
    0.5 = b
    
    so
    
    y = 0.75*x + 0.5
    

    enter image description here