summaryrefslogtreecommitdiff
path: root/stern.c
blob: c64de9592f7f03354aefbbecbfe6e836cf2077ea (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
/* $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>

int Calc3D(int *, int *, int *);
void DrawObject(int);
void MainLoop(void);

#define Usleep 10000
#define Width 400
#define Height 400
#define Pi (4 * atan(1))

#define SECOND
#if defined(FIRST)
#define D 500
#define Lines 24
static int World[Lines][3] = {
	 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
#define Lines 36
static int World[Lines][3] = {
	-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
#define Lines 72
static int World[Lines][3] = {
	 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
	

Display *display;
Window win;
GC gc;
/* XGCValues values; */

int main(void)
{
	XEvent event;
	int screen;
	int black, white;
	int r = 0;

	display = XOpenDisplay(NULL);
	screen = DefaultScreen(display);
	black = BlackPixel(display, screen);
	white = WhitePixel(display, screen);
	win = XCreateSimpleWindow(display, DefaultRootWindow(display),
		0, 0, Width, Height, 0, black, white);

	XSelectInput(display, win, StructureNotifyMask 
		| ExposureMask | KeyPressMask);

	gc = XCreateGC(display, win, 0, NULL);
	XSetForeground(display, gc, black);
	XSetBackground(display, gc, white);
	XSetLineAttributes(display, gc, 2, LineSolid, CapButt, JoinBevel);
	XSetFillStyle(display, gc, FillSolid);

	XMapWindow(display, win);

	for (;;) {
		if (XPending(display) == 0) {
			XClearWindow(display, win);
			if (r >= 360)
				r = 0;
			DrawObject(++r);
		} else
			XNextEvent(display, &event);

		switch (event.type) {
		case Expose:
			break;
		case MapNotify:
			break;
		case KeyPress:
			if (event.xkey.keycode == XKeysymToKeycode(display,
			    XK_q)) {
				XCloseDisplay(display);
				exit(0);
			}
		}

		usleep(Usleep);

	}

	return 0;
}

float Sr, Cr;

void DrawObject(int r)
{
	float R;
	int i;
	int x1, y1, x2, y2;

	R = Pi * r / 180;

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

	for(i=0; i<Lines; i+=2) {
		Calc3D(World[i], &x1, &y1);
		Calc3D(World[i+1], &x2, &y2);
		XDrawLine(display, win, gc, x1, y1, x2, y2);
	}
}

int
Calc3D(int *w, int *x, int *y)
{
	float X, Y, Z, Xa, Ya, Za;

	X = w[0];
	Y = w[1];
	Z = w[2];

	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;
}