|
|
1.1 ! root 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: main() { ! 45: int i; ! 46: point x, origin = { 0, 0 }, maxpt = { 320, 320 }; ! 47: point pts[] = { -1, -1, 1, 1, 20, 300, 500, 400 }; ! 48: rect screen = makerect(addpoint(maxpt, makepoint(-10, -10)), ! 49: addpoint(origin, makepoint(10, 10))); ! 50: ! 51: for (i = 0; i < sizeof pts/sizeof pts[0]; i++) { ! 52: printf("(%d,%d) is ", pts[i].x, ! 53: (x = makepoint(pts[i].x, pts[i].y)).y); ! 54: if (ptinrect(x, screen) == 0) ! 55: printf("not "); ! 56: printf("within [%d,%d; %d,%d]\n", screen.pt1.x, screen.pt1.y, ! 57: screen.pt2.x, screen.pt2.y); ! 58: } ! 59: exit(0); ! 60: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.