这给了我一个填充的区域:
Sector(x=200, y=200, radius=200, segments=200, angle=22.5, start_angle=0)
这只给我直线/圆弧 - 没有到中心的尺寸为 r 的直线:
Arc(px, py, radius, 50, tau, 0)
PS:我注意到,Python 3.10 的
pip install pyglet=2.0.10
在我的 Windows 10 机器上给了我一个“损坏”的包。 Arc
根本画不出来。从源代码安装 pyglet 解决了这个问题。
您可以定义自己的函数来绘制圆弧和从其端点到中心的两条直线。如果你的三角学有点生疏,实现在链接的问题中。通用公式为:
start_x = x + radius * math.cos(start_angle)
start_y = y + radius * math.sin(start_angle)
end_x = x + radius * math.cos(angle + start_angle)
end_y = y + radius * math.sin(angle + start_angle)
然后你需要定义自己的
draw_hollow_sector
函数,它看起来像这样:
它包括一个填充的部分,以便您可以看到空心部分正确地勾勒出它的轮廓。
这是完整的代码清单,请注意它已使用 black 自动格式化。
import pyglet
import math
def draw_hollow_sector(
x,
y,
radius,
angle=6.283185307179586,
start_angle=0,
color=(255, 255, 255, 255),
batch=None,
group=None,
):
"""Draw an arc joined by two lines from its endpoints to the center to form a hollow sector"""
# calculate sector line coordinates
start_x = x + radius * math.cos(start_angle)
start_y = y + radius * math.sin(start_angle)
end_x = x + radius * math.cos(angle + start_angle)
end_y = y + radius * math.sin(angle + start_angle)
# draw the arc
arc = pyglet.shapes.Arc(
x=x,
y=y,
radius=radius,
angle=angle,
start_angle=start_angle,
color=color,
closed=False,
batch=batch,
group=group,
)
# draw sector lines
start_arc_line = pyglet.shapes.Line(
x, y, start_x, start_y, color=color, batch=batch, group=group
)
end_arc_line = pyglet.shapes.Line(
x, y, end_x, end_y, color=color, batch=batch, group=group
)
# need to return shapes to ensure they're not garbage collected
return arc, start_arc_line, end_arc_line
window = pyglet.window.Window(caption="Hollow Sectors")
batch = pyglet.graphics.Batch()
x, y = 200, 200
angle = math.pi / 3
start_angle = 1
radius = 70
sector = pyglet.shapes.Sector(
x, y, radius=radius, angle=angle, start_angle=start_angle, batch=batch
)
hollow_shapes = draw_hollow_sector(
x,
y,
radius=radius,
angle=angle,
start_angle=start_angle,
color=(255, 0, 0, 255),
batch=batch,
)
hollow_shapes += draw_hollow_sector(
300,
300,
radius=110,
angle=2.7,
start_angle=0.6,
color=(255, 255, 0, 255),
batch=batch,
)
hollow_shapes += draw_hollow_sector(
200,
180,
radius=125,
angle=5.1,
start_angle=-4.1,
color=(255, 0, 255, 255),
batch=batch,
)
@window.event
def on_draw():
window.clear()
batch.draw()