博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CalFrechetDist
阅读量:5063 次
发布时间:2019-06-12

本文共 5051 字,大约阅读时间需要 16 分钟。

// FileRead.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <vector>
#include <iterator>
#include <fstream>
#include <iostream>
using namespace std;

void LoadLine(char* fname,vector<double>& generatedPoints);

double CalFrechetDist(char* fname1, char* fname2, long& pnum);

struct D_DOT {

double x;
double y;
};

typedef double* DBLPtr;

//void LoadLine(char* fname,vector<double>& generatedPoints)

//{
// char buf[1024];
// fstream infile;
// char sep[2]={0};
// double d;
// strcpy(sep,",");
//
// infile.open(fname,ios::in);
// while(1)
// {
// buf[0]=0;
// infile.getline(buf,1024);
// if(strlen(buf)<1)
// break;
//
// d=atof(strtok(buf,sep));
// generatedPoints.push_back(d);
//
// d=atof(strtok(NULL,sep));
// generatedPoints.push_back(d);
// }
//
// infile.close();
//}

void LoadLine(char* fname,vector<double>& generatedPoints)

{
//char buf[1024];
fstream infile;
char sep[2]={0};
double d;
strcpy(sep,",");

infile.open(fname); //,ios::in

printf("d:%s\n",fname);

//char str[34];

//fstream fin;
//fin.open(fname);
//if (!fin)
//{
// cout<<"error "<<endl;
// exit(1);
//}
//while(fin.getline(str,34))
//{
// cout<<str<<endl;
// /*d=atof(strtok(str,sep));
// generatedPoints.push_back(d);
// printf("d:%d\n",d);

// d=atof(strtok(NULL,sep));

// printf("d:%d\n",d);
// generatedPoints.push_back(d); */
//}
//fin.close();
char buf[34];
while(infile.getline(buf,34))//1
{
//cout<<buf<<endl;
//printf("buf:%s\n",buf);

if(buf!="\n"&&strlen(buf)>=1)

{
d=atof(strtok(buf,sep));
//cout<<"d:"<<d<<endl;
generatedPoints.push_back(d);
d=atof(strtok(NULL,sep));
generatedPoints.push_back(d);
}
}
infile.close(); //很重要别忘了关
}

int _tmain(int argc, _TCHAR* argv[])

{
int i;
double d1;
vector<double> dlist;
vector<long> pnumlist;

char ss[100] = { 0 };

long pnum;
for (i = 1; i<40; i++)
{
sprintf(ss, "C:\\Users\\Administrator\\Desktop\\testtxt\\TestTxt%d.txt", i + 1); //C:\\Users\\lenovo\\Desktop\\wo\\YYI_%d.txt
d1 = CalFrechetDist("C:\\Users\\Administrator\\Desktop\\testtxt\\TestTxt1.txt", ss, pnum); //double CalFrechetDist(char* fname1, char* fname2, long& pnum)
//cout << "距离:" << d1 << endl;
printf("距离:%d\n",d1);
dlist.push_back(d1);
pnumlist.push_back(pnum);
}
system("pause");
return 0;
}

double pi_pj(D_DOT& pi, D_DOT& pj)

{
double dx = pi.x - pj.x;
double dy = pi.y - pj.y;

return sqrt(dx*dx + dy*dy);

}

double c(D_DOT *zb1, D_DOT *zb2, int i, int j, double **ca)

{
int curI, curJ;
double **caao;
caao = (double **)new DBLPtr[i + 1];
for (curI = 0; curI <= i; curI++)
{
caao[curI] = new double[j + 1];
}

for (curI = 1; curI <= i; curI++)

{
for (curJ = 1; curJ <= j; curJ++)
{
caao[curI][curJ] = pi_pj(zb1[curI], zb2[curJ]);
}
}

ca[1][1] = caao[1][1];

for (curJ = 1; curJ <= j; curJ++)
{
for (curI = 1; curI <= i; curI++)
{
if (curJ == 1)
{
if (curI>1)
{
ca[curI][curJ] = caao[curI][curJ];
if (ca[curI][curJ]<ca[curI - 1][1])
{
ca[curI][curJ] = ca[curI - 1][1];
}
}
}
else if (curI == 1)
{
if (curJ>1)
{
ca[curI][curJ] = caao[curI][curJ];
if (ca[curI][curJ]<ca[1][curJ - 1])
{
ca[curI][curJ] = ca[1][curJ - 1];
}
}
}
else if (curI>1 && curJ>1)
{
ca[curI][curJ] = caao[curI][curJ];
double tmp1 = ca[curI - 1][curJ];
double tmp2 = ca[curI - 1][curJ - 1];
double tmp3 = ca[curI][curJ - 1];
if (tmp1<tmp2&&tmp1<tmp3)
{
if (ca[curI][curJ]<tmp1)
ca[curI][curJ] = tmp1;
}
else if (tmp2<tmp1&&tmp2<tmp3)
{
if (ca[curI][curJ]<tmp2)
ca[curI][curJ] = tmp2;
}
else
{
if (ca[curI][curJ]<tmp3)
ca[curI][curJ] = tmp3;
}
}
else ca[curI][curJ] = 9999;
}
}

for (curI = 0; curI <= i; curI++)

{
delete[] caao[curI];
}
delete[] caao;

return ca[i][j];

}

double Frechet(D_DOT *zb1,int n1,D_DOT *zb2,int n2,double** fm)

{
int i,j;
double **caa;
double result;

caa = (double **)new DBLPtr[n1+1];

for(i=0;i<=n1;i++)
{
caa[i]=new double[n2+1];
}
for(i=0;i<=n1;i++)
{
for(j=0;j<=n2;j++)
{
caa[i][j] = -1;
}
}
result = c(zb1,zb2,n1,n2,caa);
for(i=0;i<=n1;i++)
{
for(j=0;j<=n2;j++)
{
fm[j][i] = caa[i][j];
}
}
for(i=0;i<=n1;i++)
{
delete [] (caa[i]);
}

delete [] caa;

return result;
}

double CalFrechetDist(char* fname1, char* fname2, long& pnum) //计算Frechet距离

{
int i;
long len1, len2;
double d;
vector<double> generatedPoints1;
vector<double> generatedPoints2;

LoadLine(fname1, generatedPoints1); //读取文件fname1中的内容到generatedPoints1

LoadLine(fname2, generatedPoints2);

len1 = generatedPoints1.size() / 2 + 1;
D_DOT *lxy1 = new D_DOT[len1];
for (i = 0; i<len1 - 1; i++)
{
lxy1[i + 1].x = generatedPoints1[i * 2];
lxy1[i + 1].y = generatedPoints1[i * 2 + 1];
}
len2 = generatedPoints2.size() / 2 + 1;
D_DOT *lxy2 = new D_DOT[len2];
for (i = 0; i<len2 - 1; i++)
{
lxy2[i + 1].x = generatedPoints2[i * 2];
lxy2[i + 1].y = generatedPoints2[i * 2 + 1];
}

DBLPtr* fm = new DBLPtr[len2]; //指向数组的指针?no!指针数组?no!指向指针数组的指针

for (i = 0; i<len2; i++)
{
fm[i] = new double[len1];
}

d = Frechet(lxy1, len1 - 1, lxy2, len2 - 1, (double**)fm);

delete[] lxy1;

delete[] lxy2;
for (i = 0; i<len2; i++)
{
delete[] fm[i];
}
delete[] fm;

generatedPoints1.clear();

generatedPoints2.clear();
pnum = len2;
return d;
}

 

转载于:https://www.cnblogs.com/Forwithy/p/9670259.html

你可能感兴趣的文章
POP的Stroke动画
查看>>
线程同步机制初识 【转载】
查看>>
屏蔽各类弹窗广告(WPS、智能云输入法)
查看>>
作品第二课----改变DIV任意属性的值
查看>>
WCF(二) 使用配置文件实现WCF应用程序
查看>>
Oracle 游标使用全解
查看>>
什么是系统调用,对操作系统的服务请求与一般子程序调用有什么区别?
查看>>
css实现显示隐藏的5种方法
查看>>
anaconda3安装cv2模块(python3.6)
查看>>
python成长之路【第二篇】:列表和元组
查看>>
R-CNN论文详解(转载)
查看>>
关于矩阵的特征向量和特征值的含义
查看>>
MTK Android 标准编译命令
查看>>
Autofac 组件、服务、自动装配
查看>>
有一种力量叫坚持
查看>>
vs2010+qt4.8.6
查看>>
FZU 2057 家谱(dfs)
查看>>
[转]C#如何在ListView失去焦点的情况下仍然保持Item高亮
查看>>
MATLAB的crack安装小曲
查看>>
JavaScript方法splice()和slice()
查看>>