C++定义一个矩形类

  1. 定义一个矩形类rectangle,理解类的定义和this的使用。
    (1)定义两个实数数据成员x和y,作为矩形左上角坐标,定义两个实数数据成员w和h,作为矩形的长宽;
    (2)定义有参和无参的构造函数,在初始化时使用this标识数据成员;
    (3)定义成员函数:
    移动矩形 void move(double offsetx, double offsety)
    改变矩形大小 void zoom(double offsetw,double offseth)
    判断一点是否在矩形内bool in_rect(double x,double y),在返回1,否则返回0求两个矩形合并后的矩形 rectangel union_rect(rectangle r),
    求两个矩形相交后的矩形rectangle intersect_rect(rectangle r),注意没有交集的情况和嵌套的情况
    (4)编写main函数,声明两个Rectangle的变量,使用所定义的函数。
#include <iostream>
using namespace std;

class rectangle {
private:
    double x, y, w, h;

public:
    rectangle() {
        x = y = w = h = 0;
    }

    rectangle(double x, double y, double w, double h) {
        this->x = x;
        this->y = y;
        this->w = w;
        this->h = h;
    }

    void move(double offsetx, double offsety) {
        x += offsetx;
        y += offsety;
    }

    void zoom(double offsetw, double offseth) {
        w += offsetw;
        h += offseth;
        if (w < 0) { //宽度不能为负数
            w = 0;
        }
        if (h < 0) { //高度不能为负数
            h = 0;
        }
    }

    bool in_rect(double x, double y) {
        return (x >= this->x && x <= this->x + w && y >= this->y && y <= this->y + h);
    }

    rectangle union_rect(rectangle r) {
        double x1 = min(x, r.x);
        double y1 = min(y, r.y);
        double x2 = max(x + w, r.x + r.w);
        double y2 = max(y + h, r.y + r.h);
        return rectangle(x1, y1, x2 - x1, y2 - y1);
    }

    rectangle intersect_rect(rectangle r) {
        if ((x + w <= r.x) || (r.x + r.w <= x) || (y + h <= r.y) || (r.y + r.h <= y)) {
            // 无交集
            return rectangle();
        } else {
            double x1 = max(x, r.x);
            double y1 = max(y, r.y);
            double x2 = min(x + w, r.x + r.w);
            double y2 = min(y + h, r.y + r.h);
            return rectangle(x1, y1, x2 - x1, y2 - y1);
        }
    }
};

int main() {
    rectangle r1 = rectangle(2, 3, 5, 7);
    rectangle r2 = rectangle(6, 4, 8, 9);

    cout << "r1: (" << r1.x << ", " << r1.y << ", " << r1.w << ", " << r1.h << ")" << endl;
    cout << "r2: (" << r2.x << ", " << r2.y << ", " << r2.w << ", " << r2.h << ")" << endl;

    r1.move(1.5, 2.3);
    cout << "r1: after move (" << r1.x << ", " << r1.y << ", " << r1.w << ", " << r1.h << ")" << endl;

    r2.zoom(2, -1);
    cout << "r2: after zoom (" << r2.x << ", " << r2.y << ", " << r2.w << ", " << r2.h << ")" << endl;

    cout << "r1 contains (3, 5): " << (r1.in_rect(3, 5) ? "true" : "false") << endl;
    cout << "r2 contains (7, 5): " << (r2.in_rect(7, 5) ? "true" : "false") << endl;

    rectangle r3 = r1.union_rect(r2);
    cout << "union_rect: (" << r3.x << ", " << r3.y << ", " << r3.w << ", " << r3.h << ")" << endl;

    rectangle r4 = r1.intersect_rect(r2);
    if (r4.w > 0 && r4.h > 0) {
        cout << "intersect_rect: (" << r4.x << ", " << r4.y << ", " << r4.w << ", " << r4.h << ")" << endl;
    } else {
        cout << "No intersect_rect" << endl;
    }

    return 0;
}


#include<iostream>
using namespace std;

class Rectangle {
private:
    double x; //矩形左上角 x 坐标
    double y; //矩形左上角 y 坐标
    double w; //矩形宽度
    double h; //矩形高度
public:
    //有参和无参构造函数
    Rectangle(double x = 0, double y = 0, double w = 0, double h = 0) {
        this->x = x;
        this->y = y;
        this->w = w;
        this->h = h;
    }

    //移动矩形
    void move(double offsetx, double offsety) {
        this->x += offsetx;
        this->y += offsety;
    }

    //改变矩形大小
    void zoom(double offsetw, double offseth) {
        this->w += offsetw;
        this->h += offseth;
    }

    //判断一点是否在矩形内
    bool in_rect(double x, double y) {
        if (x >= this->x && x <= this->x + this->w && y >= this->y && y <= this->y + this->h) {
            return true;
        }else{
            return false;
        }
    }

    //求两个矩形合并后的矩形
    Rectangle union_rect(Rectangle r) {
        double min_x = min(this->x, r.x);
        double min_y = min(this->y, r.y);
        double max_x = max(this->x + this->w, r.x + r.w);
        double max_y = max(this->y + this->h, r.y + r.h);
        return Rectangle(min_x, min_y, max_x - min_x, max_y - min_y);
    }

    //求两个矩形相交后的矩形
    Rectangle intersect_rect(Rectangle r) {
        double min_x = max(this->x, r.x);
        double min_y = max(this->y, r.y);
        double max_x = min(this->x + this->w, r.x + r.w);
        double max_y = min(this->y + this->h, r.y + r.h);
        if (min_x >= max_x || min_y >= max_y) {
            //没有交集的情况
            return Rectangle(0, 0, 0, 0);
        }
        else if (max_x - min_x <= this->w && max_x - min_x <= r.w && max_y - min_y <= this->h && max_y - min_y <= r.h) {
            //嵌套的情况
            double center_x = (this->x + this->w + r.x + r.w) / 2;
            double center_y = (this->y + this->h + r.y + r.h) / 2;
            double width = abs(this->x - r.x) + max(this->w, r.w) - center_x + min(this->w, r.w);
            double height = abs(this->y - r.y) + max(this->h, r.h) - center_y + min(this->h, r.h);
            return Rectangle(center_x - width / 2, center_y - height / 2, width, height);
        }
        else {
            return Rectangle(min_x, min_y, max_x - min_x, max_y - min_y);
        }
    }
};

int main() {
    Rectangle r1(0, 0, 2, 2);
    Rectangle r2(1, 1, 2, 2);

    cout << "r1: (" << r1.in_rect(1.5, 1.5) << ")" << endl;
    cout << "r2: (" << r2.in_rect(0.5, 0.5) << ")" << endl;

    r1.zoom(1, 1);
    cout << "r1 zoom: (" << r1.in_rect(1.5, 1.5) << ")" << endl;

    r1.move(-1, -1);
    cout << "r1 move: (" << r1.in_rect(0.5, 0.5) << ")" << endl;

    Rectangle r3 = r1.union_rect(r2);
    cout << "rectangle union: (" << r3.in_rect(0.5, 0.5) << ")" << endl;

    Rectangle r4 = r1.intersect_rect(r2);
    cout << "rectangle intersect: (" << r4.in_rect(2, 2) << ")" << endl;

    Rectangle r5 = r1.intersect_rect(Rectangle(4, 4, 2, 2));
    cout << "rectangle intersect without overlap: (" << r5.in_rect(0.5, 0.5) << ")" << endl;

    return 0;
}

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答来自chatgpt:
    首先定义矩形类Rectangle:
    class Rectangle {
    private:
        double x, y, w, h;
    public:
        Rectangle();
        Rectangle(double x_, double y_, double w_, double h_);
        void move(double offsetx, double offsety);
        void zoom(double offsetw, double offseth);
        bool in_rect(double x_, double y_);
        Rectangle union_rect(Rectangle r);
        Rectangle intersect_rect(Rectangle r);
    };
    
    接下来实现各个函数:
    
    // 无参构造函数
    Rectangle::Rectangle() {}
    
    // 有参构造函数
    Rectangle::Rectangle(double x_, double y_, double w_, double h_) {
        this->x = x_;
        this->y = y_;
        this->w = w_;
        this->h = h_;
    }
    
    // 移动矩形
    void Rectangle::move(double offsetx, double offsety) {
        this->x += offsetx;
        this->y += offsety;
    }
    
    // 更改矩形大小
    void Rectangle::zoom(double offsetw, double offseth) {
        this->w += offsetw;
        this->h += offseth;
    }
    
    // 判断点是否在矩形内
    bool Rectangle::in_rect(double x_, double y_) {
        return x_ >= x && x_ <= x + w && y_ >= y && y_ <= y + h;
    }
    
    // 求两个矩形合并后的矩形
    Rectangle Rectangle::union_rect(Rectangle r) {
        double newx = min(this->x, r.x);
        double newy = min(this->y, r.y);
        double neww = max(this->x + this->w, r.x + r.w) - newx;
        double newh = max(this->y + this->h, r.y + r.h) - newy;
        return Rectangle(newx, newy, neww, newh);
    }
    
    // 求两个矩形相交后的矩形
    Rectangle Rectangle::intersect_rect(Rectangle r) {
        if ((this->x + this->w < r.x || this->x > r.x + r.w) || (this->y + this->h < r.y || this->y > r.y + r.h)) {
            // 没有交集
            return Rectangle(0, 0, 0, 0);
        }
        double newx = max(this->x, r.x);
        double newy = max(this->y, r.y);
        double neww = min(this->x + this->w, r.x + r.w) - newx;
        double newh = min(this->y + this->h, r.y + r.h) - newy;
        if (neww <= 0 || newh <= 0) {
            // 嵌套
            return Rectangle(0, 0, 0, 0);
        }
        return Rectangle(newx, newy, neww, newh);
    }
    
    最后在main函数中进行测试:
    
    int main() {
        Rectangle r1(0, 0, 3, 4);
        Rectangle r2(2, 2, 4, 5);
        r1.move(1, 1); // 移动矩形
        r2.zoom(1, 1); // 缩放矩形
        if (r1.in_rect(2, 3)) {
            cout << "Point is in rectangle 1" << endl; // 判断点是否在矩形内
        }
        Rectangle union_rect = r1.union_rect(r2); // 合并矩形
        Rectangle intersect_rect = r1.intersect_rect(r2); // 相交矩形
        return 0;
    }
    

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^