根据四个点坐标排列出左上右上右下左下位置关系
2017-10-28 17:24
543 查看
根据四个点坐标排列出左上右上右下左下位置关系
基本思路是:先计算出这四个点的中心位置,然后根据中心位置来做判断。
那么中心位置就是把他们四个点的坐标全部加起来再除以4。
然后根据点的y坐标值,与中心点y的值比较。大于中心点坐标y的为底下。
小于中心点坐标y的为顶上。(x是向右的,y坐标是向下的)
区分好了上下之后,再在上下的点集分别分出左右来。
x坐标小于中心点就是左,x坐标大于中心点就是右
整个算法代码如下:
#include <io.h> #include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream> #include <cstring> using namespace cv; using namespace std; void sortFourPoints(vector<Point2f>& FourPoints) { vector<Point2f> top,bottom; Point2f center={0,0} //to confirm it is the 4 pointer. else return if(FourPoints.size()!=4)return; //first to locate the center point. for (int i = 0;i < 4;i++) { center += FourPoints[i]; } center =center/4.; //to sort the two top points and two bottom points for (int i =0;i< 4;i++) { if (FourPoints[i].y<center.y) { top.push_back(FourPoints[i]); } else { bottom.push_back(FourPoints[i]); } } //to sort the two left points and two right points Point2f topleft = top[0].x > center.x ? top[1] : top[0]; Point2f topright = top[0].x > center.x ? top[0] : top[1]; Point2f bottomleft = bottom [0].x > center.x ? bottom [1] : bottom [0]; Point2f bottomright = bottom [0].x > center.x ? bottom [0] : bottom [1]; //clear the FourPoints vector. FourPoints.clear(); //update the FourPoints vector to correct order FourPoints.push_back(topleft); FourPoints.push_back(topright); FourPoints.push_back(bottomright); FourPoints.push_back(bottomleft); }
另外需要注意:
这样排列的坐标关系,有时候需要再转个90度才能满足我们的需求。
相关文章推荐
- 简单的顶点着色(根据模型坐标和世界坐标位置)
- Cocos2d中各种坐标位置关系
- JS获取鼠标坐标并且根据鼠标位置不同弹出不同内容
- 游戏Algorithm----数组重排:将数组中的元素,根据值放到与坐标相等的位置
- 计算几何--两圆的位置关系(求交点个数及交点坐标)
- 详解iOS游戏开发中Cocos2D的坐标位置关系
- 根据经纬度绘制坐标点相对位置(分别用php和html5实现)
- Cocos2d中各种坐标位置关系
- js 百度地图 根据地理位置坐标地图定位到地理位置
- 在4x4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。
- 根据控件位置自动算出坐标在后面加一个提示框
- Cocos2d-JS 贝塞尔曲线根据时间设定即时坐标位置 C++到JS 绑定实现(联网游戏客户端Bezier同步功能实现)
- Android View的几个位置坐标关系
- Android根据已知的经纬度坐标获取当前位置
- 根据地址返回坐标位置的百度地图api
- 安卓实现的根据坐标显示位置功能
- 根据四个点坐标求两个直线的交叉点坐标
- 根据模型的顶点位置坐标和纹理坐标计算顶点的法线、切线和副法线
- 自定义dialog窗口,根据坐标可随意设置dialog显示位置,实现了窗口弹出动画