Douglas-Peucker.m
Upload User: hdfuse
Upload Date: 2021-06-01
Package Size: 3k
Code Size: 1k
Development Platform:

Matlab

  1. function y=f21(x)
  2. e=0.5;
  3. x=0:0.01:2;
  4. y=sin(12*x.^2);
  5. plot(x,y);
  6. hold on;
  7. t=zeros(1,200);
  8. d=zeros(1,200);
  9. for i=2:199
  10.     plot(x(1),y(1),'*');
  11.     hold on
  12.     A=(y(200)-y(1))/sqrt((y(200)-y(1))^2+(x(200)-x(1))^2);
  13.     B=(x(200)-x(1))/sqrt((y(200)-y(1))^2+(x(200)-x(1))^2);
  14.     C=(x(200)*y(200)-x(1)*y(1))/sqrt((y(200)-y(1))^2+(x(200)-x(1))^2);
  15.     d(i)=abs(A*x(i)+B*y(i)+C)/sqrt(A^2+B^2);
  16.     dh=max(d(i));
  17.     if d(i)==dh
  18.         t=i;
  19.     end
  20.     if dh>e
  21.         plot(x(t),y(t),'*');
  22.         hold on;
  23.         break;
  24.     end
  25. end
  26. for j=2:t-1
  27.     A=(y(t)-y(1))/sqrt((y(t)-y(1))^2+(x(t)-x(1))^2);
  28.     B=(x(t)-x(1))/sqrt((y(t)-y(1))^2+(x(t)-x(1))^2);
  29.     C=(x(t)*y(t)-x(1)*y(1))/sqrt((y(t)-y(1))^2+(x(t)-x(1))^2);
  30.     d(j)=abs(A*x(j)+B*y(j)+C)/sqrt(A^2+B^2);
  31.     dh=max(d(j));
  32.     if d(j)==dh
  33.         t=j;
  34.     end
  35.     if dh>e
  36.         plot(x(t),y(t),'*');
  37.         hold on;
  38.      end
  39. end
  40. for k=t+1:199
  41.     A=(y(200)-y(t+1))/sqrt((y(200)-y(t+1))^2+(x(200)-x(t+1))^2);
  42.     B=(x(200)-x(t+1))/sqrt((y(200)-y(t+1))^2+(x(200)-x(t+1))^2);
  43.     C=(x(200)*y(200)-x(t+1)*y(t+1))/sqrt((y(200)-y(t+1))^2+(x(200)-x(t+1))^2);
  44.     d(k)=abs(A*x(k)+B*y(k)+C)/sqrt(A^2+B^2);
  45.     dh=max(d(k));
  46.     if d(k)==dh
  47.         t=k;
  48.     end
  49.     if dh>e
  50.         plot(x(t),y(t),'*');
  51.         break;
  52.     end
  53. end