我基本上想从给定角度的坐标(x,y)绘制一条线(计算切线值)。
用这样的简单代码行pl.plot([x1, x2], [y1, y2], 'k-', lw=1)
可以在两点之间绘制一条线,但是为此我需要计算(x2,y2)坐标。我的(x1,y1)坐标是固定的,并且角度已知。计算(x2,y2)在某些时候会引起问题,所以我只想绘制从(x1,y1)角度(最好是长度)的直线。
我想到的最简单的解决方案是使用点坡函数y - y1 = m(x - X1)
。解释这些并进行一些搜索,我使用了这段代码:
x1 = 10
y1 = -50
angle = 30
sl = tan(radians(angle))
x = np.array(range(-10,10))
y = sl*(x-x1) + y1
pl.plot(x,y)
pl.show
sl是坡度,x1和y1是坐标。我需要自我解释,因为这是一个很糟糕的问题。
所以,现在,关于我该如何解决的任何想法?
python大神给出的解决方案
我不确定自己从解释中究竟想要什么,但是我认为这将完成您所要求的工作。
如果知道要使用的线的角度和长度,则应使用三角函数获取新点。
import numpy as np
import math
import matplotlib.pyplot as plt
def plot_point(point, angle, length):
'''
point - Tuple (x, y)
angle - Angle you want your end point at in degrees.
length - Length of the line you want to plot.
Will plot the line on a 10 x 10 plot.
'''
# unpack the first point
x, y = point
# find the end point
endy = length * math.sin(math.radians(angle))
endx = length * math.cos(math.radians(angle))
# plot the points
fig = plt.figure()
ax = plt.subplot(111)
ax.set_ylim([0, 10]) # set the bounds to be 10, 10
ax.set_xlim([0, 10])
ax.plot([x, endx], [y, endy])
fig.show()