Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding points on a line with a given distance

I have a question i know a line i just know its slope(m) and a point on it A(x,y) How can i calculate the points(actually two of them) on this line with a distance(d) from point A ??? I m asking this for finding intensity of pixels on a line that pass through A(x,y) with a distance .Distance in this case will be number of pixels.

like image 873
Emre Avatar asked Aug 09 '09 02:08

Emre


People also ask

What is the formula for finding distance with points?

Learn how to find the distance between two points by using the distance formula, which is an application of the Pythagorean theorem. We can rewrite the Pythagorean theorem as d=√((x_2-x_1)²+(y_2-y_1)²) to find the distance between any two points.


3 Answers

I would suggest converting the line to a parametric format instead of point-slope. That is, a parametric function for the line returns points along that line for the value of some parameter t. You can represent the line as a reference point, and a vector representing the direction of the line going through that point. That way, you just travel d units forward and backward from point A to get your other points.

Since your line has slope m, its direction vector is <1, m>. Since it moves m pixels in y for every 1 pixel in x. You want to normalize that direction vector to be unit length so you divide by the magnitude of the vector.

    magnitude = (1^2 + m^2)^(1/2)

    N = <1, m> / magnitude = <1 / magnitude, m / magnitude>

The normalized direction vector is N. Now you are almost done. You just need to write the equation for your line in parameterized format:

    f(t) = A + t*N

This uses vector math. Specifically, scalar vector multiplication (of your parameter t and the vector N) and vector addition (of A and t*N). The result of the function f is a point along the line. The 2 points you are looking for are f(d) and f(-d). Implement that in the language of your choosing.

The advantage to using this method, as opposed to all the other answers so far, is that you can easily extend this method to support a line with "infinite" slope. That is, a vertical line like x = 3. You don't really need the slope, all you need is the normalized direction vector. For a vertical line, it is <0, 1>. This is why graphics operations often use vector math, because the calculations are more straight-forward and less prone to singularities. It may seem a little complicated at first, but once you get the hang of vector operations, a lot of computer graphics tasks get a lot easier.

like image 115
A. Levy Avatar answered Nov 01 '22 08:11

A. Levy


Let me explain the answer in a simple way.

Start point - (x0, y0)

End point - (x1, y1)

We need to find a point (xt, yt) at a distance dt from start point towards end point.

Point on a line at a distance

The distance between Start and End point is given by d = sqrt((x1 - x0)^2 + (y1 - y0)^2)

Let the ratio of distances, t = dt / d

Then the point (xt, yt) = (((1 - t) * x0 + t * x1), ((1 - t) * y0 + t * y1))

When 0 < t < 1, the point is on the line.

When t < 0, the point is outside the line near to (x0, y0).

When t > 1, the point is outside the line near to (x1, y1).

like image 25
Sen Jacob Avatar answered Nov 01 '22 09:11

Sen Jacob


Here's a Python implementation to find a point on a line segment at a given distance from the initial point:

import numpy as np

def get_point_on_vector(initial_pt, terminal_pt, distance):
    v = np.array(initial_pt, dtype=float)
    u = np.array(terminal_pt, dtype=float)
    n = v - u
    n /= np.linalg.norm(n, 2)
    point = v - distance * n

    return tuple(point)

Based on the excellent answer from @Theophile here on math stackexchange.

like image 1
HumbleBee Avatar answered Nov 01 '22 08:11

HumbleBee