Нахождение точек на строке с данным расстоянием

Ключевое слово константы на функции членства отмечает этот параметр как постоянный. Функция может все еще отключить звук глобальных данных (так не может кэшироваться), но не данные объектов (обеспечение запросов к объектам константы).

18
задан A. Levy 20 August 2009 в 04:40
поделиться

2 ответа

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.

15
ответ дан 30 November 2019 в 09:11
поделиться

Let's call the point you are trying to find P, with coordinates px, py, and your starting point A's coordinates ax and ay. Slope m is just the ratio of the change in Y over the change in X, so if your point P is distance s from A, then its coordinates are px = ax + s, and py = ay + m * s. Now using Pythagoras, the distance d from A to P will be d = sqrt(s * s + (m * s) * (m * s)). To make P be a specific D units away from A, find s as s = D/sqrt(1 + m * m).

0
ответ дан 30 November 2019 в 09:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: