首页
登录 | 注册

矩形相关操作

题目描述

如下结构用来存储图像屏幕上的对象信息。
struct point {int x, y;};
struct rectangle{struct point upper_left, lower_right;};
编写函数,要求可以在rectangle结构变量r上执行以下操作,且r作为实际参数传递
1.计算r的面积
2.计算r的中心,并且以此中心作为point值返回,如果中心的x或y坐标不为整数,取整数值
3.确定点p是否在r内,返回1或0.(p是struct point类型的另外一个实际参数)

输入描述

输入分两行
第一行输入四个整数,分别代表矩形的左上角和右下角坐标
第二行输入两个整数,代表某个点的坐标

输出描述

输出分三行
Area of r is 面积值
Center of r is <中心点坐标>
Point <输入点的坐标> is [not] in r

代码

<span style="font-size:14px;">#include<iostream>
using namespace std;
struct point
{
	int x, y;
};
struct rectangle
{
	struct point upper_left, lower_right;
};
int mj(int x1, int y1, int x2, int y2)
{
	int s;
	s = (x2 - x1)*(y1 - y2);
	return s;
}
struct point u(int x1, int y1, int x2, int y2)
{
	point p;
	p.x = (x1 + x2) / 2; p.y = (y1 + y2) / 2;
	return p;
}
int r(int x1, int y1, int x2, int y2, int x3, int y3)
{
	if ((x1 <= x3) && (x3 <= x2) && (y1 >= y3) && (y3 >= y2))
		return 1;
	else
		return 0;
}
int main()
{
	int s,a;
	int x1, x2, y1, y2, x3, y3;
	struct point b;
	cin >> x1 >> y1 >> x2 >> y2;
	cin >> x3 >> y3;
	struct point upper_left = { x1, y1 };
	struct point lower_right = { x2, y2 };
	s = mj(x1, y1, x2, y2);
	b=u(x1, y1, x2, y2);
	cout << "Area of r is " << s << endl;
	cout << "Center of r is <" << b.x<<","<<b.y << ">" << endl;
	a = r(x1, y1, x2, y2, x3, y3);
	if (a ==1)
		cout << "Point " << "<" << x3 << "," << y3 << "> is in r";
	else
		cout << "Point " << "<" << x3 << "," << y3 << "> is not in r";
	return 0;
}</span>

是struct oint类型的另外一个实际参数)



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.008 s.
京ICP备10005923号