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;
}
|