python 坐标连线_从具有和角度的坐标绘制线 - python
我基本上想從給定角度的坐標(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()
總結
以上是生活随笔為你收集整理的python 坐标连线_从具有和角度的坐标绘制线 - python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机弹钢琴的游戏是什么
- 下一篇: 刺激战场怎么创造房间