1.判断点在多边形内的多种写法
博客链接:
http://blog.csdn.net/hgl868/article/details/7947272
http://blog.csdn.net/zhongshansubor/article/details/6694994
2.如果你懂opencv
opencv 函数pointPolygonTest 检测一个点是否在多边形内
博客链接:
http://blog.csdn.net/xiaxiazls/article/details/48392875
用于测试一个点是否在多边形中
当measureDist设置为true时,若返回值为正,表示点在多边形内部,
返回值为负,表示在多边形外部,返回值为0,表示在多边形上。
当measureDist设置为false时,若返回值为+1,表示点在多边形内部,
返回值为-1,表示在多边形外部,返回值为0,表示在多边形上。
以上我都验证了:(可用)
博客链接:
http://blog.csdn.net/hgl868/article/details/7947272
http://blog.csdn.net/zhongshansubor/article/details/6694994
2.如果你懂opencv
opencv 函数pointPolygonTest 检测一个点是否在多边形内
博客链接:
http://blog.csdn.net/xiaxiazls/article/details/48392875
用于测试一个点是否在多边形中
当measureDist设置为true时,若返回值为正,表示点在多边形内部,
返回值为负,表示在多边形外部,返回值为0,表示在多边形上。
当measureDist设置为false时,若返回值为+1,表示点在多边形内部,
返回值为-1,表示在多边形外部,返回值为0,表示在多边形上。
以上我都验证了:(可用)
#include <cv.hpp>
#include <core.hpp>
#include <highgui.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
typedef int BOOL;
typedef struct _SPOINT
{
int x;
int y;
}POINT,*pPOINT,*LPPOINT;
BOOL PtInPolygon (POINT p, LPPOINT ptPolygon, int nCount)
{
int nCross = 0;
for (int i = 0; i < nCount; ++i)
{
/* code */
POINT p1 = ptPolygon[i];
POINT p2 = ptPolygon[(i + 1) % nCount];
if (p1.y == p2.y)
continue;
if (p.y < std::min(p1.y,p2.y))
continue;
if (p.y >= std::max(p1.y,p2.y))
continue;
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);
}
int main(int argc, char const *argv[])
{
Mat picture(500,500,CV_8UC3,Scalar(255,255,255)); //准备一画板, 500*500 的大小, Mat 为opencv 结构
Point a1 = Point(50,50); //待测试的多边形 顶点坐标a1 Point 为opencv 结构
Point a2 = Point(100,100);
Point a3 = Point(80,185);
Point a4 = Point(230,125);
std::vector<Point2f> vPoint; //Point2f 为opencv 里定义的数据类型,这数据结构 包含两个float(x,y)
vPoint.clear();
line(picture,a1,a2,Scalar(0,0,255)); //opencv 画线
line(picture,a2,a3,Scalar(0,255,0));
line(picture,a1,a4,Scalar(0,125,120));
line(picture,a3,a4,Scalar(120,12,120));
Point centerL = Point(280,220);
Point centerR = Point(360,270);
Point cetL = Point(120,100);
Point cetR = Point(160,140);
LPPOINT m_point = new POINT[4]; //自定义的数据类型,跟 是否用opencv 无关,
for (int i = 0; i < 4; ++i)
{
if (i == 0)
{
m_point[i].x = a1.x;
m_point[i].y = a1.y;
}
if (i == 1)
{
m_point[i].x = a2.x;
m_point[i].y = a2.y;
}
if (i == 2)
{
m_point[i].x = a3.x;
m_point[i].y = a3.y;
}
if (i == 3)
{
m_point[i].x = a4.x;
m_point[i].y = a4.y;
}
vPoint.push_back(Point2f(m_point[i].x,m_point[i].y)); //放入容器。备用
}
POINT m_center; //中心点。该中心点不在 多边形内
m_center.x = (centerR.x - centerL.x)/2; //中心点 为 框的 中心 。在下面将画出 框,便于 确定点的位置
m_center.y = (centerR.y - centerL.y)/2;
m_center.x = centerL.x + m_center.x;
m_center.y = centerL.y + m_center.y; //中心点坐标
//-------------
POINT m_test; //测试点。该 测试点 在 多边形内
m_test.x = (cetR.x - cetL.x)/2;
m_test.y = (cetR.y - cetL.y)/2;
m_test.x = cetL.x + m_test.x;
m_test.y = cetL.y + m_test.y; // 测试点 坐标
//----------------
int ret = PtInPolygon(m_test,m_point,4);
if (ret)
{
std::cout << m_test.x << std::endl;
std::cout << m_test.y << std::endl;
std::cout << ret << std::endl;
rectangle(picture,cetL,cetR,Scalar(0,0,255)); // 画测试点 的框 1
}
Point2f a(m_test.x,m_test.y);
double Testret = pointPolygonTest(vPoint,a,false); //opencv 库里面的函数
if (Testret)
{
std::cout << "..."<< Testret << std::endl; //if(ret) ..Testret is 1.
}
rectangle(picture,centerL,centerR,Scalar(255,0,0)); // 画 中心点 的框 2 opencv
namedWindow("Display Image",CV_WINDOW_AUTOSIZE); // 给显示窗口命名 opencv
imshow("Display Image",picture); // 显示粗来 opencv
waitKey();
return 0;
}