Загрузка данных


#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define FLOWER_COUNT      8
#define PETAL_COUNT_MIN   8
#define PETAL_COUNT_MAX   8
#define BASE_PETAL_W      15.0
#define BASE_PETAL_H      45.0
#define BASE_CENTER_R     20
#define SCALE_MIN         0.8
#define SCALE_MAX         1.2

typedef struct {
    int x, y;
    int petals;
    double scale;
} Daisy;

static int rand_range(int min, int max) {
    if (min == max) return min;
    return min + rand() % (max - min + 1);
}

int main() {
    srand((unsigned)time(NULL));

    Display *dpy = XOpenDisplay(NULL);
    if (!dpy) { fprintf(stderr, "Ошибка дисплея\n"); return 1; }

    int screen = DefaultScreen(dpy);
    Window root = RootWindow(dpy, screen);
    int win_w = 800, win_h = 600;
    int margin = 60;

    Window win = XCreateSimpleWindow(dpy, root, 50, 50, win_w, win_h, 1,
                                     BlackPixel(dpy, screen), WhitePixel(dpy, screen));
    XSelectInput(dpy, win, ExposureMask | KeyPressMask | StructureNotifyMask);
    XMapWindow(dpy, win);

    GC gc = XCreateGC(dpy, win, 0, NULL);
    XSetFillStyle(dpy, gc, FillSolid);

    Colormap cmap = DefaultColormap(dpy, screen);
    XColor xc;
    XAllocNamedColor(dpy, cmap, "skyblue", &xc, &xc); unsigned long blue   = xc.pixel;
    XAllocNamedColor(dpy, cmap, "yellow",  &xc, &xc); unsigned long yellow = xc.pixel;
    XAllocNamedColor(dpy, cmap, "white",   &xc, &xc); unsigned long white  = xc.pixel;

    Atom wmDelete = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(dpy, win, &wmDelete, 1);

    Daisy *daisies = malloc(FLOWER_COUNT * sizeof(Daisy));
    for (int i = 0; i < FLOWER_COUNT; i++) {
        daisies[i].x = rand_range(margin, win_w - margin);
        daisies[i].y = rand_range(margin, win_h - margin);
        daisies[i].petals = rand_range(PETAL_COUNT_MIN, PETAL_COUNT_MAX);
        daisies[i].scale = SCALE_MIN + ((double)rand() / RAND_MAX) * (SCALE_MAX - SCALE_MIN);
    }

    const int PTS_COUNT = 32;
    XPoint *pts = malloc(PTS_COUNT * sizeof(XPoint));

    XEvent ev;
    int running = 1;
    while (running) {
        XNextEvent(dpy, &ev);
        switch (ev.type) {
            case Expose:
                XSetForeground(dpy, gc, white);
                XFillRectangle(dpy, win, gc, 0, 0, win_w, win_h);

                for (int f = 0; f < FLOWER_COUNT; f++) {
                    Daisy *d = &daisies[f];
                    double s = d->scale;

                    double p_w = BASE_PETAL_W * s;
                    double p_h = BASE_PETAL_H * s;
                    double dist = (BASE_PETAL_H + BASE_CENTER_R) * 0.6 * s;
                    int c_r = (int)(BASE_CENTER_R * s);

                    XSetForeground(dpy, gc, blue);
                    for (int i = 0; i < d->petals; i++) {
                        double angle = (2.0 * M_PI * i) / d->petals;
                        double cos_a = cos(angle);
                        double sin_a = sin(angle);

                        for (int p = 0; p < PTS_COUNT; p++) {
                            double t = (2.0 * M_PI * p) / PTS_COUNT;
                            double lx = p_h * cos(t);
                            double ly = p_w * sin(t);
                            double rx = lx * cos_a - ly * sin_a;
                            double ry = lx * sin_a + ly * cos_a;
                            pts[p].x = (short)(d->x + rx + dist * cos_a);
                            pts[p].y = (short)(d->y + ry + dist * sin_a);
                        }
                        XFillPolygon(dpy, win, gc, pts, PTS_COUNT, Convex, CoordModeOrigin);
                    }

                    XSetForeground(dpy, gc, yellow);
                    XFillArc(dpy, win, gc, d->x - c_r, d->y - c_r,
                             c_r * 2, c_r * 2, 0, 360 * 64);
                }
                break;

            case KeyPress:
                running = 0;
                break;
            case ClientMessage:
                if (ev.xclient.data.l[0] == wmDelete) running = 0;
                break;
        }
    }

    free(pts);
    free(daisies);
    XFreeGC(dpy, gc);
    XDestroyWindow(dpy, win);
    XCloseDisplay(dpy);
    return 0;
}