实验E5:设计一个矩形类(类的设计)

在这里插入图片描述
declare.h

#pragma once
class Point;
class line;
class Rectangle;

Rectangle.h

#pragma once
#include <iostream>
#include <algorithm>
using namespace std;
class Point {
    double x, y;
public:
    Point(double  px = 0, double py = 0) {
        x = px;
        y = py;
    }
    Point(const Point& p) {//复制构造
        x = p.x;
        y = p.y;
    }
    ~Point() {//析构
    }

    void change(double px, double py) {//改变Point的位置
        x = px;
        y = py;
    }

    void show_point() {//输出
        cout << endl << "x=" << x << endl << "y=" << y << endl;
    }
    bool operator ==(const Point &p) {//重载==运算符
        if (p.x == x && p.y == y) {
            return true;
        }
        return false;
    }
    double get_x() {
        return x;
    }
    double get_y() {
        return y;
    }
};

class line {
    double l, r;//左端点 ,右端点
public:
    line(double nl, double nr) {
        l = nl;
        r = nr;
    }
    ~line() {//析构函数

    }
    double get_l() {
        return l;
    }
    double get_r() {
        return r;
    }
    static bool cmp(line& l1, line& l2) {
        return l1.get_l() <= l2.get_l();
    }
    line find_lap(line l2, bool& flag) {
        line arrL[2] = { line(l,r),l2 };
        sort(arrL, arrL + 2, cmp);//这样就完成了一个x坐标从小到大的排列  其实也就是比较经典的区间线段合并

        if (flag && arrL[0].get_r() >= arrL[1].get_l()) {//只有当flag为true时候才会进去,防止本来是false的被覆盖为true
            flag = true;
        }
        else {
            flag = false;
        }
        return line(arrL[1].get_l(), arrL[1].get_l() + arrL[0].get_r() - arrL[1].get_l());//返回了重叠的线
    }

};

class Rectangle {
    Point p1, p2;//p1左端点,p2右端点,可以极大程度简化code量
public:
    Rectangle(double x1, double y1, double x2, double y2) {//两种初始化方式
        p1.change(x1, y1), p2.change(x2, y2);
    }
    Rectangle(Point np1, Point np2) {
        p1 = np1;
        p2 = np2;
    }
    Rectangle(const Rectangle& r) {
        p1 = r.p1;
        p2 = r.p2;
    }
    ~Rectangle() {//析构
    }
    double get_s() {//计算面积
        return abs(p1.get_x() - p2.get_x()) * abs(p1.get_y() - p2.get_y());

    }
    bool operator == (Rectangle& r) {//判断是否相等 重载==运算符
        if (p1 == r.get_p1() && r.get_p2() == p2) {
            return true;
        }
        else {
            return false;
        }
    }
    void change_size(double p2tox, double p2toy) {//改变右下角坐标
        p2.change(p2tox, p2toy);
        cout << "after moving the Lower right corner" << endl;
        this->show_point();
    }

    void moveoff(double dx, double dy) {//移动
        p1.change(p1.get_x() + dx, p1.get_y() + dy);
        p2.change(p1.get_x() + dx, p2.get_y() + dy);
    }

    void show_point() {
        cout << "the Upper left corner is"; p1.show_point();
        cout << "the Lower right corner is"; p2.show_point();
    }
    Point get_p1() {
        return p1;
    }
    Point get_p2() {
        return p2;
    }
    Rectangle find_overlap(Rectangle& r, bool& flag) {
        flag = true;
        line xl1(p1.get_x(), p2.get_x());
        line xl2(r.p1.get_x(), r.p2.get_x());
        line yl1(p2.get_y(), p1.get_y());
        line yl2(r.p2.get_y(), r.p1.get_y());

        line ansx = xl1.find_lap(xl2, flag);//x方向重叠区域
        line ansy = yl1.find_lap(yl2, flag);//y方向重叠区域
        //此时用flag判断是否有一个正确的结果,true为有,反之无
        return Rectangle(Point(ansx.get_l(), ansy.get_r()), Point(ansx.get_r(), ansy.get_l()));

    } //思路来源 sdu新生赛 D题

};
//class 创建完成


main

#include <iostream>
using namespace std;
#include "declare.h"
#include "Rectangle.h"
int main() {

    Rectangle a(1, 2, 3, 1);
    Rectangle b(-2, -1, -1, -3);
    Rectangle c(1, 2, 3, 1);
    bool flag = 0;
    cout << "the area of a is" << a.get_s() << endl;//计算面积

    if (a == b) {
        cout << "a==b" << endl;
    }
    else {
        cout << "a does not equal b" << endl;
    }

    if (a == c) {
        cout << "a==c" << endl;
    }
    else {
        cout << "a does not equal c" << endl;
    }
    a.change_size(9, 4);
    a.moveoff(1, 3);
    a.show_point();
    
    Rectangle ans = a.find_overlap(b, flag);
    if (flag) {
        ans.show_point();
    }
    else {
        cout << "They do not have overlap" << endl;
    }

    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值