矢量数据压缩:道格拉斯普克算

时间:2022-01-07 23:43:45

 

CSDN上能够找到很多道格拉斯普克算法的代码:其中一个

矢量数据压缩:道格拉斯普克算矢量数据压缩:道格拉斯普克算参考代码
  1 #include<stdio.h>
  2 #include<math.h>
  3 #include<malloc.h>
  4 #include<string.h>
  5 #define NULL 0
  6 #define LEN sizeof(struct point)
  7 
  8 struct point
  9 {
 10     char ptname[8];
 11     float x;    
 12     float y;
 13     struct point *next;
 14 };
 15 
 16 struct point* creat(void)
 17 {
 18     struct point *head;
 19     struct point *p1,*p2;
 20     int n=0;
 21     p1=p2=(struct point*)malloc(LEN);
 22     scanf("%s",p1->ptname);
 23     head=NULL;
 24     while(strcmp(p1->ptname,"finish")!=0)
 25     {
 26         printf("请输入该点X Y坐标\n");
 27         scanf("%f%f",&p1->x,&p1->y);
 28         n=n+1;
 29         if(n==1) 
 30             head=p1;
 31         else
 32             p2->next=p1;
 33         p2=p1;
 34         p1=(struct point*)malloc(LEN);
 35         printf("请输入下一点点名\n");
 36         scanf("%s",p1->ptname);
 37     }
 38     p2->next=NULL;
 39     return(head);
 40 }
 41 
 42 struct point *getlast(struct point *p)
 43 {
 44     while(p->next!=NULL)
 45         p=p->next;
 46     return p;
 47 }
 48 
 49 float len(float x1,float y1,float x2,float y2,float x3,float y3)
 50 {
 51     float a,b,c,s,h;
 52     a=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
 53     b=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
 54     c=sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
 55     s=0.5*(a+b+c);
 56     h=(sqrt(s*(s-a)*(s-b)*(s-c)))*2/c;
 57     return h;
 58 }
 59 
 60 void doglas(struct point *head,struct point *last,float sigma)
 61 {
 62     struct point *hl;/*hl contains the remotest point C*/
 63     struct point *p=head->next;
 64     float h,max=0;
 65     while(p->next!=last)
 66     {
 67         h=len(head->x,head->y,p->x,p->y,last->x,last->y);
 68         if(h>max)
 69         {
 70             max=h;
 71             hl=p;
 72         }
 73         p=p->next;
 74     }
 75     if(max<=sigma)    /*It is not a feature point*/
 76     {
 77         head->next=last;
 78     }
 79     else              /*It is a feature point*/
 80     {
 81         doglas(head,hl,sigma);
 82         doglas(hl,last,sigma);
 83     }
 84 }
 85 
 86 void linkout(struct point *head)
 87 {
 88      struct point *p=head;
 89      printf("点名     X            Y\n");
 90      while(p)
 91      {
 92          printf("%s  %f  %f\n",p->ptname,p->x,p->y);
 93          p=p->next;
 94      }
 95 }
 96 void main()
 97 {
 98     printf("****************道格拉斯普克法******************\n");
 99     printf("################################################\n\n\n");
100     float sigma;
101     printf("请输入阈值\n");
102     scanf("%f",&sigma);
103     printf("请输入第一点点名,以'finish'结束输入\n");
104     struct point *head;
105     head=creat();
106     printf("\n\n您输入的点为:\n\n");
107     linkout(head);
108     doglas(head,getlast(head),sigma);
109     printf("\n\n经道格拉斯普克法处理后的点为:\n");
110     while(head!=NULL)
111     {
112         printf("%s  ",head->ptname);
113         head=head->next;
114     }
115     printf("\n");
116 }

 

来源:http://download.csdn.net/download/hejuncsnd/2906435