从与的坐标系绘制直线

2024-06-12 23:45:03 发布

您现在位置: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

这里是slope,x1和y1是坐标。我需要解释一下,因为这是一个糟糕的问题。

那么,现在,我有什么办法可以做到/解决这个问题吗?


Tags: 代码plot绘制解决方案直线角度plx1
1条回答
网友
1楼 · 发布于 2024-06-12 23:45:03

我不太确定你到底想从解释中得到什么,但我认为这会做一些接近你要求的事情。

如果知道要使用的直线的角度和长度,则应使用三角法来获取新点。

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

相关问题 更多 >