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

矩形相交判断:无交集时输出‘empty’的实现咨询

Got it, let's work through this problem step by step. Your current intersection code has two key flaws: it doesn't account for rectangles with negative width/height (which happen when the rectangle extends left or upward from its x/y origin), and it doesn't check whether the computed "intersection" is actually a valid, non-empty rectangle. Here's how to fix both issues:

How to Handle Empty Rectangle Intersections & Negative Dimensions

Step 1: Standardize Rectangles First

First, we need to normalize any rectangle that has negative width or height. For a rectangle with negative width, its right edge is actually to the left of the x coordinate—so we adjust x to be the leftmost edge and set width to a positive value. The same logic applies to negative height:

void standardize(rectangle &r) {
    if (r.w < 0) {
        r.x += r.w;
        r.w = -r.w;
    }
    if (r.h < 0) {
        r.y += r.h;
        r.h = -r.h;
    }
}

(Note: Adjust the syntax to match your programming language if needed—this example uses C++-like syntax.)

Step 2: Calculate Valid Intersection Bounds

Once both rectangles are standardized (so x/y are the top-left corner, and w/h are positive), compute the intersection's boundaries properly:

  • Left edge: max(r1.x, r2.x)
  • Top edge: max(r1.y, r2.y)
  • Right edge: min(r1.x + r1.w, r2.x + r2.w)
  • Bottom edge: min(r1.y + r1.h, r2.y + r2.h)

Step 3: Check for Empty Intersection

If the left edge is greater than or equal to the right edge, or the top edge is greater than or equal to the bottom edge, there's no overlapping area—we print "empty" in this case. Otherwise, we construct the valid intersection rectangle.

Full Modified Code

Here's the updated function that handles both edge cases:

rectangle intersection(rectangle r1, rectangle r2) {
    // Standardize both rectangles to fix negative dimensions
    standardize(r1);
    standardize(r2);
    
    // Calculate intersection boundaries
    int inter_x = max(r1.x, r2.x);
    int inter_y = max(r1.y, r2.y);
    int inter_right = min(r1.x + r1.w, r2.x + r2.w);
    int inter_bottom = min(r1.y + r1.h, r2.y + r2.h);
    
    // Check if intersection is empty
    if (inter_x >= inter_right || inter_y >= inter_bottom) {
        printf("empty");
        // Return a dummy empty rectangle (adjust based on your needs)
        rectangle empty = {0, 0, 0, 0};
        return empty;
    }
    
    // Construct and return the valid intersection
    rectangle inter;
    inter.x = inter_x;
    inter.y = inter_y;
    inter.w = inter_right - inter_x;
    inter.h = inter_bottom - inter_y;
    return inter;
}

Testing with Your Example

Let's plug in your specific rectangle values:

  • r1: x=2, y=3, w=5, h=6 (already standardized; right edge = 2+5=7, bottom edge=3+6=9)
  • r2: x=0, y=7, w=-4, h=2 → after standardization: x=0 + (-4) = -4, w=4, y=7, h=2 (right edge=-4+4=0, bottom edge=7+2=9)

Calculations:

  • inter_x = max(2, -4) = 2
  • inter_y = max(3,7) =7
  • inter_right = min(7,0) =0
  • inter_bottom = min(9,9)=9

Since 2 >= 0, we print "empty"—which matches your observation that there's no intersection between the two rectangles.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:16:36