You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断最大矩形是否包含其他小矩形?C语言开发问询

C语言矩形生成与包含判断程序

需求

  • 生成3个坐标范围在(-50,50)的随机矩形,通过左上角与右下角坐标计算面积
  • 找出面积最大的矩形,判断其余矩形是否完全包含于其中(非部分重叠)

已完成代码(存在待完善项)

#include <stdio.h> 
#include <locale.h>

struct Point {
    int x;
    int y;
};

struct Rectangle {
    struct Point topLeft;
    struct Point botRight;
};

int Area(struct Rectangle r) {
    int length, breadth;
    length = r.botRight.x - r.topLeft.x;
    breadth = r.topLeft.y - r.botRight.y;

    return length * breadth;
}

int main() {
    srand(time(NULL));

    struct Rectangle r1, r2, r3;
    
    r1.topLeft.x  = -50 + rand() % 50;
    r1.topLeft.y  = -50 + rand() % 50;
    r1.botRight.x = -50 + rand() % 50;
    r1.botRight.y = -50 + rand() % 50;

    while (r1.botRight.x <= r1.topLeft.x) {
        r1.botRight.x = -50 + rand() % 50;
    }
    while (r1.topLeft.y <= r1.botRight.y) {
        r1.topLeft.y = -50 + rand() % 50;
    }
    printf("\t----------RECTANGLE 1----------\n");
    printf("\tTop left point is x = %d y = %d\n", r1.topLeft.x, r1.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", r1.botRight.x, r1.botRight.y);
    printf("\tArea is %d\n", Area(r1));
    
    r2.topLeft.x  = -50 + rand() % 50;
    r2.topLeft.y  = -50 + rand() % 50;
    r2.botRight.x = -50 + rand() % 50;
    r2.botRight.y = -50 + rand() % 50;

    while (r2.botRight.x <= r2.topLeft.x) {
        r2.botRight.x = -50 + rand() % 50;
    }
    while (r2.topLeft.y <= r2.botRight.y) {
        r2.topLeft.y = -50 + rand() % 50;
    }
    printf("\t----------RECTANGLE 2----------\n");
    printf("\tTop left point is x = %d y = %d\n", r2.topLeft.x, r2.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", r2.botRight.x, r2.botRight.y);
    printf("\tArea is %d\n", Area(r2));
    
    r3.topLeft.x = -50 + rand() % 50;
    r3.topLeft.y = -50 + rand() % 50;
    r3.botRight.x = -50 + rand() % 50;
    r3.botRight.y = -50 + rand() % 50;

    while (r3.botRight.x <= r3.topLeft.x) {
        r3.botRight.x = -50 + rand() % 50;
    }
    while (r3.topLeft.y <= r3.botRight.y) {
        r3.topLeft.y = -50 + rand() % 50;
    }
    printf("\t----------RECTANGLE 3----------\n");
    printf("\tTop left point is x = %d y = %d\n", r3.topLeft.x, r3.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", r3.botRight.x, r3.botRight.y);
    printf("\tArea is %d\n\n", Area(r3));
    
    if (Area(r1) >= Area(r2) && Area(r1) >= Area(r3))
        printf("\tRECTANGLE 1 HAS A BIGGEST AREA --> %d\n", Area(r1));
    
    if (Area(r2) >= Area(r1) && Area(r2) >= Area(r3))
        printf("\tRECTANGLE 2 HAS A BIGGEST AREA --> %d\n", Area(r2));

    if (Area(r3) >= Area(r1) && Area(r3) >= Area(r2))
        printf("\tRECTANGLE 3 HAS A BIGGEST AREA --> %d\n", Area(r3));
}

代码问题与完善方案

1. 基础问题修正

  • 缺少#include <time.h>:srand(time(NULL))依赖该头文件,必须添加
  • 随机坐标范围错误:原代码-50 + rand()%50生成的是[-50, -1]的整数,不符合(-50,50)的要求,应改为-50 + rand()%100,范围为[-50, 49],满足开区间要求
  • 重复代码冗余:封装生成、打印矩形的函数,避免重复逻辑

2. 补充包含判断功能

添加函数判断矩形r_small是否完全被r_large包含:

// 判断r_small是否完全被r_large包含
int isContained(struct Rectangle r_small, struct Rectangle r_large) {
    return (r_small.topLeft.x >= r_large.topLeft.x) &&
           (r_small.topLeft.y <= r_large.topLeft.y) &&
           (r_small.botRight.x <= r_large.botRight.x) &&
           (r_small.botRight.y >= r_large.botRight.y);
}

完善后的完整代码

#include <stdio.h> 
#include <locale.h>
#include <time.h>
#include <stdlib.h>

struct Point {
    int x;
    int y;
};

struct Rectangle {
    struct Point topLeft;
    struct Point botRight;
};

// 计算矩形面积
int Area(struct Rectangle r) {
    int length = r.botRight.x - r.topLeft.x;
    int breadth = r.topLeft.y - r.botRight.y;
    return length * breadth;
}

// 判断r_small是否完全被r_large包含
int isContained(struct Rectangle r_small, struct Rectangle r_large) {
    return (r_small.topLeft.x >= r_large.topLeft.x) &&
           (r_small.topLeft.y <= r_large.topLeft.y) &&
           (r_small.botRight.x <= r_large.botRight.x) &&
           (r_small.botRight.y >= r_large.botRight.y);
}

// 生成符合要求的随机矩形
void generateRectangle(struct Rectangle *r) {
    // 初始化坐标
    r->topLeft.x = -50 + rand() % 100;
    r->topLeft.y = -50 + rand() % 100;
    r->botRight.x = -50 + rand() % 100;
    r->botRight.y = -50 + rand() % 100;

    // 确保右下角x坐标大于左上角x坐标
    while (r->botRight.x <= r->topLeft.x) {
        r->botRight.x = -50 + rand() % 100;
    }
    // 确保左上角y坐标大于右下角y坐标(符合坐标系习惯)
    while (r->topLeft.y <= r->botRight.y) {
        r->topLeft.y = -50 + rand() % 100;
    }
}

// 打印矩形信息
void printRectangle(struct Rectangle r, int index) {
    printf("\t----------RECTANGLE %d----------\n", index);
    printf("\tTop left point is x = %d y = %d\n", r.topLeft.x, r.topLeft.y);
    printf("\tBottom right point is x = %d y = %d\n", r.botRight.x, r.botRight.y);
    printf("\tArea is %d\n", Area(r));
}

int main() {
    srand(time(NULL));
    setlocale(LC_ALL, "");

    struct Rectangle r1, r2, r3;
    struct Rectangle *largestRect;
    int largestArea;

    // 生成三个矩形
    generateRectangle(&r1);
    generateRectangle(&r2);
    generateRectangle(&r3);

    // 打印矩形信息
    printRectangle(r1, 1);
    printRectangle(r2, 2);
    printRectangle(r3, 3);
    printf("\n");

    // 找出面积最大的矩形
    largestArea = Area(r1);
    largestRect = &r1;
    if (Area(r2) > largestArea) {
        largestArea = Area(r2);
        largestRect = &r2;
    }
    if (Area(r3) > largestArea) {
        largestArea = Area(r3);
        largestRect = &r3;
    }

    printf("\tRECTANGLE %d HAS THE BIGGEST AREA --> %d\n", 
           (largestRect == &r1) ? 1 : (largestRect == &r2) ? 2 : 3, largestArea);

    // 判断其余矩形是否被最大矩形包含
    if (largestRect == &r1) {
        printf("\tRectangle 2 is %s in Rectangle 1\n", isContained(r2, r1) ? "completely contained" : "not contained");
        printf("\tRectangle 3 is %s in Rectangle 1\n", isContained(r3, r1) ? "completely contained" : "not contained");
    } else if (largestRect == &r2) {
        printf("\tRectangle 1 is %s in Rectangle 2\n", isContained(r1, r2) ? "completely contained" : "not contained");
        printf("\tRectangle 3 is %s in Rectangle 2\n", isContained(r3, r2) ? "completely contained" : "not contained");
    } else {
        printf("\tRectangle 1 is %s in Rectangle 3\n", isContained(r1, r3) ? "completely contained" : "not contained");
        printf("\tRectangle 2 is %s in Rectangle 3\n", isContained(r2, r3) ? "completely contained" : "not contained");
    }

    return 0;
}

说明

  • 封装generateRectangle、printRectangle函数,减少代码冗余,提升可读性
  • 修正随机坐标范围问题,确保坐标落在(-50,50)区间内
  • isContained函数通过对比边界坐标,精准判断矩形完全包含关系
  • 优化最大矩形查找逻辑,避免重复计算面积

内容的提问来源于stack exchange,提问作者dan1chX

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 19:31:36