Linuxpops  3.01.04
Spectral Anaysis Package for Single Dish Data
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test.c
Go to the documentation of this file.
1 /*
2  * * fib.c -- a simple X program that illustrates the golden ratio and spiral
3  * *
4  * * gcc fib.c -o fib -lX11 -lm
5  * */
6 
7 #include <X11/Xlib.h>
8 #include <X11/Xutil.h>
9 #include <stdio.h>
10 #include <math.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #define M_PHI 1.618033988749895
15 #define WINDOW_HEIGHT 1000
16 
17 /*
18  * * this expects a 24-bit colour value (i.e., 0xRRGGBB)
19  * * the high 8 bits are ignored
20  * */
21 void set_colour(Display *display, int s, Colormap cmap, unsigned int rgb_value)
22 {
23  XColor colour;
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);
29 
30  XSetForeground(display, DefaultGC(display, s), colour.pixel);
31 }
32 
33 void draw_section(Display *display, Window window, int s, Colormap cmap, unsigned int xpos, unsigned int ypos, unsigned int height, unsigned short index)
34 {
35  if (index % 2)
36  XFillRectangle(display, window, DefaultGC(display, s), xpos, ypos, height, height * M_PHI);
37  else
38  XFillRectangle(display, window, DefaultGC(display, s), xpos, ypos, height * M_PHI, height);
39 
40  set_colour(display, s, cmap, 0xFFFFFF);
41 
42  switch(index % 4)
43  {
44  case 0:
45  XDrawArc(display, window, DefaultGC(display, s), xpos + 1, ypos + 1, 2 * height - 2, 2 * height - 2, 90 * 64, 90 * 64);
46  break;
47  case 1:
48  XDrawArc(display, window, DefaultGC(display, s), xpos - height + 1, ypos + 1, 2 * height - 2, 2 * height - 2, 0 * 64, 90 * 64);
49  break;
50  case 2:
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);
52  break;
53  case 3:
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);
55  break;
56  }
57 }
58 
59 void draw_spiral(Display *display, Window window, int s, Colormap cmap, unsigned int xpos, unsigned int ypos, unsigned int height, unsigned int seed)
60 {
61 unsigned short index = 0;
62  while (height > 2)
63  {
64  set_colour(display, s, cmap, rand_r(&seed));
65  draw_section(display, window, s, cmap, xpos, ypos, height, index);
66 
67  switch(index % 4)
68  {
69  case 0:
70  xpos += height;
71  break;
72  case 1:
73  ypos += height;
74  break;
75  }
76 
77  height = round((double)height / M_PHI);
78  index++;
79  }
80 }
81 
82 int main(void)
83 {
84  Display *display;
85  Window window;
86  XSetWindowAttributes attributes;
87  XSizeHints sizehints;
88  XWMHints hints;
89  XTextProperty windowName, iconName;
90  XEvent event;
91  Colormap cmap;
92  XColor colour;
93 
94  char *window_name = "Fib -- an xlib golden spiral";
95  char *icon_name = "Fib";
96 
97  int s;
98  unsigned long value_mask;
99 
100  /* open connection to X server */
101  display = XOpenDisplay(NULL);
102  if (display == NULL)
103  {
104  fprintf(stderr, "ERROR -- cannot open display\n");
105  exit(7);
106  }
107 
108  s = DefaultScreen(display);
109  cmap = DefaultColormap(display, s);
110 
111  /* set up attributes */
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;
116 
117  /* create window */
118  window = XCreateWindow(display,
119  RootWindow(display, s),
120  10, 10, WINDOW_HEIGHT * M_PHI + 80, WINDOW_HEIGHT + 60, 2,
121  DefaultDepth(display, s),
122  InputOutput,
123  DefaultVisual(display, s),
124  value_mask, &attributes);
125 
126  /* select events we care about */
127  XSelectInput(display, window, ExposureMask | KeyPressMask);
128 
129  /* give the window hints */
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);
139 
140  /* map the window */
141  XMapWindow(display, window);
142 
143  /* event loop */
144  for (;;)
145  {
146  XNextEvent(display, &event);
147 
148  /* draw/redraw window */
149  if (event.type == Expose)
150  {
151  draw_spiral(display, window, s, cmap, 40, 30, WINDOW_HEIGHT, 1428571428);
152  XFlush(display);
153  }
154 
155  /* exit on keystroke */
156  if (event.type == KeyPress)
157  break;
158  }
159 
160  /* close connection to X server */
161  XCloseDisplay(display);
162 
163  return 0;
164 }
int main(void)
Definition: gsl_try_2.c:176
#define M_PHI
Definition: test.c:14
void draw_section(Display *display, Window window, int s, Colormap cmap, unsigned int xpos, unsigned int ypos, unsigned int height, unsigned short index)
Definition: test.c:33
void set_colour(Display *display, int s, Colormap cmap, unsigned int rgb_value)
Definition: test.c:21
void draw_spiral(Display *display, Window window, int s, Colormap cmap, unsigned int xpos, unsigned int ypos, unsigned int height, unsigned int seed)
Definition: test.c:59
#define WINDOW_HEIGHT
Definition: test.c:15