从 python 中的原点生成光线

Generate rays from origin in python

我正在尝试生成从原点 (0,0) 向各个方向发出一定长度的光线。我希望这些光线具有一定的长度。光线应以给定分辨率以 360 度方式投射。

from matplotlib.lines import Line2D  
fig = plt.figure()


ax = fig.add_subplot(1,1,1)
a=0
b=10
for i in range(0,100):
    ax.add_line(Line2D([0,a], [0,b], linewidth=1, linestyle = "-", color="green"))
    a+=0.5
    b-=0.5

我尝试了上面给出的一种天真的方法,但我既不能获得 360 度线也不能控制分辨率。

您可以查看 turtle library 它有许多不同的方法供您使用。

以下是实现您想要做的事情的片段:

from turtle import *

# Color of the line segments drawn
color('red')

# Speed at which turtle draws
speed(100)

# Start drawing on the canvas until you come back to the origin
begin_fill()
while True:
    # Length of the line segment
    back(200)
    left(1)
    forward(200)
    print(position())
    if abs(pos()) < 1:
        break

# Finish drawing
end_fill()
done()

我想出了一个受此处给出的答案启发的解决方案:Plotting a line from a coordinate with and angle

import math
import matplotlib.pyplot as plt

# create figure
fig = plt.figure()
ax = plt.subplot(111)

#define origin and length
x, y = (0,0)
length=10

#iterate through possible angles and plot
for angle in range(0, 360, 1):
    # find the end point
    endy = y + length * math.sin(math.radians(angle))
    endx = length * math.cos(math.radians(angle))

    ax.plot([x, endx], [y, endy])
    
plt.show()