struct.c (1540B)
1 typedef struct point { int x,y; } point; 2 typedef struct rect { point pt1, pt2; } rect; 3 4 point addpoint(point p1, point p2) { /* add two points */ 5 p1.x += p2.x; 6 p1.y += p2.y; 7 return p1; 8 } 9 10 #define min(a, b) ((a) < (b) ? (a) : (b)) 11 #define max(a, b) ((a) > (b) ? (a) : (b)) 12 13 rect canonrect(rect r) { /* canonicalize rectangle coordinates */ 14 rect temp; 15 16 temp.pt1.x = min(r.pt1.x, r.pt2.x); 17 temp.pt1.y = min(r.pt1.y, r.pt2.y); 18 temp.pt2.x = max(r.pt1.x, r.pt2.x); 19 temp.pt2.y = max(r.pt1.y, r.pt2.y); 20 return temp; 21 } 22 23 point makepoint(int x, int y) { /* make a point from x and y components */ 24 point p; 25 26 p.x = x; 27 p.y = y; 28 return p; 29 } 30 31 rect makerect(point p1, point p2) { /* make a rectangle from two points */ 32 rect r; 33 34 r.pt1 = p1; 35 r.pt2 = p2; 36 return canonrect(r); 37 } 38 39 int ptinrect(point p, rect r) { /* is p in r? */ 40 return p.x >= r.pt1.x && p.x < r.pt2.x 41 && p.y >= r.pt1.y && p.y < r.pt2.y; 42 } 43 44 struct odd {char a[3]; } y = {'a', 'b', 0}; 45 46 odd(struct odd y) { 47 struct odd x = y; 48 printf("%s\n", x.a); 49 } 50 51 main() { 52 int i; 53 point x, origin = { 0, 0 }, maxpt = { 320, 320 }; 54 point pts[] = { -1, -1, 1, 1, 20, 300, 500, 400 }; 55 rect screen = makerect(addpoint(maxpt, makepoint(-10, -10)), 56 addpoint(origin, makepoint(10, 10))); 57 58 for (i = 0; i < sizeof pts/sizeof pts[0]; i++) { 59 printf("(%d,%d) is ", pts[i].x, 60 (x = makepoint(pts[i].x, pts[i].y)).y); 61 if (ptinrect(x, screen) == 0) 62 printf("not "); 63 printf("within [%d,%d; %d,%d]\n", screen.pt1.x, screen.pt1.y, 64 screen.pt2.x, screen.pt2.y); 65 } 66 odd(y); 67 exit(0); 68 } 69