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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
struct A{ //stores the pairs of points
int x;
int y;
};
int N;
int sx=10000,sy=10000; //Coordinates of the smallest point.
vector <A> mass;
int used[100];
bool CMP(A i,A j) //Which point is bigger by sin
{
if((i.x-sx)/(sqrt(pow(i.x-sx,2)+pow(i.y-sy,2)))>(j.x-sx)/(sqrt(pow(j.x-sx,2)+pow(j.y-sy,2))))
return 1;
else return 0;
}
// Function that calculates the absolute value
// of a double type.
double MyAbs(double num)
{
double inv = num * -1;
return (num <= 0) ? inv : num;
}
// Function that calculates the area.
//Given vector of points in the XY plane.
double CalcArea(vector<pair<double, double > > list)
{
double area = 0; // Total Area
double diff = 0; // Difference Of Y{i + 1} - Y{i - 1}
unsigned int last = list.size() - 1; // Size Of Vector - 1
//That loop goes to all points from 2 to n-1.
for(unsigned int i = 1; i < last; i++)
{
diff =list[i + 1].second - list[i - 1].second;
area += list[i].first * diff;
}
//Now we consider the points 1 and n.
diff = list[1].second - list[last].second;
area += list[0].first * diff; //
diff = list[0].second - list[last - 1].second;
area += list[last].first * diff;
/* Calculate The Final Answer */
area = 0.5 * MyAbs(area);
return area; // Return The Area
}
int main()
{
N=5;
double it;
int i;
for(i=1;i<=N;i++)
{ A temp;
cin>>temp.x>>temp.y;
mass.push_back(temp);
if(sy>mass[mass.size()-1].y)
{
it=mass.size()-1;
sy=mass[mass.size()-1].y;
sx=mass[mass.size()-1].x;
}
if(mass[mass.size()-1].y==sy&&mass[mass.size()-1].x<sx)
{
it=mass.size()-1;
sx=mass[mass.size()-1].x;
}
}
mass.erase(mass.begin()+it);
sort(mass.begin(),mass.end(),CMP);
cout<<endl<<endl<<endl<<endl;
for(i=0;i<mass.size();i++)
cout<<mass[i].x<<" "<<mass[i].y<<endl;//Here it outputs the points which creates the external
//line of the polygon.
vector<A> stack;
A temp;
temp.x=sx;
temp.y=sy;
stack.push_back(temp);
stack.push_back(mass[0]);
for(i=1;i<mass.size();i++)
{
int x1,x2,x3,y1,y2,y3;
int status=0;
while(status==0)
{
x3=mass[i].x;
y3=mass[i].y;
x1=stack[stack.size()-2].x;
y1=stack[stack.size()-2].y;
x2=stack[stack.size()-1].x;
y2=stack[stack.size()-1].y;
if((x1*y3+y1*x2+x3*y2)-(x2*y3+y2*x1+x3*y1)>0)
stack.pop_back();
else
{
stack.push_back(mass[i]);
status=1;
}
}
}
for(i=0;i<stack.size();i++)
cout<<stack[i].x<<" "<<stack[i].y<<endl;
double x = 0;
double y = 0;
vector< pair<double, double> > v;
int mm=mass.size();
int nn=stack.size();
while(stack.size() > 0)
{
for (i=0;i<=stack.size();i++)
v.push_back(make_pair(x[i], y[i]));// here I want to push back the pairs x and y into V
nn--; //which goes ti the function that computes the area.
}
while(mass.size() > 0)
{
for (i=0;i<=mass.size();i++)
v.push_back(make_pair(x[i], y[i]));// here I want to push back the pairs x and y into V
mm--; //which goes ti the function that computes the area.
}
cout<<"The Area Of The Polygon Is: " << CalcArea(v) <<endl;
system ("pause");
return 0;
}
|