I'm trying to calculate the x and y coordinates that are a set distance between the coordinates of two pixels in an image.

For example, if I travel from my original location (x1=4, y1=3) to a new location (x2=4, y2=11), it is pretty straight-forward to calculate that these points are 8 pixels apart:

import math
x_dist = (x2 - x1)
y_dist = (y2 - y1)
distance = math.sqrt(x_dist * x_dist + y_dist * y_dist)

However, I'm having difficulty working out the coordinate that is 2 pixels along this path. I know that in this case the answer should be x=4, y=5, however I can't seem to work out how to do this. Most of the equations I've looked at on interpolation require you to know x or the y coordinate you're searching for, and finding the midpoint coordinate doesn't help much either.

EDIT Thank-you Arpan for your amazing help, this is how I've implemented your suggestion in Python. I've also calculated the midpoint to check that the answer was corect:

import math

def calc_distance(x1, y1, x2, y2):
    x_dist = (x2 - x1)
    y_dist = (y2 - y1)
    return math.sqrt(x_dist * x_dist + y_dist * y_dist)

x1, y1, x2, y2 = 0, 3, 2, 11

distance = calc_distance(x1,y1,x2,y2)
first_point_dist = distance / 2

mid_x, mid_y = (x1 + x2) / 2, (y1 + y2) / 2

x_diff = x2 - x1
y_diff = y2 - y1
if x_diff == 0:
    new_x = x1
    if y_diff > 0:
        new_y =  y1 + dist_to_point
        new_y =  y1 - dist_to_point
elif y_diff == 0:
    new_y = y1
    if x_diff > 0:
        new_x = x1 + dist_to_point
        new_x = x1 - dist_to_point
    tan_angle = y_diff / x_diff
    angle = math.atan(tan_angle)
    new_x = round(x1 + first_point_dist * math.cos(angle))
    new_y = round(y1 + first_point_dist * math.sin(angle))

print(new_x, new_y, mid_x, mid_y, distance)

Final Edit I just realised that the angle had to be in radians, NOT degrees, I was converting it to degrees but ending up with strange answers, that has been modified now and seems to be working well. Thanks for all the help!

Final Final Edit Forgot to check if the points on the same x or y axis were positive or negative.

  • $\begingroup$ It would be better to calculate the square of the distance. That way, you wouldn't have to invoke the (computationally expensive) routine "sqrt". $\endgroup$ – John Joy Apr 21 '15 at 12:50
  • $\begingroup$ I tried that with one of my other programs written in C++ and didn't notice that much of a difference in speed, but then I think the main bottleneck in the video was reading in and creating image matrices from 4K video. It might be worth trying again to see if I get a bit of a speed up. $\endgroup$ – Jack Simpson Apr 22 '15 at 1:16

I think what you're looking for is the parametric form.

Given your initial point $(x_0,y_0)$ and another point of the line $(x_1,y_1)$, you can write the slope of line as $$\tan\theta=\frac{y_1-y_0}{x_1-x_0}$$

For a point $(x,y)$ at a distance $r$ along the line from $(x_0,y_0)$, $$x=x_0+r\cos\theta,y=y_0+r\sin\theta$$ Where $\tan\theta$ is the slope as above.

  • $\begingroup$ Will it cause problems if the tanθ equals a division by zero? $\endgroup$ – Jack Simpson Apr 21 '15 at 7:58
  • 1
    $\begingroup$ Yes but then the meaning of the two points having the same x coords makes the distance easy $\endgroup$ – danimal Apr 21 '15 at 8:59
  • $\begingroup$ Good point, thank-you so much for the help! $\endgroup$ – Jack Simpson Apr 21 '15 at 15:24
  • $\begingroup$ @Arpan maybe you can help me with this question: math.stackexchange.com/questions/1548841/… $\endgroup$ – John Smith Nov 27 '15 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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