From 1290b40f2d50445e8ed97a3b8a72161c3f145482 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 26 Jan 2008 19:12:34 +0000 Subject: replace arrays with structs --- stern.c | 168 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file 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 #include -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; ix; + Y = p->y; + Z = p->z; Ya = Y * Cr + Z * Sr; Za = Z * Cr - Y * Sr; -- cgit v1.2.3