POLYGON
#include <cmath>
point::point()
{x=y=0;}
void point::get()
{cin>>x>>y;}
ostream& operator<<(ostream& Out,point p)
{
Out<<p.x<<" "<<p.y<<endl;
return Out;
}
void outofrange::what()
{
cout<<"Out of range";
}
polygon::polygon(int n)
{
num_Of_Ver=n;
vertices=new point[n];
}
polygon::~polygon()
{
delete(vertices);
}
void polygon::get()
{
vertices=new point[num_Of_Ver];
for(int i=0;i<num_Of_Ver;i++)
cin>>(vertices+i)->x>>(vertices+i)->y;
}
point& polygon::operator[](int idx)
{
if(idx>num_Of_Ver)
throw outofrange();
return *(vertices+idx);
}
double polygon::area()
{
double a;
for(int i=0;i<num_Of_Ver;i++)
{
a+=(vertices+i)->x*((vertices+(i+1)%num_Of_Ver)->y);
a-=(vertices+i)->y*((vertices+(i+1)%num_Of_Ver)->x);
}
return abs(a/2);
}
Design an class polygon to represent a polygon in a two dimensional space. Provide member functions to get the details of the polygon and compute area
UML:
CODE:
point::point()
{x=y=0;}
void point::get()
{cin>>x>>y;}
ostream& operator<<(ostream& Out,point p)
{
Out<<p.x<<" "<<p.y<<endl;
return Out;
}
void outofrange::what()
{
cout<<"Out of range";
}
polygon::polygon(int n)
{
num_Of_Ver=n;
vertices=new point[n];
}
polygon::~polygon()
{
delete(vertices);
}
void polygon::get()
{
vertices=new point[num_Of_Ver];
for(int i=0;i<num_Of_Ver;i++)
cin>>(vertices+i)->x>>(vertices+i)->y;
}
point& polygon::operator[](int idx)
{
if(idx>num_Of_Ver)
throw outofrange();
return *(vertices+idx);
}
double polygon::area()
{
double a;
for(int i=0;i<num_Of_Ver;i++)
{
a+=(vertices+i)->x*((vertices+(i+1)%num_Of_Ver)->y);
a-=(vertices+i)->y*((vertices+(i+1)%num_Of_Ver)->x);
}
return abs(a/2);
}
No comments:
Post a Comment