【问题标题】:Plotting a line from a coordinate with and angle从坐标和角度绘制一条线
【发布时间】:2021-11-01 03:01:27
【问题描述】:

我基本上想从坐标 (x, y) 以给定角度绘制一条线(计算切线值)。

使用像pl.plot([x1, x2], [y1, y2], 'k-', lw=1) 这样的简单代码行,我可以在两点之间绘制一条线,但为此我需要计算 (x2, y2) 坐标。我的 (x1, y1) 坐标是固定的,角度是已知的。计算 (x2, y2) 在某些时候会导致问题,所以我只想从 (x1, y1) 绘制具有角度(最好是长度)的线。

我想出的最简单的解决方案是使用点斜率函数y - y1 = m(x - X1)。解释this并搜索一下我使用了这段代码:

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 是坐标。我需要解释一下自己,因为这是一个糟糕的问题。

那么现在,关于我如何做/解决这个问题的任何想法?

【问题讨论】:

  • 这更像是一个几何问题,试试谷歌搜索吧。
  • 是的,做到了!否则我不会发布它。我想到的最简单的解决方案是使用斜率和已知坐标的直线方程。在那种情况下,我决定这样做: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 这里 y 是直线方程,sl 是直线的斜率。但是,这似乎不起作用。

标签: python matplotlib plot


【解决方案1】:

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

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

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 = y + 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 + ...
  • 这就是我目前在我的代码中所做的。给定斜率,我计算两个点之间的另一个坐标和绘图,正如我在问题中给出的那样,与你提供的一样。但我不想在我的代码中包含这些计算。这就是为什么我想知道是否有一个库可以使用,我可以从中绘制一条具有一个坐标、角度和长度的线。
  • @H.AzizKayıhan 当你可以自己创建一个简单的def 时,你为什么想要/需要一个库?
【解决方案2】:

this website 的启发,给定 WGS84 坐标、方位角(有时称为前向方位角)和距离,您可以使用以下逻辑计算结果目标点:

import math 

distance = 100 # kilometres
radius = 6371 # earth's radius in kilometres

lon, lat = -7.83197, 37.040893
bearing = 40

δ = distance / radius
θ = math.radians(bearing)

φ1 = math.radians(lat)
λ1 = math.radians(lon)

sinφ2 = math.sin(φ1) * math.cos(δ) + math.cos(φ1) * math.sin(δ) * math.cos(θ)
φ2 = math.asin(sinφ2)
y = math.sin(θ) * math.sin(δ) * math.cos(φ1)
x = math.cos(δ) - math.sin(φ1) * sinφ2
λ2 = λ1 + math.atan2(y, x)

lat2 = math.degrees(φ2)
lon2 = math.degrees(λ2)

这将产生

>>> lon2, lat2
(-7.831861171142511, 37.04091627610624)

【讨论】:

    【解决方案3】:

    复数的标准模块 cmath 让它变得简单。

       import cmath
    
       pt = cmath.rect(r, angle)  
       x = pt.real  
       y = pt.imag
    

    给定直线的长度(或半径)r,以及以弧度为单位的角度,我们可以得到从原点开始的直线的终点 x 和 y 坐标 (x, y) (0, 0)。

    • 不从原点开始:如果线从任何其他点 (x1, y1) 开始,只需添加到 (x2, y2) 为 x2 = x1 + x 和 y2 = y1 + y

    • 度数到弧度:如果角度可用度数,使用 math.radians(deg) 得到相同的弧度。当然,记得在使用前导入数学。

    cmath.rect(r, phi) 是您将调用的函数。它返回一个复数!只需将其实部和虚部作为您需要的 x 和 y 值。

    【讨论】:

      【解决方案4】:

      你想要的是一种新的东西,叫做 axline

      import numpy as np
      import matplotlib.pyplot as plt
      
      x1 = 10
      y1 = -50
      angle = 30
      
      sl = np.tan(np.radians(angle))
      
      x = np.arange(-10,10)
      y = sl*(x-x1) + y1
      
      plt.plot(x,y, 'o', label='manual')
      
      plt.axline((x1,y1), slope=sl, color='red', label='axline')
      
      plt.legend()
      plt.grid()
      plt.show()
      

      【讨论】:

      • 没有名为 numpy 的模块?
      • ?您运行此代码并收到该消息? “”没有名为 numpy 的模块“”?如果是这种情况,您应该安装模块 numpy 和(可能)matplotlib。一个简单的“pip install numpy matplotlib”应该可以工作。但我建议你看看 Anaconda(Python 发行版)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多