图像识别出了9个点,那么如何进行排序呢?
下面写出的代码是我项目真实应用的,然后使用了
三角形辨别 和 线性规划的知识
感觉还是有点高端的,拿出来与大家分享
#include <iostream>
using namespace std;
/*
P结构体存原数据
res结构体存排序后的数据
测试数据:
400 450
380 500
600 620
450 820
350 810
350 790
600 326
750 486
860 593
*/
#define nums 9
struct pos1{
int x;
int y;
int flag;
}P[10];
struct pos2{
int x;
int y;
}res[10];
int max_y(){
int max = 0,flag=0,pos;
for(int i=0;i<nums;i++){
if(P[i].flag){
if(P[i].y>max){
max = P[i].y;
pos = i;
}
}
}
P[pos].flag = 0;
return pos;
}
int min_x(){
int min = 99999999,flag=0,pos;
for(int i=0;i<nums;i++){
if(P[i].flag){
if(P[i].x<min){
min = P[i].y;
pos = i;
}
}
}
P[pos].flag = 0;
return pos;
}
int main(){
int max,pos,min;
for(int i=0;i<nums;i++){
cin>>P[i].x>>P[i].y;
P[i].flag = 1;
}
// 第一个点,找X最大的
max = 0;
for(i=0;i<nums;i++){
if(P[i].x>max){
max = P[i].x;
pos = i;
}
}
res[0].x = P[pos].x;
res[0].y = P[pos].y;
P[pos].flag = 0; //表示已经被用了
//第三个点,找Y轴最小的
min = 999999999;
for(i=0;i<nums;i++){
if(P[i].flag){
if(P[i].y<min){
min = P[i].y;
pos = i;
}
}
}
res[2].x = P[pos].x;
res[2].y = P[pos].y;
P[pos].flag = 0;
//脚上三个点搞定
int foot1,foot2,foot3;
foot1 = max_y();
foot2 = max_y();
foot3 = max_y();
int a = (P[foot1].x-P[foot2].x)*(P[foot1].x-P[foot2].x)+(P[foot1].y-P[foot2].y)*(P[foot1].y-P[foot2].y);
int b = (P[foot1].x-P[foot3].x)*(P[foot1].x-P[foot3].x)+(P[foot1].y-P[foot3].y)*(P[foot1].y-P[foot3].y);
int c = (P[foot2].x-P[foot3].x)*(P[foot2].x-P[foot3].x)+(P[foot2].y-P[foot3].y)*(P[foot2].y-P[foot3].y);
if(a<b&&a<c){
if(P[foot1].y>P[foot2].y){ //赋值问题,此时P[1]是最小的
res[6].x = P[foot2].x;
res[6].y = P[foot2].y;
res[7].x = P[foot1].x;
res[7].y = P[foot1].y;
res[8].x = P[foot3].x;
res[8].y = P[foot3].y;
}
else{ //赋值问题,此时P[1]是最小的
res[6].x = P[foot1].x;
res[6].y = P[foot1].y;
res[7].x = P[foot2].x;
res[7].y = P[foot2].y;
res[8].x = P[foot3].x;
res[8].y = P[foot3].y;
}
}
if(b<a&&b<c){
if(P[foot1].y>P[foot3].y){ //赋值问题,此时P[1]是最小的
res[6].x = P[foot3].x;
res[6].y = P[foot3].y;
res[7].x = P[foot1].x;
res[7].y = P[foot1].y;
res[8].x = P[foot2].x;
res[8].y = P[foot2].y;
}
else{ //赋值问题,此时P[1]是最小的
res[6].x = P[foot1].x;
res[6].y = P[foot1].y;
res[7].x = P[foot3].x;
res[7].y = P[foot3].y;
res[8].x = P[foot2].x;
res[8].y = P[foot2].y;
}
}
if(c<b&&c<a){
if(P[foot2].y>P[foot3].y){ //赋值问题,此时P[1]是最小的
res[6].x = P[foot3].x;
res[6].y = P[foot3].y;
res[7].x = P[foot2].x;
res[7].y = P[foot2].y;
res[8].x = P[foot1].x;
res[8].y = P[foot1].y;
}
else{ //赋值问题,此时P[1]是最小的
res[6].x = P[foot2].x;
res[6].y = P[foot2].y;
res[7].x = P[foot3].x;
res[7].y = P[foot3].y;
res[8].x = P[foot1].x;
res[8].y = P[foot1].y;
}
}
P[foot1].flag=0;P[foot2].flag=0;P[foot3].flag=0;
//cout<<res[6].x<<" "<<res[6].y<<" "<<res[7].x<<" "<<res[7].y<<" "<<res[8].x<<" "<<res[8].y<<endl;
//腰上两个点
int waist1,waist2;
waist1 = min_x();waist2 = min_x();
if(P[waist1].y>P[waist2].y){ //把waist1放4
res[4].x = P[waist1].x;
res[4].y = P[waist1].y;
res[3].x = P[waist2].x;
res[3].y = P[waist2].y;
}
else{ //把waist2放4
res[4].x = P[waist2].x;
res[4].y = P[waist2].y;
res[3].x = P[waist1].x;
res[3].y = P[waist1].y;
}
P[waist1].flag = 0;P[waist2].flag = 0;
//cout<<res[3].x<<" "<<res[3].y<<" "<<res[4].x<<" "<<res[4].y<<endl;
//剩余两个点的确定,第二个点和第六个点,通过线性规划的方法实现
//cout<<res[0].x<<" "<<res[0].y<<" "<<res[2].x<<" "<<res[2].y<<endl;
int count=0,temp[10]; //temp用来存储结点信息
for(i=0;i<nums;i++){
if(P[i].flag){
temp[count++] = i; //结点信息存进了temp[0]和temp[1]里面
}
}
///
//
///
//下面这个模块是直接调用的之前测试成功的代码
int x1,y1,x2,y2,x3,y3,x4,y4,lflag=0;
x1 = res[0].x;y1 =res[0].y;x3 =res[1].x;y3 = res[1].y;
x2 = P[temp[0]].x;y2=P[temp[0]].y;x4 = P[temp[1]].x;y4=P[temp[1]].y;
int k1 = (y2-y1)/(x2-x1);
int k2 = (y3-y2)/(x3-x2);
if(x4<x2){ //在左边的情况
int temp_y = k1*(x4-x2)+y2;
if(temp_y<y4){
lflag = 1; //在下面,说明x2就是2那个点
}
else{
lflag = 2; //在上面
}
}
if(x4>x2){
int temp_y =k2*(x4-x2)+y2;
if(temp_y<y4){
lflag = 1; //在下面,说明x2就是1那个点
}
else{
lflag = 2; //在上面
}
}
if(lflag==1){
res[1].x = x2;
res[1].y = y2;
res[5].x = x4;
res[5].y = y4;
}
if(lflag==2){
res[1].x = x4;
res[1].y = y4;
res[5].x = x2;
res[5].y = y2;
}
// 测试已经通过,已经只缺两个
cout<<endl<<endl<<"分割线--------------------------"<<endl;
for(i=0;i<nums;i++){
cout<<res[i].x<<" "<<res[i].y<<endl;
}
}