多边形在一个原点的哪个象限
只需要判断当前的多边形的每一个顶点在原点的哪个象限就可以了。
代码
typedef struct {
int x, y;
} Point;
int get_quadrant(Point pTarget, Point pOrigin)
{
Point p;
p.x = pTarget.x - pOrigin.x;
p.y = pTarget.y - pOrigin.y;
if (p.x > 0 && p.y > 0)
{
return 1;
}
else if (p.x < 0 && p.y > 0)
{
return 2;
} else if (p.x < 0 && p.y < 0)
{
return 3;
} else if (p.x > 0 && p.y < 0)
{
return 4;
} else
{
// 点在坐标轴上
return 0;
}
}
bool GetDikedAvoidQuadrant(std::vector<Point>& vecPolygon, Point pOrigin, std::vector<int>& vecQuadrant)
{
bool bRet = false;
std::vector<Point>::iterator iterPolygon = vecPolygon.begin();
for (; iterPolygon != vecPolygon.end(); iterPolygon++)
{
//获取象限
int iQuadrant = get_quadrant(*iterPolygon, pOrigin);
if(iQuadrant >= 0 && iQuadrant <= 4)
{
vecQuadrant[iQuadrant] = 1;
bRet = true;
}
}
return bRet;
}
mian函数
int main()
{
//获取象限
std::vector<int> vecQuadrant = { 0, 0, 0, 0, 0 }; //在坐标轴上, 1 ,2, 3, 4 象限
std::vector<Point> polygon; //保存区域顶点坐标
Point circleCenter; //原点坐标
//如果在哪个象限vecQuadrant对应位置为1,否则为零
GetDikedAvoidQuadrant(polygon, circleCenter, vecQuadrant);
return;
}