这是一个非常有效的办法,还灵活运用了STL:
#include
#ifndef rectange_H
#define rectange_H
struct rectange{
char status;
int tatus;
double cx;
double cy;
double base;
double haut; //parametre de rectange
rectange(char sta='+',double cx=0,double
cy=0,double ba=0,double ha=0) {
if('+'==sta)
this->tatus=1;
else
this->tatus=-1;
this->status=sta;
this->cx=cx;
this->cy=cy;
this->base=ba;
this->haut=ha;
}
rectange(const rectange& rect) {
this->tatus=rect.tatus;
this->status=rect.status;
this->cx=rect.cx;
this->cy=rect.cy;
this->base=rect.base;
this->haut=rect.haut;
}
void Erectange(char sta='+',double cx=0,double
cy=0,double ba=0,double ha=0) {
if('+'==sta)
this->tatus=1;
else
this->tatus=-1;
this->status=sta;
this->cx=cx;
this->cy=cy;
this->base=ba;
this->haut=ha;
}
};
#endif
#include
#include
#include
#include
#include
#include "rectange.hpp"
#include
#ifndef app_H
#define app_H
using namespace std;
class myapp {
private:
list
relist;
list
resortie;
public:
myapp(){};
bool verifiefichier(string nom);
void lirefichiere();
void pushsortie(rectange
&);
void decompose(rectange &,
rectange &);
void traiter();
int sortie();
bool intersection(rectange &re1,
rectange &re2);
bool pointdansrect(rectange
&rect, double px, double py);
int xject(rectange re1, rectange re2, double
px);
int yject(rectange re1, rectange re2, double
py);
bool vdc(rectange re1, rectange re2, double px,
double py);
void
affiche(list
&);
};
//difinir tous les fonction en dorde la classe
void myapp::lirefichiere() {
char status='+';
double cx, cy, ba, ha;
string nomdefichier, lstr;
stringstream istr;
ifstream fich;
rectange *ptrect;
bool vf=false;
//obtenir les noms des fichiers
while (vf==false) {
cout
<< "Entrez le nom
de fichier pour analser:\n";
cin
>> nomdefichier;
vf =
verifiefichier(nomdefichier); //ouvir la fichier
}
fich.open(nomdefichier.c_str(), ios::in);
//ouvir la fichier
if (!fich)
cout
<
<
while (getline(fich, lstr)) {
istr.clear();
istr<
istr
>> status;
istr
>>cx;
istr
>> cy;
istr
>> ba;
istr
>> ha;
ptrect= new rectange(status,
cx, cy, ba, ha);
relist.push_back(*ptrect);
}
fich.close();
}
bool myapp::verifiefichier(string nom) {
// cout
<
<
ifstream infich;
infich.open(nom.c_str(), ios::in);
if (!infich) {
cout
<< "Echec de ouvir de fichier: "
<< nom
<< endl;
return false;
}
//si vrai, retourse true
infich.close();
return true;
}
//verifie deux rectange ont intersection
bool myapp::intersection(rectange &re1, rectange
&re2) {
bool vf, f1, f2;
double d1, d2;
d1=fabs(re1.cx -re2.cx);
d2=(re1.base+re2.base)/2;
f1=(d1
d1=fabs(re1.cy -re2.cy);
d2=(re1.haut+re2.haut)/2;
f2=(d1
vf=f1&&f2;
if (vf)
return true;
else
return false;
}
//verifie un point dans rectange
bool myapp::pointdansrect(rectange &rect, double
px, double py) {
bool f1, f2;
double d1, d2;
d1=fabs(rect.cx -px);
d2=(rect.base)/2;
f1=(d1 <= d2);
d1=fabs(rect.cy -py);
d2=(rect.haut)/2;
f2=(d1<=d2);
if (f1&&
f2)
return true;
else
return false;
}
//valeur projective en cordonne x
int myapp::xject(rectange re1, rectange re2, double px) {
bool f1, f2;
int re;
double d1, d2;
d1=fabs(re1.cx -px);
d2=(re1.base/2);
f1= (d1
d1=fabs(re2.cx -px);
d2=re2.base/2;
f2= (d1
if (f1 && f2)
{
re=re1.tatus;
re +=re2.tatus;
return re;
} else if (f1)
return (re1.tatus);
else if (f2)
return (re2.tatus);
else
return 0;
}
//valeur projective en cordonne x
bool myapp::vdc(rectange re1, rectange re2, double px, double py)
{
bool f1, f2, re=false;
double d1, d2, d3, d4;
d1=fabs(re1.cx -px);
d2=(re1.base/2);
d3=fabs(re1.cy -py);
d4=re1.haut/2;
f1= (d1>d2)
||(d3>d4);
d1=fabs(re2.cx -px);
d2=(re2.base/2);
d3=fabs(re2.cy -py);
d4=re2.haut/2;
f2= (d1>d2)
||(d3>d4);
if (f1 && f2)
{
re=true;}
return re;
}
//valeur projective en cordonne y
int myapp::yject(rectange re1, rectange re2, double py) {
bool f1, f2;
double d1, d2;
d1=fabs(re1.cy -py);
d2=re1.haut/2;
f1= (d1
d1=fabs(re2.cy -py);
d2=re2.haut/2;
f2= (d1
if (f1 &&
f2)
return (re1.tatus
+re2.tatus);
else if (f1)
return (re1.tatus);
else if (f2)
return (re2.tatus);
else
return 0;
}
//push les petite rectange
void myapp::pushsortie(rectange &rept) {
bool bl=false;
list
::iterator it2=resortie.begin();
if (resortie.empty()) {
resortie.push_back(rept);
return;
}
// affiche(resortie);
for (it2=resortie.begin(); it2!=resortie.end();)
{
if (!intersection(rept, *it2))
{
it2++;
} else {
rectange
re(*it2);
resortie.erase(it2);
decompose(rept,
re);
bl=true;
break;
}
}
if (!bl)
resortie.push_back(rept);
return;
}
void myapp::affiche(list
&rl) {
list
::iterator it=rl.begin();
if (rl.size()==0) {
cout<<
"LIST est vide..."<
return;
}
for (it=rl.begin(); it!=rl.end(); ++it) {
cout
<< it->status
<tatus<<
"cx=" << it->cx;
cout
<cy
<base
<
<haut<
}
return;
}
void myapp::decompose(rectange &ree1, rectange
&ree2) {
//la plus petite valeur de X
// cout <
decompose....." << endl;
rectange re1(ree1);
rectange re2(ree2);
char status='+';
double vx, vy, vxb, vyb, vc;
double cx, cy, ba, ha; //pour un rectange
double xl, xh, yl, yh; //pour range
double x, y, xb, yb; //pour boucle
xl=((re1.cx-re1.base/2)
? (re1.cx -re1.base/2)
:
(re2.cx-re2.base/2);
//la plus grande valeur de x
xh=((re1.cx+re1.base/2)
? (re2.cx +re2.base/2)
:
(re1.cx+re1.base/2);
//la plus petite valeur de y
yl=((re1.cy-re1.haut/2)
? (re1.cy -re1.haut/2)
:
(re2.cy-re2.haut/2);
//la plus grande valeur de y
yh=((re1.cy+re1.haut/2)
? (re2.cy +re2.haut/2)
:
(re1.cy+re1.haut/2);
vxb=xject(re1, re2, xl);
xb=xl;
yb=yl;
for (x=xl; x<= xh; x=
round(100*(x+(xh-xl)/250))/100) {
vx=xject(re1, re2, x);
if (vx==vxb
&& x
continue;
vyb=yject(re1, re2, yl);
for (y=yl;
y<=yh; y=round(100*(y+(yh-yl)/250))/100) {
vy=0;
if
(y
vy=
yject(re1, re2, y);
else
vy=vyb;
if (vy==vyb
&& y
continue;
ba=round(10*fabs(x-xb))/10;
ha=round(10*fabs(y-yb))/10;
cx=round(10*(xb+ba/2))/10;
cy=round(10*(yb+ha/2))/10;
vc=xject(re1,
re2, cx)+yject(re1, re2, cy);
if (vdc(re1,
re2, cx, cy)==true)
vc=0;
if (vc!=0
&& ba!=0
&& ha!=0) {
if
(vc<0)
status='-';
else
status='+';
rectange
*pt= new rectange(status,cx,cy,ba,ha);
pushsortie(*pt);//resortie.push
}
vyb =
vy;
yb = y;
}
yb=yl;
xb=x;
vxb=xject(re1, re2, xb);
}
}
void myapp::traiter() {
// rectange &pt1,&pt2;
if (relist.size()==1) {
rectange &pt =
relist.front();
relist.pop_front();
pushsortie(pt);
return;
}
rectange &pt1 =
relist.front();
relist.pop_front();
rectange &pt2 =
relist.front();
if (intersection(pt1, pt2))
decompose(pt1, pt2);
else {
pushsortie(pt1);
pushsortie(pt2);
}
relist.pop_front();
if (relist.size()>0)
traiter();
else
return;
}
int myapp::sortie() {
double area, circon;
double cx, cy; //pour un rectange
int deligne=0;
char ch='-';
list
::iterator it1=resortie.begin();
list
::iterator it2=resortie.begin();
if (resortie.size()==0) {
cout
<
<
return 0;
}
//enlever des rectange vide
for (list
::iterator it2=resortie.begin(); it2 != resortie.end();) {
if
(ch==(it2->status)) {
it2=resortie.erase(it2);
} else {
it2++;
}
}
//calculer area
area=0.0;
it2=resortie.begin();
for (; it2!=resortie.end(); ++it2) {
area +=
it2->base * (it2->haut);
}
cout << "Le valeur
de area est:" <
<< endl;
circon=0;
for (it1=resortie.begin(); it1!=resortie.end();
++it1) {
deligne=0;
it2=resortie.begin();
//cx,cy -point
de milieu
cx= it1->cx -
it1->base/2;
cy= it1->cy;
for (it2=resortie.begin();
it2!=resortie.end(); ++it2) {
if
(pointdansrect(*it2, cx, cy))
deligne++;
}
if (deligne==1)
circon +=
it1->haut;
deligne=0;
it2=resortie.begin();
cx=it1->cx;
cy=it1->cy+
(it1->haut)/2;
for (it2=resortie.begin();
it2!=resortie.end(); ++it2) {
if
(pointdansrect(*it2, cx, cy))
deligne++;
}
if (deligne==1)
circon +=
it1->base;
deligne=0;
it2=resortie.begin();
cx=it1->cx+it1->base/2;
cy=it1->cy;
for (it2=resortie.begin();
it2!=resortie.end(); ++it2) {
if
(pointdansrect(*it2, cx, cy))
deligne++;
}
if (deligne==1)
circon +=
it1->haut;
deligne=0;
it2=resortie.begin();
cx=it1->cx;
cy=it1->cy-it1->haut/2;
for (it2=resortie.begin();
it2!=resortie.end(); ++it2) {
if
(pointdansrect( *it2, cx, cy))
deligne++;
}
if (deligne==1)
circon +=
it1->base;
}
cout
<
<
<< endl;
return 0;
}
#endif
//============================================================================
#include
#include "tp2.hpp"
using namespace std;
int main(){
myapp ap1;
ap1.lirefichiere();
// cout <
lirefichiere.." <
ap1.traiter();
// cout <
traiter.." <
ap1.sortie();
// cout <
sortie.." <
return 1;
}