Determine if two rectangles overlap each other?

前端 未结 23 834
礼貌的吻别
礼貌的吻别 2020-11-22 04:09

I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectang

23条回答
  •  感情败类
    2020-11-22 04:59

    I have implemented a C# version, it is easily converted to C++.

    public bool Intersects ( Rectangle rect )
    {
      float ulx = Math.Max ( x, rect.x );
      float uly = Math.Max ( y, rect.y );
      float lrx = Math.Min ( x + width, rect.x + rect.width );
      float lry = Math.Min ( y + height, rect.y + rect.height );
    
      return ulx <= lrx && uly <= lry;
    }
    

提交回复
热议问题