COMP2113
COMP2113_ENGG1340 Programming technologies and Computer programming II [Section 2BC] [2023]
Loading...
Searching...
No Matches
bounding.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool mergeBoundingBoxes (int &x1, int &y1, int &w1, int &h1, int x2, int y2, int w2, int h2)
 

Function Documentation

◆ mergeBoundingBoxes()

bool mergeBoundingBoxes ( int & x1,
int & y1,
int & w1,
int & h1,
int x2,
int y2,
int w2,
int h2 )

Definition at line 9 of file 2.cpp.

9 {
10 // the bottom right corner of the first box
11 int x1_ = x1+w1;
12 int y1_ = y1+h1;
13 // the bottom right corner of the second box
14 int x2_ = x2+w2;
15 int y2_ = y2+h2;
16 bool overlap = false;
17 // previous assumption: x1,y1 is closer to the top left corner
18 // x2,y2 is closer to the bottom right corner
19 // however, this assumption is not always true as the question didn't state that
20 // test cases may provide different inputs, hence we need to check
21 // which corner is closer to the top left corner (0,0)
22 // x2 > x1 : check if the second box is more to the right
23 if ((x1_ > x2) && (y1_ > y2) && (x1 <= x2) && (y1 <= y2)){
24 overlap = true;
25 }
26 // if the second box is more to the top left corner,
27 // compare the bottom right corner of the first box with the top left corner of the second box
28 else if ((x2_ > x1) && (y2_ > y1) && (x1 >= x2) && (y1 >= y2)){
29 overlap = true;
30 }
31 else if ((x1_ > x2) && (y2_ > y1) && (x1 <= x2) && (y1 >= y2)){
32 overlap = true;
33 }
34 else if ((x2_ > x1) && (y1_ > y2) && (x1 >= x2) && (y1 <= y2)){
35 overlap = true;
36 }
37 // the width and height of the merged box
38 // note: the use of max() and min() here is important
39 // because the top left corner of the merged box is
40 // *NOT* necessarily be the top left corner of the first box
41 w1 = max(x1_,x2_)-min(x1,x2);
42 h1 = max(y1_,y2_)-min(y1,y2);
43 x1 = min(x1,x2);
44 y1 = min(y1,y2);
45 return overlap;
46}

Referenced by main().

Here is the caller graph for this function: