Как нарисовать линию на изображении с учетом наклона и координат пересечения (x, y), используя Python - PullRequest
0 голосов
/ 12 июля 2020

У меня есть наклон линии, и у меня есть координаты x и y точки пересечения, через которую я хочу пройти через go. По сути, я хочу, чтобы строка отображалась на самом изображении.

По сути, это единственный код, который у меня есть (только переменные):

slope = 3
intercepts = [7, 10]

Я хочу использовать Python для этой задачи. Любая помощь будет принята с благодарностью!

1 Ответ

1 голос
/ 13 июля 2020

Вы можете настроить этот ответ от Томмазо Ди Ното :

import matplotlib.pyplot as plt
import random

x_intercept = 7  
y_intercept = 10 
my_slope = 3 

def find_second_point(slope,x0,y0):
    # this function returns two points 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_x1 = x0 + random.randint(x0,x0+10)  
    new_y1 = (slope*new_x1) + q  
    new_x2 = x0 - random.randint(x0,x0+10)  
    new_y2 = (slope*new_x2) + q 
    return new_x1, new_y1, new_x2, new_y2   


new_x1, new_y1,new_x2, new_y2 = find_second_point(my_slope , x_intercept, y_intercept )

def slope(x1, y1, x2, y2):
    return (y2-y1)/(x2-x1)
print(slope(x_intercept,y_intercept, new_x1, new_y1))


plt.figure(1)  # create new figure
plt.plot((new_x2, new_x1),(new_y2, new_y1), c='r', label='Segment')
plt.scatter(x_intercept, y_intercept, c='b', linewidths=3, label='Intercept')
plt.scatter(new_x1, new_y1, c='g', linewidths=3, label='New Point 1')
plt.scatter(new_x2, new_y2, c='cyan', linewidths=3, label='New Point 2')
plt.legend()  # add legend to image

plt.show()

Вывод:

slope
3.0

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...