Skip to content

Commit 65da3c2

Browse files
committed
6.2
1 parent 25db0c4 commit 65da3c2

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <stdio.h>
2+
3+
#define XMAX 100
4+
#define YMAX 100
5+
6+
#define min(a, b) ((a) < (b) ? (a) : (b))
7+
#define max(a, b) ((a) > (b) ? (a) : (b))
8+
9+
struct point {
10+
int x;
11+
int y;
12+
};
13+
14+
struct rect {
15+
struct point pt1;
16+
struct point pt2;
17+
};
18+
19+
main()
20+
{
21+
struct rect screen;
22+
struct point middle;
23+
struct point makepoint(int, int);
24+
struct point addpoint(struct point, struct point);
25+
int ptinrect(struct point, struct rect);
26+
struct rect canonrect(struct rect);
27+
28+
screen.pt1 = makepoint(0, 0);
29+
screen.pt2 = makepoint(XMAX, YMAX);
30+
middle = makepoint((screen.pt1.x + screen.pt2.x) / 2,
31+
(screen.pt1.y + screen.pt2.y) /2);
32+
33+
printf("middle.x: %d, middle.y: %d\n", middle.x, middle.y);
34+
35+
struct point p1 = { 5, 5 };
36+
37+
p1 = addpoint(p1, middle);
38+
39+
printf("p1.x: %d, p1.y: %d\n", p1.x, p1.y);
40+
41+
int p1inscreen = ptinrect(p1, screen);
42+
43+
printf("p1inscreen: %d\n", p1inscreen);
44+
45+
struct point p2;
46+
47+
printf("p2.x: %d, p2.y: %d\n", p2.x, p2.y);
48+
49+
50+
struct rect r1 = { 10, 10, 0, 0 };
51+
52+
printf("r1.pt1.x: %d, r1.pt1.y: %d, r1.pt2.x: %d, r1.pt2.y: %d\n",
53+
r1.pt1.x, r1.pt1.y, r1.pt2.x, r1.pt2.y);
54+
55+
r1 = canonrect(r1);
56+
57+
printf("r1.pt1.x: %d, r1.pt1.y: %d, r1.pt2.x: %d, r1.pt2.y: %d\n",
58+
r1.pt1.x, r1.pt1.y, r1.pt2.x, r1.pt2.y);
59+
60+
struct rect r2 = { { 20, 0 }, { 0, 20 } };
61+
62+
printf("r2.pt1.x: %d, r2.pt1.y: %d, r2.pt2.x: %d, r2.pt2.y: %d\n",
63+
r2.pt1.x, r2.pt1.y, r2.pt2.x, r2.pt2.y);
64+
65+
r2 = canonrect(r2);
66+
67+
printf("r2.pt1.x: %d, r2.pt1.y: %d, r2.pt2.x: %d, r2.pt2.y: %d\n",
68+
r2.pt1.x, r2.pt1.y, r2.pt2.x, r2.pt2.y);
69+
70+
71+
struct point origin = { 15, 20 };
72+
struct point *pp;
73+
74+
pp = &origin;
75+
printf("origin is (%d, %d)\n", (*pp).x, (*pp).y);
76+
printf("origin is (%d, %d)\n", pp->x, pp->y);
77+
78+
return 0;
79+
}
80+
81+
struct point makepoint(int x, int y)
82+
{
83+
struct point temp;
84+
85+
temp.x = x;
86+
temp.y = y;
87+
return temp;
88+
}
89+
90+
struct point addpoint(struct point p1, struct point p2)
91+
{
92+
p1.x += p2.x;
93+
p1.y += p2.y;
94+
return p1;
95+
}
96+
97+
int ptinrect(struct point p, struct rect r)
98+
{
99+
return p.x >= r.pt1.x && p.x < r.pt2.x
100+
&& p.y >= r.pt1.y && p.y < r.pt2.y;
101+
}
102+
103+
struct rect canonrect(struct rect r)
104+
{
105+
struct rect temp;
106+
107+
temp.pt1.x = min(r.pt1.x, r.pt2.x);
108+
temp.pt1.y = min(r.pt1.y, r.pt2.y);
109+
temp.pt2.x = max(r.pt1.x, r.pt2.x);
110+
temp.pt2.y = max(r.pt1.y, r.pt2.y);
111+
return temp;
112+
}
113+

0 commit comments

Comments
 (0)