简体   繁体   English

如何使用 Python 在给定斜率和截距坐标 (x, y) 的图像上绘制一条线

[英]How to draw a line on an image given the slope and the intercept coordinates (x, y) using Python

I have a slope of a line, and I have the x and y coordinates of the intercept that I want the line to go through.我有一条线的斜率,并且我有我希望这条线通过 go 的截距的 x 和 y 坐标。 Essentially, I want the line to be displayed on the image itself.本质上,我希望线条显示在图像本身上。

The following is basically the only code I have so far (just the variables):以下基本上是我迄今为止唯一的代码(只是变量):

slope = 3
intercepts = [7, 10]

I want to use Python for this task.我想使用 Python 来完成这项任务。 Any help would be greatly appreciated!任何帮助将不胜感激!

You can custom this answer from Tommaso Di Noto :您可以从 Tommaso Di Noto 自定义此答案

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()

Output: Output:

slope
3.0

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用两点的 x 和 y 坐标绘制一条线? - How can draw a line using the x and y coordinates of two points? 如何在python中给定点(x,y)和线与y轴的夹角的情况下找到线的斜率(m)? - How do I find the slope (m) for a line given a point (x,y) on the line and the line's angle from the y axis in python? 用python中的给定斜率找出y轴截距 - figuring out y-intercept with a given slope in python 使用 X 和 y 坐标计算直线方程时的坡度方向错误 - Wrong slope direction while calculating line equation with X and y coordinates Python/Matplotlib:在给定截距和斜率的情况下向图中添加回归线 - Python/Matplotlib: adding regression line to a plot given its intercept and slope 在给定 x 和 y 坐标 pygame 的情况下绘制形状 - Draw shape given x and y coordinates pygame 如何使用python获取x,y坐标? - How to get x,y coordinates using python? Python:Linregress斜率和y轴截距 - Python: Linregress slope and y-intercept 在给定 X 和 Y 坐标的情况下,如何 plot 在 python 中相邻多边形? - How to plot adjoining polygons in python given the coordinates of X and Y? 如何在Python中有效地生成具有随机斜率和截距的直线? - How to efficiently generate a straight line with random slope and intercept in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM