NEED HELP WITH A PROGRAM! I will sacrafice my first born to you if you can solve this

I need help finishing a program. I have been working on getting this working properly for the last 6 hours, to no avail. The idea is that the program will take in an unknown number of lines from a text file, with each line containing an X, a Y, a width, and a length, respectively. It will then load a vector with rectangle objects using those values, and then print out intersections between the rectangles. The instructor provided sample input and output, and I can't seem to get my output to match his (logic error). Please note that I am lazily using windows system calls to pause so if you are using mac or linux please feel free to change it to the appropriate call or if you are anal retentive change it to cin or scanf. Here is the sample input file:
12 30 50 50
02 02 20 40
33 33 02 02
00 00 15 15
99 99 01 01
And here is the sample output:
Rectangle 0 intersects with Rectangle 1,2,3
Rectangle 1 intersects with Rectangle 0,2
Rectangle 2 intersects with Rectangle 0,1
Rectangle 3 intersects with Rectangle 0
Rectangle 4 does not intersect with other Rectangles

Here is my code. The problem lies within the rectangle class's intersect function. Please, HELP!:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
class rectangle
{
public:
rectangle(int x, int y, int width, int length)
{
this->x=x;
this->y=y;
this->width=width;
this->length=length;
}
~rectangle()
{
}
bool intersects(rectangle &rect)
{
int refX1=rect.getX();
int refY1=rect.getY();
int refWidth=rect.getWidth();
int refLength=rect.getLength();
int refX2=refX1+refWidth;
int refY2=refY1+refLength;
int x1 = x;
int y1 = y;
int x2 = x+width;
int y2 = y+length;
return ((x2 >= refX1) &&

(y2 >= refY1) &&

(x1 <= refX2) &&

(y1 <= refY2));

//determine if they cross each other
}
int getX()
{
return x;
}
int getY()
{
return y;
}
int getWidth()
{
return width;
}
int getLength()
{
return length;
}
private:
int x;
int y;
int width;
int length;
};
int main()
{
vector <rectangle> rect;
string temp;
int x, y, width, length;
bool hasIntersect;
cout<<"Loading..."<<endl;
ifstream inputFile("project2.txt");
if(!inputFile.is_open())
cout<<"Could not open file!"<<endl;
while(inputFile.good()) //load rectangles
{
inputFile>>temp;
x=atoi(temp.c_str());
inputFile>>temp;
y=atoi(temp.c_str());
inputFile>>temp;
width=atoi(temp.c_str());
inputFile>>temp;
length=atoi(temp.c_str());
rect.push_back(rectangle(x,y,width,length));
cout<<"Loaded rectangle.."<<endl;
}
for(int i=0;i<rect.size();i++)
{
hasIntersect=false;
cout<<"Rectangle "<<i<<" intersects with ";
for(int j=0;j<rect.size();j++)
{
if(i!=j) //don't check if a rectangle intersects itself
{
if(rect[i].intersects(rect[j]))
{
hasIntersect=true;
cout<<j<<" ";
}
}
}
if(!hasIntersect)
cout<<"nothing."<<endl;
else
cout<<endl;
}
system("pause");
return 0;
} 
Last edited on
Can you please use the code format tag to format your code. It's impossible to read as it is.
done
Your input formatting was wrong, and you forgot to account for certain cases, but the main problem I thing you're having is that the example output does not match the example input (I checked this by drawing the rectangles on graph paper.) The correct output for
12 30 50 50
02 02 20 40
33 33 02 02
00 00 15 15
99 99 01 01

is
rect 0: 1 2
rect 1: 0 3
rect 2: 0
rect 3: 1
rect 4: nothing

Anyway, I fixed the rest of your problems:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class rectangle {
	public:
	rectangle(int x, int y, int width, int length){
		this->x=x;
		this->y=y;
		this->width=width;
		this->length=length;
	}
	~rectangle(){
	}
	bool intersects(rectangle &rect){
		int x2=rect.getX();
		int y2=rect.getY();
		int width2=rect.getWidth();
		int length2=rect.getLength();
		return x<x2 && x2<x+width && y<y2 && y2<y+length
			|| x2<x && x<x2+width2 && y2<y && y<y2+length2
			|| x2<x+width && x+width<x2+width2 && y2<y && y<y2+length2
			|| x2<x && x<x2+width2 && y2<y+length && y+length<y2+length2;
	}
	int getX(){
		return x;
	}
	int getY(){
		return y;
	}
	int getWidth(){
		return width;
	}
	int getLength(){
		return length;
	}
	private:
	int x;
	int y;
	int width;
	int length;
};

int main(){
	vector <rectangle> rect;
	string temp;
	int x, y, width, length;
	bool hasIntersect;
	cout<<"Loading..."<<endl;
	ifstream inputFile("project2.txt");
	if(!inputFile.is_open())
		cout<<"Could not open file!"<<endl;
	while(inputFile.good()){ //load rectangles
		getline(inputFile,temp);
		if(sscanf(temp.c_str(),"%d %d %d %d",&x,&y,&width,&length)<4)break;
		rect.push_back(rectangle(x,y,width,length));
		cout<<"Loaded rectangle.."<<endl;
		printf("%d %d %d %d\n",x,y,width,length);
	}
	for(int i=0;i<rect.size();i++){
		hasIntersect=false;
		cout<<"Rectangle "<<i<<" intersects with ";
		for(int j=0;j<rect.size();j++){
			if(i!=j){ //don't check if a rectangle intersects itself
				if(rect[i].intersects(rect[j])){
					hasIntersect=true;
					cout<<j<<" ";
				}
			}
		}
		if(!hasIntersect)
			cout<<"nothing."<<endl;
		else
			cout<<endl;
		}
		//system("pause");//do not do this, do this instead:
		getline(cin,temp);
		return 0;
} 

Anyway, don't worry about your firstborn, just burn some incense or something like a decent person.
Last edited on
Seeing as rocketboy9000 has already given you the answer, the test for intersection can be simplified, if you consider the conditions required for the rectangles to _not_ overlap...

return (! ( (x2 < refX1) || (y2 < refY1) || (refX2 < x1) || (refY2 < y1) ) );

That reads "intersection will NOT occur, if the right-hand side of the first rectangle is 'before' the left-hand side of the second rectangle, or the bottom of the first rectangle is 'before' the top of the second rectangle, or the right-hand side of the second rectangle is 'before' the left-hand side of the first rectangle, or the bottom of the second rectangle is 'before' the top of the first rectangle".
Last edited on
Topic archived. No new replies allowed.