c - How to draw rounded rectangle in X11 - Stack Overflow

I'm trying to draw a rounded rectangle in X11 using the following:#include <X11Xlib.h>#in

I'm trying to draw a rounded rectangle in X11 using the following:

#include <X11/Xlib.h>
#include <stdlib.h>

int main() {
    Display *display;
    Window window;
    GC gc;
    XEvent event;
    int screen;
    
    display = XOpenDisplay(NULL);
    if (display == NULL) {
        exit(1);
    }

    screen = DefaultScreen(display);

    window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 400, 300, 1, BlackPixel(display, screen), WhitePixel(display, screen));
    XSelectInput(display, window, ExposureMask | KeyPressMask);
    XMapWindow(display, window);
    gc = XCreateGC(display, window, 0, NULL);

    while (1) {
        XNextEvent(display, &event);
        if (event.type == Expose) {
            int x = 50, y = 50, width = 200, height = 100, radius = 5;
        
            // Draw the main rectangle (central part)
            XFillRectangle(display, window, gc, x + radius, y, width - 2 * radius, height);
            XFillRectangle(display, window, gc, x, y + radius, width, height - 2 * radius);
                    
            // Draw the arcs for the corners
            XFillArc(display, window, gc, x, y, 2 * radius, 2 * radius, 90 * 64, 90 * 64); // Top-left corner
            XFillArc(display, window, gc, x + width - 2 * radius, y, 2 * radius, 2 * radius, 0 * 64, 90 * 64); // Top-right corner
            XFillArc(display, window, gc, x, y + height - 2 * radius, 2 * radius, 2 * radius, 180 * 64, 90 * 64); // Bottom-left corner
            XFillArc(display, window, gc, x + width - 2 * radius, y + height - 2 * radius, 2 * radius, 2 * radius, 270 * 64, 90 * 64); // Bottom-right corner
        }
    }

    XCloseDisplay(display);
    return 0;
}

However, if the given border radius is small (i.e. less than 5px), not all corners look the same:

Similarily, if I make a very small 360-degree arc:

XFillArc(display, window, gc, x, y, 10, 10, 0, 360 * 64)

I get a drawing that is not a symmetrical circle:

EDIT: This doesn't occur if the arc's width and height are odd numbers. Arguably that makes some sense, but strangely, it seems that when an arc requested what is actually drawn is the corresponding segment of this 'circle'.

I suppose that since I multiply the given radius by 2 when creating my rounded rectangle, the width and height of each arc are always even numbers and therefore the arcs never look quite right. There must be a way to make each arc look the same given any width/height without having to round the radius to an even number.

Does anyone have any thoughts on how I could make this work? Thanks!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744180087a4561938.html

相关推荐

  • c - How to draw rounded rectangle in X11 - Stack Overflow

    I'm trying to draw a rounded rectangle in X11 using the following:#include <X11Xlib.h>#in

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信