How to draw a circular arc / hallow sector in pyglet

48 views Asked by At

I desperately trying to draw circular arc / hallow sector in pyglet. E.g from this post where ArcType.ROUND is shown https://github.com/pyglet/pyglet/issues/349

This gives me a filled sector: Sector(x=200, y=200, radius=200, segments=200, angle=22.5, start_angle=0)

This gives me only the line/circular arc - without the lines of the size r to the center: Arc(px, py, radius, 50, tau, 0)

What I need is: enter image description here

PS: It comes to my attention that pip install pyglet=2.0.10 with python 3.10 give me a "broken" package on my windows 10 machine. The Arc could not be drawn at all. Installing pyglet from source solved this issue.

1

There are 1 answers

0
import random On

You can define your own function to draw the arc and two lines from its endpoints to the centre. If your trigonometry is a little rusty, the implementation is in the linked issue. The generic formulae are:

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)

Then you need to define your own draw_hollow_sector function and it'll look something like this: Hollow Sectors

It includes a filled sector so that you can see the hollow sector outlines it correctly.

Here's the full code listing, note that it has been autoformatted with 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()