python 坐标连线_从具有和角度的坐标绘制线 - python
我基本上想從給定角度的坐標(biāo)(x,y)繪制一條線(計(jì)算切線值)。
用這樣的簡(jiǎn)單代碼行pl.plot([x1, x2], [y1, y2], 'k-', lw=1)可以在兩點(diǎn)之間繪制一條線,但是為此我需要計(jì)算(x2,y2)坐標(biāo)。我的(x1,y1)坐標(biāo)是固定的,并且角度已知。計(jì)算(x2,y2)在某些時(shí)候會(huì)引起問題,所以我只想繪制從(x1,y1)角度(最好是長(zhǎng)度)的直線。
我想到的最簡(jiǎn)單的解決方案是使用點(diǎn)坡函數(shù)y - y1 = m(x - X1)。解釋這些并進(jìn)行一些搜索,我使用了這段代碼:
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是坐標(biāo)。我需要自我解釋,因?yàn)檫@是一個(gè)很糟糕的問題。
所以,現(xiàn)在,關(guān)于我該如何解決的任何想法?
python大神給出的解決方案
我不確定自己從解釋中究竟想要什么,但是我認(rèn)為這將完成您所要求的工作。
如果知道要使用的線的角度和長(zhǎng)度,則應(yīng)使用三角函數(shù)獲取新點(diǎn)。
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()
總結(jié)
以上是生活随笔為你收集整理的python 坐标连线_从具有和角度的坐标绘制线 - python的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机弹钢琴的游戏是什么
- 下一篇: python将元祖写入txt文档_pyt