#include <iostream.h> #include <conio.h> #include <math.h> class TFigure { //площадь для треугольника float Square(int *m) { int j=0,s=0; for(int i=0;i<3;i++) { if(i==2) j=0; else j++; s+=( (m[2*i]-m[2*j])*(m[2*i+1]+m[2*j+1]) ); } return fabs(s/2); }; public: //конструктор TFigure(int numPoints, const char *s = ""):name(s), ptsAmount(numPoints) { pts = new TPoint[ptsAmount]; cout << name << " created ..." << endl; for(int i = 0; i < ptsAmount; i++) { TPoint p; cout << "Enter coordinates of apex" << i+1 << ":"; cin >> p.x >> p.y; pts[i] = p; } } //деструктор virtual ~TFigure() { delete [] pts; } //дружественная функция для обращения к pts friend bool IsIntersect(TFigure *T1,TFigure *T2); //проверка,находится ли точка (х,у) внутри границ bool IsInside(int x,int y) { int s=0,j=0,mas[6]; mas[0]=x;mas[1]=y; //находим площадь каждого внутреннего треугольника for(int i=0;i<ptsAmount;i++) { if(i==ptsAmount-1){j=0;}else{j++;}; mas[2]=pts[i].x; mas[3]=pts[i].y; mas[4]=pts[j].x; mas[5]=pts[j].y; s+=Square(mas); } //учтём погрешность if (fabs(GetSquare()-s)<0.001) return true; else return false; } //площадь для любого многоугольника float GetSquare() { int j=0,s=0; for(int i=0;i<ptsAmount;i++) { if(i==ptsAmount-1){j=0;} else {j++;} s+=( (pts[i].x-pts[j].x)*(pts[i].y+pts[j].y) ); } return fabs(s/2); }; //перемещение void move_it(int step_x, int step_y) { cout << "How will be move " << name <<"?" << endl; cout << "x:";cin >> step_x; cout << "y:";cin >> step_y; for(int i = 0; i < ptsAmount; i++) { pts[i].x += step_x; pts[i].y += step_y; } } //вывод позиции фигуры void show_position() { cout << name ; for(int i = 0;i < ptsAmount; i++) cout << " (" << pts[i].x << ";" << pts[i].y << ") "; cout << endl; } struct TPoint { TPoint(int px=0,int py=0):x(px),y(py) { } int x, y; }; private: const char *name;TPoint *pts; protected: int ptsAmount; }; //класс треугольника class TTriangle: public TFigure { public: TTriangle(int numPoints = 3) : TFigure(numPoints, "triangle") { } }; //класс прямоугольника class TRectangle: public TFigure { public: TRectangle(int numPoints = 4): TFigure(numPoints, "rectangle") { } }; //проверка фигур на факт пересечения bool IsIntersect(TFigure *T1,TFigure *T2) { bool flag=false; for(int i=0;i<T2->ptsAmount;i++) if(T1->IsInside(T2->pts[i].x,T2->pts[i].y)) {flag=true;break;}; return flag; } void show_actions() { cout << "1 : Show position" << endl;; cout << "2 : Move the object" << endl; cout << "3 : Check the intersecion" << endl; cout << "4 : Get square" << endl; cout << "5 : Info" << endl; cout << "6 : Exit from program" << endl; cout << endl; } int main() { TTriangle T1; TRectangle T2; if (IsIntersect(&T1,&T2)) cout << "Yes" ; else cout << "No";getche(); }