NEED HELP WITH A PROGRAM!

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). 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!:
#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;
}
BUMP...willing to become someone's slave for help with this ;-)
Draw the rectangles on paper and see if your output is correct.
Topic archived. No new replies allowed.