为什么只能处理竖直方向的点,平行方向的没变化?代码如下:
(点阵中点的个数M
初始搜索点StartIndex=0;
间隔点EndIndex=0;
给定的限定误差T;
点的x,y坐标存于array中,按顺序存储;)
while(StartIndex<M&&EndIndex<=M)
{
s=array[StartIndex][1];//s行
k=array[StartIndex][0];//k列
temp[wide*3*s+k*3]=0;
temp[wide*3*s+k*3+1]=0;
temp[wide*3*s+k*3+2]=0;////////对应的象素点置为黑
EndIndex=StartIndex+2;
pflag=false;
while(EndIndex<=M&&pflag==false)
{
Maxdis=0;
for(i=StartIndex+1;i<EndIndex;i++)
{
dis=datacut.GetDis(array[StartIndex][0],array[StartIndex][1],array[EndIndex][0],array[EndIndex][1],array[i][0],array[i][1]);///求点到直线StartIndex-EndIndex的距离
if(dis>Maxdis)
{
Maxdis=dis;
MaxIndex=i;
} }/////////////////获得最大距离点及最大距离
if(Maxdis<T)
EndIndex++;////距离小于限定值,将EndIndex的下一点作为新的EndIndex,重新寻找距离最大点
else
{
StartIndex=MaxIndex;
pflag=true;
}////////距离不小于限定值,将最大距离点做为下一初始点StartIndex
}
}
(点阵中点的个数M
初始搜索点StartIndex=0;
间隔点EndIndex=0;
给定的限定误差T;
点的x,y坐标存于array中,按顺序存储;)
while(StartIndex<M&&EndIndex<=M)
{
s=array[StartIndex][1];//s行
k=array[StartIndex][0];//k列
temp[wide*3*s+k*3]=0;
temp[wide*3*s+k*3+1]=0;
temp[wide*3*s+k*3+2]=0;////////对应的象素点置为黑
EndIndex=StartIndex+2;
pflag=false;
while(EndIndex<=M&&pflag==false)
{
Maxdis=0;
for(i=StartIndex+1;i<EndIndex;i++)
{
dis=datacut.GetDis(array[StartIndex][0],array[StartIndex][1],array[EndIndex][0],array[EndIndex][1],array[i][0],array[i][1]);///求点到直线StartIndex-EndIndex的距离
if(dis>Maxdis)
{
Maxdis=dis;
MaxIndex=i;
} }/////////////////获得最大距离点及最大距离
if(Maxdis<T)
EndIndex++;////距离小于限定值,将EndIndex的下一点作为新的EndIndex,重新寻找距离最大点
else
{
StartIndex=MaxIndex;
pflag=true;
}////////距离不小于限定值,将最大距离点做为下一初始点StartIndex
}
}
double CDataCut::GetDis(int x1, int y1, int x2, int y2, int x0, int y0)
{
double dis;
double k;
double b;
double M,N; if(x1==x2)
dis=fabs(x0-x1);
else
{
k=(y1-y2)/(x1-x2);
b=(x1*y2-x2*y1)/(x1-x2);
M=fabs(y0-k*x0-b);
N=sqrt(k*k+1);
dis=M/N; }
return dis;}