summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2008-01-26 19:12:34 +0000
committerDimitri Sokolyuk <demon@dim13.org>2008-01-26 19:12:34 +0000
commit1290b40f2d50445e8ed97a3b8a72161c3f145482 (patch)
treedd91b45c0c891c6dc697409fba4b63d362281ea8
parent79d7c5db98d7f71111cba0e96610f2fd62225c17 (diff)
replace arrays with structs
-rw-r--r--stern.c168
1 files changed, 97 insertions, 71 deletions
diff --git a/stern.c b/stern.c
index c64de95..b74de23 100644
--- a/stern.c
+++ b/stern.c
@@ -13,82 +13,108 @@
#include <stdio.h>
#include <math.h>
-int Calc3D(int *, int *, int *);
-void DrawObject(int);
-void MainLoop(void);
+struct point {
+ int x, y, z;
+};
+
+struct line {
+ struct point a, b;
+};
-#define Usleep 10000
-#define Width 400
-#define Height 400
-#define Pi (4 * atan(1))
+int Calc3D(struct point *, 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
-#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
+#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
-#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
+#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
-#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
+#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;
GC gc;
-/* XGCValues values; */
int main(void)
{
@@ -120,7 +146,7 @@ int main(void)
XClearWindow(display, win);
if (r >= 360)
r = 0;
- DrawObject(++r);
+ DrawObject(++r, world, NMELM(world));
} else
XNextEvent(display, &event);
@@ -146,7 +172,7 @@ int main(void)
float Sr, Cr;
-void DrawObject(int r)
+void DrawObject(int r, struct line *p, int nelem)
{
float R;
int i;
@@ -157,21 +183,21 @@ void DrawObject(int r)
Sr = sin(R);
Cr = cos(R);
- for(i=0; i<Lines; i+=2) {
- Calc3D(World[i], &x1, &y1);
- Calc3D(World[i+1], &x2, &y2);
+ for(i = 0; i < nelem; ++i) {
+ Calc3D(&p[i].a, &x1, &y1);
+ Calc3D(&p[i].b, &x2, &y2);
XDrawLine(display, win, gc, x1, y1, x2, y2);
}
}
int
-Calc3D(int *w, int *x, int *y)
+Calc3D(struct point *p, int *x, int *y)
{
float X, Y, Z, Xa, Ya, Za;
- X = w[0];
- Y = w[1];
- Z = w[2];
+ X = p->x;
+ Y = p->y;
+ Z = p->z;
Ya = Y * Cr + Z * Sr;
Za = Z * Cr - Y * Sr;