如何在X11中绘制对称圆角矩形?解决小半径显示异常问题
X11绘制圆角矩形与小圆弧的显示异常问题
我尝试使用以下代码在X11中绘制圆角矩形:
#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; // 绘制主体矩形(中间部分) XFillRectangle(display, window, gc, x + radius, y, width - 2 * radius, height); XFillRectangle(display, window, gc, x, y + radius, width, height - 2 * radius); // 绘制四个角的圆弧 XFillArc(display, window, gc, x, y, 2 * radius, 2 * radius, 90 * 64, 90 * 64); // 左上角 XFillArc(display, window, gc, x + width - 2 * radius, y, 2 * radius, 2 * radius, 0 * 64, 90 * 64); // 右上角 XFillArc(display, window, gc, x, y + height - 2 * radius, 2 * radius, 2 * radius, 180 * 64, 90 * 64); // 左下角 XFillArc(display, window, gc, x + width - 2 * radius, y + height - 2 * radius, 2 * radius, 2 * radius, 270 * 64, 90 * 64); // 右下角 } } XCloseDisplay(display); return 0; }
但当设置的圆角半径较小(比如小于5px)时,四个角的显示效果并不一致:

类似地,当我绘制一个小型360度圆弧时:
XFillArc(display, window, gc, x, y, 10, 10, 0, 360 * 64)
得到的图形并非对称圆形:

编辑补充:当圆弧的宽度和高度为奇数时,不会出现此问题。这似乎有一定规律,但奇怪的是,实际绘制出的圆弧看起来像是对应“圆形”的片段。
我推测,由于绘制圆角矩形时将半径乘以2,每个圆弧的宽高始终为偶数,所以才会一直出现显示问题。必须找到一种方法,能让任意宽高的圆弧都显示一致,无需将半径调整为偶数。
请问有没有人知道如何解决这个问题?谢谢!
内容的提问来源于stack exchange,提问作者Andereoo
相关产品推荐
相关产品推荐

