矩形相关操作

题目描述

如下结构用来存储图像屏幕上的对象信息。
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类型的另外一个实际参数)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值