从一个坐标和一个角度绘制一条直线
我基本上想绘制一个坐标(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是坐标。我需要解释自己,因为这被认为是一个很糟糕的问题。
那么,现在,我怎么可以做/解决任何想法?
我不确定你想从解释中得到什么,但我认为这会做出接近你要求的东西。
如果您知道要使用的线的角度和长度,您应该使用三角函数来获取新点。
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()
您忘了将起始坐标添加到末尾:'endy = y + ...' – hitzg 2015-02-10 21:40:43
这就是我目前在我的代码中执行的操作。考虑到斜率,我计算了另一个坐标和两点之间的图,就像我在我的问题中给出的一样,与您提供的一样。 但我不想在我的代码中包含这些计算。这就是为什么我想知道是否有一个我可以使用的图书馆,我可以用一个坐标,角度和长度绘制一条线。 – 2015-02-17 23:06:41
这是更多的几何问题,尝试使用Google搜索它。 – Blue 2015-02-09 19:59:14
是的,做到了!否则我不会发布它。我想到的最简单的解决方案是使用斜率和已知坐标的线的方程。在这种情况下,我决定这样做: 'X1 = 10 Y1 = -50 角= 30 SL = TAN(弧度(角度)) X = np.array(范围(-10,10) ) Y = SL *(X-X1)+ Y1 pl.plot(X,Y) pl.show' 这里y是线和SL的方程是直线的斜率。但是,这似乎并不奏效。 – 2015-02-10 16:18:56