最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin6浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论