Raylib: [shapes] DrawArc

Created on 27 Jan 2019  Â·  8Comments  Â·  Source: raysan5/raylib

I can't seem to find a way to draw arcs filled or otherwise. I working around this by cutting the circle with a rectangle that works fine till you want to use the alpha channel.

Most helpful comment

I found that you don't need these lines
//angle between 0...359
startAngle %= 360;
endAngle %= 360;
And without them you can draw any arc from any point.

Now to solve for ellipse...

All 8 comments

Look at DrawCircleV() from shapes.c, it's fairly easy to modify it to draw arcs.

Maybe adding functions like DrawArc() and DrawArcLines() will be useful in the future.

Actually there are many ways to draw arcs by using raylib built-in functions, just made this quick thing using DrawPolyEx()

void DrawArc(Vector2 center, float radius, int startAngle, int endAngle, Color color) {
    #define SEGMENTS 16

    //angle between 0...359
    startAngle %= 360;
    endAngle %= 360;

    //calculate segment point coordinates for the arc
    Vector2 points[SEGMENTS+3];
    int step = (endAngle-startAngle)/(SEGMENTS-1);
    step = (step <= 0) ? 1 : step;

    float angle = startAngle;
    for(int i = 1; i<SEGMENTS; i++) {
        points[i].x = center.x + cosf(DEG2RAD*(360-angle))*radius;
        points[i].y = center.y + sinf(DEG2RAD*(360-angle))*radius;
        //DrawLine(center.x, center.y, points[i].x, points[i].y, i==1?BLUE:RED); //to visualize the lines
        angle += step;
    }
    points[0] = center;
    points[SEGMENTS] = (Vector2){center.x + cosf(DEG2RAD*(360-endAngle))*radius, center.y + sinf(DEG2RAD*(360-endAngle))*radius};
    points[SEGMENTS+1] = center;

    //DrawLine(center.x, center.y, points[SEGMENTS].x, points[SEGMENTS].y, RED); //to visualize the lines
    DrawPolyEx(&points, SEGMENTS+3, color);
}

Use it like this DrawArc((Vector2){200,200}, 120, 20, 320, Fade(GOLD, 0.6)); to create a pacman ;)

That's for the tip I did look at shape.c and it looks easy enough to modify. My only thought is if I modify the library I have to very careful about portability.

That function is spot on and a great place to start.

Thanks very much.

I found that you don't need these lines
//angle between 0...359
startAngle %= 360;
endAngle %= 360;
And without them you can draw any arc from any point.

Now to solve for ellipse...

@Demizdor @DarkElvenAngel Feel free to send a PR with DrawArc() and DrawArcLines(). :)

Just reviewing this issue... noticed that DrawCircleSector() (a pacman or circle piece) can be accomplished just adapting DrawCircleV(), actually it already draws quads/triangles every 10 degrees to create the circle...

But supporting DrawArc() (like a ring, with inner radius and outer radius) is a bit more complex.

DrawCircleSector() redesigned in commit https://github.com/raysan5/raylib/commit/88dfd2ab236a28db3cf1e3715b9e98a426de48d6.

@Demizdor Feel free to send your DrawRing() implementation!

Closing this issue! Thanks @Demizdor!

Was this page helpful?
0 / 5 - 0 ratings