Вот возможный обходной путь, к которому я пришел: предположим, мои координаты перехвата сохранены как x_intercept
и y_intercept
, а наклон ( m ) сохранен как my_slope
который был найден по известному уравнению m = (y2-y1) / (x2-x1) , или каким-либо другим способом вам удалось его найти.
Используя другое известное общее уравнение для линии y = mx + q , я определяю функцию find_second_point
, которая сначала вычисляет q (поскольку известны m , x и y ), а затем вычисляется еще одна случайная точка, принадлежащая этой линии.
Как только у меня появятся две точки (начальная x_intercept
, y_intercept
и недавно найденная new_x
, new_y
), я просто нанесу отрезок на эти две точки. Вот код:
import numpy as np
import matplotlib.pyplot as plt
x_intercept = 3 # invented x coordinate
y_intercept = 2 # invented y coordinate
my_slope = 1 # invented slope value
def find_second_point(slope,x0,y0):
# this function returns a point which belongs to the line that has the slope
# inserted by the user and that intercepts the point (x0,y0) inserted by the user
q = y0 - (slope*x0) # calculate q
new_x = x0 + 10 # generate random x adding 10 to the intersect x coordinate
new_y = (slope*new_x) + q # calculate new y corresponding to random new_x created
return new_x, new_y # return x and y of new point that belongs to the line
# invoke function to calculate the new point
new_x, new_y = find_second_point(my_slope , x_intercept, y_intercept )
plt.figure(1) # create new figure
plt.plot((x_intercept, new_x),(y_intercept, new_y), c='r', label='Segment')
plt.scatter(x_intercept, y_intercept, c='b', linewidths=3, label='Intercept')
plt.scatter(new_x, new_y, c='g', linewidths=3, label='New Point')
plt.legend() # add legend to image
plt.show()
вот изображение, сгенерированное кодом:
![result image](https://i.stack.imgur.com/rFJDw.png)