14 #define M_PHI 1.618033988749895
15 #define WINDOW_HEIGHT 1000
21 void set_colour(Display *display,
int s, Colormap cmap,
unsigned int rgb_value)
24 colour.red = (rgb_value & 0xFF0000) >> 8;
25 colour.green = (rgb_value & 0xFF00);
26 colour.blue = (rgb_value & 0xFF) << 8;
27 colour.flags = DoRed | DoGreen | DoBlue;
28 XAllocColor(display, cmap, &colour);
30 XSetForeground(display, DefaultGC(display, s), colour.pixel);
33 void draw_section(Display *display, Window window,
int s, Colormap cmap,
unsigned int xpos,
unsigned int ypos,
unsigned int height,
unsigned short index)
36 XFillRectangle(display, window, DefaultGC(display, s), xpos, ypos, height, height *
M_PHI);
38 XFillRectangle(display, window, DefaultGC(display, s), xpos, ypos, height * M_PHI, height);
45 XDrawArc(display, window, DefaultGC(display, s), xpos + 1, ypos + 1, 2 * height - 2, 2 * height - 2, 90 * 64, 90 * 64);
48 XDrawArc(display, window, DefaultGC(display, s), xpos - height + 1, ypos + 1, 2 * height - 2, 2 * height - 2, 0 * 64, 90 * 64);
51 XDrawArc(display, window, DefaultGC(display, s), xpos + height * (M_PHI - 2) + 1, ypos - height + 1, 2 * height - 1, 2 * height - 2, 270 * 64, 90 * 64);
54 XDrawArc(display, window, DefaultGC(display, s), xpos + 1, ypos + height * (M_PHI - 2) + 1, 2 * height - 2, 2 * height - 2, 180 * 64, 90 * 64);
59 void draw_spiral(Display *display, Window window,
int s, Colormap cmap,
unsigned int xpos,
unsigned int ypos,
unsigned int height,
unsigned int seed)
61 unsigned short index = 0;
65 draw_section(display, window, s, cmap, xpos, ypos, height, index);
77 height = round((
double)height /
M_PHI);
86 XSetWindowAttributes attributes;
89 XTextProperty windowName, iconName;
94 char *window_name =
"Fib -- an xlib golden spiral";
95 char *icon_name =
"Fib";
98 unsigned long value_mask;
101 display = XOpenDisplay(NULL);
104 fprintf(stderr,
"ERROR -- cannot open display\n");
108 s = DefaultScreen(display);
109 cmap = DefaultColormap(display, s);
112 attributes.background_pixel = BlackPixel(display, s);
113 attributes.border_pixel = WhitePixel(display, s);
114 attributes.event_mask = ButtonPressMask;
115 value_mask = CWBackPixel | CWBorderPixel | CWEventMask;
118 window = XCreateWindow(display,
119 RootWindow(display, s),
121 DefaultDepth(display, s),
123 DefaultVisual(display, s),
124 value_mask, &attributes);
127 XSelectInput(display, window, ExposureMask | KeyPressMask);
130 sizehints.flags = USPosition | USSize;
131 XSetWMNormalHints(display, window, &sizehints);
132 hints.initial_state = NormalState;
133 hints.flags = StateHint;
134 XSetWMHints(display, window, &hints);
135 XStringListToTextProperty(&window_name, 1, &windowName);
136 XSetWMName(display, window, &windowName);
137 XStringListToTextProperty(&icon_name, 1, &iconName);
138 XSetWMIconName(display, window, &iconName);
141 XMapWindow(display, window);
146 XNextEvent(display, &event);
149 if (event.type == Expose)
156 if (event.type == KeyPress)
161 XCloseDisplay(display);
void draw_section(Display *display, Window window, int s, Colormap cmap, unsigned int xpos, unsigned int ypos, unsigned int height, unsigned short index)
void set_colour(Display *display, int s, Colormap cmap, unsigned int rgb_value)
void draw_spiral(Display *display, Window window, int s, Colormap cmap, unsigned int xpos, unsigned int ypos, unsigned int height, unsigned int seed)