The foliowing structures are designed to store information about objects on a graphics screen:struct point { int x, y; };struct rectangle { struct point upper_left, lower_right; };A point structure stores the x and y coordinates of a point on the screen. A rectangle structure stores the coordinates of the upper left and lower right corners of a rectangle. Write functions that perform the following operations on a rectangle structure r passed as an argument:(a) Compute the area of r.(b) Compute the center of r. returning it as a point value. If either the x or y coordinate of the center isn't an integer, store its truncated value in the point structure.(c) Move r by x units in the x direction and y units in the y direction, returning the modified version of r. (x and y are additional arguments to the function.)(d) Determine whether a point p lies within x. returning true or false. (p is an additional argument of iype struct point.)分别用C语言给出完整代码
时间: 2023-06-27 08:07:00 浏览: 232
(a) 计算矩形 r 的面积
```
int area(struct rectangle r) {
int width = r.lower_right.x - r.upper_left.x;
int height = r.lower_right.y - r.upper_left.y;
return width * height;
}
```
(b) 计算矩形 r 的中心坐标
```
struct point center(struct rectangle r) {
struct point center;
center.x = (r.upper_left.x + r.lower_right.x) / 2;
center.y = (r.upper_left.y + r.lower_right.y) / 2;
if (center.x != (int)center.x) {
center.x = (int)center.x;
}
if (center.y != (int)center.y) {
center.y = (int)center.y;
}
return center;
}
```
(c) 移动矩形 r
```
struct rectangle move(struct rectangle r, int x, int y) {
r.upper_left.x += x;
r.upper_left.y += y;
r.lower_right.x += x;
r.lower_right.y += y;
return r;
}
```
(d) 判断点 p 是否在矩形 r 中
```
bool is_point_in_rect(struct rectangle r, struct point p) {
if (p.x >= r.upper_left.x && p.x <= r.lower_right.x &&
p.y >= r.upper_left.y && p.y <= r.lower_right.y) {
return true;
} else {
return false;
}
}
```
阅读全文