#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
class Triangle
{
public:
Triangle();
~Triangle();
float area(float coordx1, float coordx2, float coordx3, float coordy1, float coordy2, float coordy3)
{
return abs((coordx1*(coordy2 - coordy3) + coordx2*(coordy3-coordy1) + coordx3*(coordy1-coordy2)) / 2.0);
}
void isIn(float coord1, float coord2)
{
float A = area (x1, x2, x3, y1, y2, y3);
float A1 = area (coord1, coord2, x3, y1, y2, y3);
float A2 = area (x1, x2, coord1, coord2, y2, y3);
float A3 = area (x1, x2, x3, y1, coord1, coord2);
if(A == A1 + A2 + A3)
{
cout << x1 << x2 << x3 << y1 << y2 << y3;
}
}
float x1, x2, x3, y1, y2, y3;
};
class Circle
{
public:
Circle();
~Circle();
void isIn(float coord1, float coord2)
{
if(((coord1 - x)*(coord1 - x) + (coord2 - y)*(coord2 - y)) < rad*rad)
{
cout << "It's in" << x << y << rad;
}
}
float x, y, rad;
};
class Rectangle
{
public:
Rectangle();
~Rectangle();
void isIn(float coord1, float coord2)
{
if((x1 < coord1 < x2 || x2 < coord1 < x2) && (y1 < coord2 < y2 || y2 < coord2 < y2))
{
cout << "It's in" << x1 << x2 << y1 << y2;
}
}
float x1, x2, y1, y2;
};
int isSame(char* str)
{
if(str[0]=='t')
return 1;
if(str[0]=='c')
return 2;
if(str[0]=='r')
return 3;
}
int main()
{ Triangle *objT[50];
Circle *objC[50];
Rectangle *objR[50];
char polygon[10];
int knt1 = 0, knt2 = 0, knt3 = 0;
float pointx, pointy;
cout << "Enter point which has x= ";
cin >> pointx;
cout << "and y= ";
cin >> pointy;
ifstream polygonfile;
polygonfile.open("D:\\OOP\\domashno_5_2\\dom5\\polygons.txt");
if(polygonfile.is_open())
{
while(!polygonfile.eof())
{
polygonfile >> polygon;
if(isSame(polygon) == 1)
{
polygonfile >> objT[knt1]->x1 >> objT[knt1]->x2 >> objT[knt1]->x3 >> objT[knt1]->y1 >> objT[knt1]->y2 >> objT[knt1]->y3;
knt1 += 1;
}
if(isSame(polygon) == 2)
{
polygonfile >> objC[knt2]->x >> objC[knt2]->y >> objC[knt2]->rad;
knt2 += 1;
}
if(isSame(polygon) == 3)
{
polygonfile >> objR[knt3]->x1 >> objR[knt3]->x2 >> objR[knt3]->y1 >> objR[knt3]->y2;
knt3 += 1;
cout << objR[knt3]->x1;
}
}
}
polygonfile.close();
for (int i=0; i<knt1; i++)
{
objT[i]->isIn(pointx, pointy);
}
for (int i=0; i<knt2; i++)
{
objC[i]->isIn(pointx, pointy);
}
for (int i=0; i<knt3; i++)
{
objR[i]->isIn(pointx, pointy);
}
return 0;
}
I have to see if the point which you want is in one or more of the polygons
this is the file
circle 3.0 7.5 3.15
triangle 1.0 1.0 2.0 1.0 1.0 2.0
rect -1.0 -1.0 2.0 2.0
Last edited on