typedef struct CvPoint{
int x,
int y
}CvPoint;
//点是否在多边形里面
bool PtInPolygon(CvPoint p,CvPoint* ptpolygon,int nCount)
{
int nCross=0;
for (int i=0;i < nCount;i++)
{
CvPoint p1=ptpolygon[i];
CvPoint p2=ptpolygon[(i + 1) % nCount];
//求解 y=p.y 与 p1p2 的交点
if (p1.y == p2.y) //p1p2与y=po.y平行
continue;
if (p.y < min(p1.y,p2.y)) //交点在p1p2延长线上
continue;
if (p.y >=max(p1.y,p2.y))//交点在p1p2延长线上
continue;
//求交点的x坐标
double x=(double)(p.y - p1.y) * (double)(p2.x - p1.x) / (double)(p2.y - p1.y) + p1.x;
if (x>p.x)
nCross++; //只统计单边交点
}
return (nCross % 2 == 1);
}
// 计算多边形面积
double PolygonArea(CvPoint* pVertex, int NUM){
double fArea = 0;
for(int i = 0; i < NUM - 1; i++){
fArea += (0.5 * (pVertex[i].x * pVertex[i + 1].y - pVertex[i].y * pVertex[i + 1].x));}
return fArea;
}
//计算四边形的面积
double SquArea(int x1,int y1,int x2, int y2, int x3,int y3,int x4,int y4)
{
double T_area1,T_area2,S_area;
T_area1=TrianglArea( x1,y1,x2,y2, x3,y3);
T_area2=TrianglArea( x1,y1,x4,y4, x3,y3);
S_area=T_area1+T_area2;
return S_area;
}
//计算三角型面积的函数
double TrianglArea( int x1,int y1,int x2, int y2, int x3,int y3)
{
double a,b,c,s,T_area;
a=sqrt((double)(pow((double)(x2-x1),2)+pow((double)(y2-y1),2)));
b=sqrt((double)(pow((double)(x3-x2),2)+pow((double)(y3-y2),2)));
c=sqrt((double)(pow((double)(x3-x1),2)+pow((double)(y3-y1),2)));
if((a+b>c)&&(a+c>b)&&(b+c>a))
{
s=(a+b+c)/2;
T_area=sqrt(s*(s-a)*(s-b)*(s-c));
}
else
//不能构成三角形
T_area=0.;
return T_area;
}
//计算两边之和减去第三边
double Distance2(int x1,int y1,int x2, int y2, int x3,int y3)
{
double T_distance,a,b,c;
a=sqrt((double)(pow((double)(x2-x1),2)+pow((double)(y2-y1),2)));//质心到两点的距离 a,b
b=sqrt((double)(pow((double)(x3-x1),2)+pow((double)(y3-y1),2)));
c=sqrt((double)(pow((double)(x3-x2),2)+pow((double)(y3-y2),2)));
T_distance=a+b-c;
return T_distance;
}
bool Direcetion(CvPoint P1,CvPoint P2,CvPoint P3)
{
int a = 0,b = 0,c = 0;
int x1,y1;
if(P2.y >= P3.y)
{
a = P2.y - P3.y;
b = P3.x - P2.x;
c = P3.y * P2.x - P3.x * P2.y;
}
else
{
a = P3.y - P2.y;
b = P2.x - P3.x;
c = P2.y * P3.x - P2.x * P3.y;
}
y1 = P1.y;
x1 = (-b*y1-c) / a;
if (P1.x<=x1)
{
return true;
}
else
return false;
}
//点到直线的距离 直线由两点p2、p3决定 ax+by+c=0
//计算点P1到直线P2P3的距离
double Distance(CvPoint P1,CvPoint P2,CvPoint P3)
{
double s = 0;
double a = 0,b = 0,c = 0;
if(P2.y >= P3.y)
{
a = P2.y - P3.y;
b = P3.x - P2.x;
c = P3.y * P2.x - P3.x * P2.y;
}
else
{
a = P3.y - P2.y;
b = P2.x - P3.x;
c = P2.y * P3.x - P2.x * P3.y;
}
s = fabs(a * P1.x + b * P1.y + c) / sqrt( a * a + b * b);
return s;
}