summaryrefslogtreecommitdiff
path: root/stern.c
blob: b9c1ac12ebc5a1008f1f9a2fff2a0dc8b415c784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* $Id$
 * stern.c   25.12.2006
 * stern.c   07.02.2004
 * STERN.C   11.07.2003
 * STERN.CPP 11.02.2000
 * STERN.BAS 29.04.1996
 */

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

struct point {
	int	x, y, z;
};

struct line {
	struct	point a, b;
};

int	Calc3D(struct point *, float, float, int *, int *);
void	DrawObject(int, struct line *, int);

#define Usleep	10000
#define Width	400
#define Height	400
#define Pi	M_PI
#define NMELM(p)	(sizeof(p) / sizeof((p)[0]))

#define SECOND
#if defined(FIRST)
#define D	500
struct line world[] = {
	{{ 50, 50, 50}, {-50, 50, 50}},
	{{ 50,-50, 50}, {-50,-50, 50}},
	{{ 50, 50,-50}, {-50, 50,-50}},
	{{ 50,-50,-50}, {-50,-50,-50}},
	{{ 50, 50, 50}, { 50,-50, 50}},
	{{-50, 50, 50}, {-50,-50, 50}},
	{{ 50, 50,-50}, { 50,-50,-50}},
	{{-50, 50,-50}, {-50,-50,-50}},
	{{ 50, 50, 50}, { 50, 50,-50}},
	{{ 50,-50, 50}, { 50,-50,-50}},
	{{-50,-50, 50}, {-50,-50,-50}},
	{{-50, 50, 50}, {-50, 50,-50}}
};
#elif defined(SECOND)
#define	D	200
struct line world[] = {
	{{-60, 40, 20}, {-20, 40, 20}},
	{{-20, 40, 20}, {-20,  0, 20}},
	{{-20,  0, 20}, { 60,  0, 20}},
	{{ 60,  0, 20}, { 60,-40, 20}},
	{{ 60,-40, 20}, {-60,-40, 20}},
	{{-60,-40, 20}, {-60, 40, 20}},
	{{-60, 40,-20}, {-20, 40,-20}},
	{{-20, 40,-20}, {-20,  0,-20}},
	{{-20,  0,-20}, { 60,  0,-20}},
	{{ 60,  0,-20}, { 60,-40,-20}},
	{{ 60,-40,-20}, {-60,-40,-20}},
	{{-60,-40,-20}, {-60, 40,-20}},
	{{-60, 40, 20}, {-60, 40,-20}},
	{{-20, 40, 20}, {-20, 40,-20}},
	{{-20,  0, 20}, {-20,  0,-20}},
	{{ 60,  0, 20}, { 60,  0,-20}},
	{{ 60,-40, 20}, { 60,-40,-20}},
	{{-60,-40, 20}, {-60,-40,-20}}
};
#else
#define	D	300
struct line world[] = {
	{{ 90,  0,  0}, { 10, 10, 10}},
	{{ 90,  0,  0}, { 10, 10,-10}},
	{{ 90,  0,  0}, { 10,-10,-10}},
	{{ 90,  0,  0}, { 10,-10, 10}},
	{{-90,  0,  0}, {-10, 10, 10}},
	{{-90,  0,  0}, {-10, 10,-10}},
	{{-90,  0,  0}, {-10,-10,-10}},
	{{-90,  0,  0}, {-10,-10, 10}},
	{{  0, 90,  0}, { 10, 10, 10}},
	{{  0, 90,  0}, {-10, 10, 10}},
	{{  0, 90,  0}, {-10, 10,-10}},
	{{  0, 90,  0}, { 10, 10,-10}},
	{{  0,-90,  0}, { 10,-10, 10}},
	{{  0,-90,  0}, {-10,-10, 10}},
	{{  0,-90,  0}, {-10,-10,-10}},
	{{  0,-90,  0}, { 10,-10,-10}},
	{{  0,  0, 90}, { 10, 10, 10}},
	{{  0,  0, 90}, { 10,-10, 10}},
	{{  0,  0, 90}, {-10,-10, 10}},
	{{  0,  0, 90}, {-10, 10, 10}},
	{{  0,  0,-90}, { 10, 10,-10}},
	{{  0,  0,-90}, { 10,-10,-10}},
	{{  0,  0,-90}, {-10,-10,-10}},
	{{  0,  0,-90}, {-10, 10,-10}},
	{{ 10, 10, 10}, {-10, 10, 10}},
	{{ 10,-10, 10}, {-10,-10, 10}},
	{{ 10, 10,-10}, {-10, 10,-10}},
	{{ 10,-10,-10}, {-10,-10,-10}},
	{{ 10, 10, 10}, { 10,-10, 10}},
	{{-10, 10, 10}, {-10,-10, 10}},
	{{ 10, 10,-10}, { 10,-10,-10}},
	{{-10, 10,-10}, {-10,-10,-10}},
	{{ 10, 10, 10}, { 10, 10,-10}},
	{{ 10,-10, 10}, { 10,-10,-10}},
	{{-10,-10, 10}, {-10,-10,-10}},
	{{-10, 10, 10}, {-10, 10,-10}}
};
#endif

/* XGCValues values; */
Display	*display;
Window	win;
Pixmap	pix;
GC	wingc, pixgc;
int	black, white;
int	die = 0;

int
main(int argc, char **argv)
{
	int screen;
	int r = 0;
	int pause = 0;
	Atom delwin;

	display = XOpenDisplay(argc == 2 ? argv[1] : NULL);
	screen = DefaultScreen(display);
	black = BlackPixel(display, screen);
	white = WhitePixel(display, screen);
	win = XCreateSimpleWindow(display, DefaultRootWindow(display),
		0, 0, Width, Height, 0, black, white);
	XStoreName(display, win, "Stern $Revision$");
	delwin = XInternAtom(display, "WM_DELETE_WINDOW", 0);
	XSetWMProtocols(display, win, &delwin, 1);

	wingc = XCreateGC(display, win, 0, NULL);
	XSetGraphicsExposures(display, wingc, False);
	XSetBackground(display, wingc, white);

	pix = XCreatePixmap(display, win, Width, Height, 1);
	pixgc = XCreateGC(display, pix, 0, NULL);
	XSetLineAttributes(display, pixgc, 2, LineSolid, CapButt, JoinBevel);

	XSelectInput(display, win, KeyPressMask);
	XMapWindow(display, win);

	while (!die) {
		if (XPending(display)) {
			XEvent event;

			XNextEvent(display, &event);
			switch (event.type) {
			case KeyPress:
				switch (XLookupKeysym(&event.xkey, 0)) {
				case XK_space:
					pause = !pause;
					break;
				case XK_Escape:
				case XK_q:
					XCloseDisplay(display);
					exit(0);
				}
			case ClientMessage:
				die = *event.xclient.data.l == delwin;
				break;
			default:
				break;
			}
		}

		if (!pause) {
			r %= 360;
			DrawObject(++r, world, NMELM(world));
		}

		usleep(Usleep);
	}

	XFreePixmap(display, pix);
	XDestroyWindow(display, win);
	XCloseDisplay(display);

	return 0;
}

void
DrawObject(int r, struct line *p, int nelem)
{
	float R, Sr, Cr;
	int i;
	int x1, y1, x2, y2;

	R = Pi * r / 180;

	Sr = sin(R);
	Cr = cos(R);

	XSetForeground(display, pixgc, 0);
	XFillRectangle(display, pix, pixgc, 0, 0, Width, Height);
	XSetForeground(display, pixgc, 1);

	for(i = 0; i < nelem; ++i) {
		Calc3D(&p[i].a, Sr, Cr, &x1, &y1);
		Calc3D(&p[i].b, Sr, Cr, &x2, &y2);
		XDrawLine(display, pix, pixgc, x1, y1, x2, y2);
	}

	XCopyPlane(display, pix, win, wingc, 0, 0, Width, Height, 0, 0, 1);
}

int
Calc3D(struct point *p, float Sr, float Cr, int *x, int *y)
{
	float X, Y, Z, Xa, Ya, Za;

	X = p->x;
	Y = p->y;
	Z = p->z;

	Ya = Y * Cr + Z * Sr;
	Za = Z * Cr - Y * Sr;
	Xa = X * Cr + Za * Sr;
	Z = Za * Cr - X * Sr;
	X = Xa * Cr + Ya * Sr;
	Y = Ya * Cr - Xa * Sr;

	*x = 2 * X * (Y + D) / D + Width / 2;
	*y = 2 * Z * (Y + D) / D + Height / 2;

	return Y < 0;
}