00001 typedef struct point { int x,y; } point;
00002 typedef struct rect { point pt1, pt2; } rect;
00003
00004 point addpoint(point p1, point p2) {
00005 p1.x += p2.x;
00006 p1.y += p2.y;
00007 return p1;
00008 }
00009
00010 #define min(a, b) ((a) < (b) ? (a) : (b))
00011 #define max(a, b) ((a) > (b) ? (a) : (b))
00012
00013 rect canonrect(rect r) {
00014 rect temp;
00015
00016 temp.pt1.x = min(r.pt1.x, r.pt2.x);
00017 temp.pt1.y = min(r.pt1.y, r.pt2.y);
00018 temp.pt2.x = max(r.pt1.x, r.pt2.x);
00019 temp.pt2.y = max(r.pt1.y, r.pt2.y);
00020 return temp;
00021 }
00022
00023 point makepoint(int x, int y) {
00024 point p;
00025
00026 p.x = x;
00027 p.y = y;
00028 return p;
00029 }
00030
00031 rect makerect(point p1, point p2) {
00032 rect r;
00033
00034 r.pt1 = p1;
00035 r.pt2 = p2;
00036 return canonrect(r);
00037 }
00038
00039 int ptinrect(point p, rect r) {
00040 return p.x >= r.pt1.x && p.x < r.pt2.x
00041 && p.y >= r.pt1.y && p.y < r.pt2.y;
00042 }
00043
00044 struct odd {char a[3]; } y = {'a', 'b', 0};
00045
00046 odd(struct odd y) {
00047 struct odd x = y;
00048 printf("%s\n", x.a);
00049 }
00050
00051 main() {
00052 int i;
00053 point x, origin = { 0, 0 }, maxpt = { 320, 320 };
00054 point pts[] = { -1, -1, 1, 1, 20, 300, 500, 400 };
00055 rect screen = makerect(addpoint(maxpt, makepoint(-10, -10)),
00056 addpoint(origin, makepoint(10, 10)));
00057
00058 for (i = 0; i < sizeof pts/sizeof pts[0]; i++) {
00059 printf("(%d,%d) is ", pts[i].x,
00060 (x = makepoint(pts[i].x, pts[i].y)).y);
00061 if (ptinrect(x, screen) == 0)
00062 printf("not ");
00063 printf("within [%d,%d; %d,%d]\n", screen.pt1.x, screen.pt1.y,
00064 screen.pt2.x, screen.pt2.y);
00065 }
00066 odd(y);
00067 exit(0);
00068 }
00069