找回密码
 注册
Simdroid-非首页
查看: 232|回复: 13

怎么拟合一条闭合曲线

[复制链接]
发表于 2011-4-28 16:17:02 | 显示全部楼层 |阅读模式 来自 大连理工大学
比如最简单的 一个长方形 只知道每边上的有限个点 怎么把这个闭合曲线拟合出来?
发表于 2011-4-28 17:11:01 | 显示全部楼层 来自 黑龙江哈尔滨
Simdroid开发平台
这个问题以前讨论过,搜索一下以前的贴子
回复 不支持

使用道具 举报

 楼主| 发表于 2011-4-29 15:48:16 | 显示全部楼层 来自 大连理工大学
2# messenger 我想使用插值得到更多的点
但是在使用那个插值函数时 总是出现错误提示 是怎么回事 附上代码 是一个很简单的长方形
The values of X should be distinct.

x=[0;1;2;2;2;1;0;0];
y=[0;0;0;1;2;2;2;1];
points=[x;y];
x0=mean(x);y0=mean(y); % the center point
r=sqrt((x-x0).^2+(y-y0).^2); % the radius
theta=acos((x-x0)./r); %the angle
index=(y-y0)<0;
theta(index)=2*pi-theta(index); %计算各个点的角度
[theta,i1]=sort(theta);
r1=r(i1);
theta1=min(theta):2*pi/100:max(theta);
r2=interp1(theta,[r1],theta1,'spline');  
xi=x0+r2.*cos(theta1);
yi=y0+r2.*sin(theta1);
points=[[xi;xi(1)],[yi;yi(1)]]';
curve=cscvn(points);
fnplt(curve);
hold on;
plot(points(1,:),points(2,:),'o')
hold off
回复 不支持

使用道具 举报

发表于 2011-4-29 16:48:04 | 显示全部楼层 来自 黑龙江哈尔滨
points在数组合并时,数组的行列搞反了。

  1. x=[0;1;2;2;2;1;0;0];
  2. y=[0;0;0;1;2;2;2;1];
  3. points=[x;y];
  4. x0=mean(x);y0=mean(y); % the center point
  5. r=sqrt((x-x0).^2+(y-y0).^2); % the radius
  6. theta=acos((x-x0)./r); %the angle
  7. index=(y-y0)<0;
  8. theta(index)=2*pi-theta(index); %计算各个点的角度
  9. [theta,i1]=sort(theta);
  10. r1=r(i1);
  11. theta1=min(theta):2*pi/100:max(theta);
  12. r2=interp1(theta,[r1],theta1,'spline');  
  13. xi=x0+r2.*cos(theta1);
  14. yi=y0+r2.*sin(theta1);
  15. %points=[[xi;xi(1)],[yi;yi(1)]]';
  16. points=[[xi xi(1)];[yi yi(1)]];
  17. curve=cscvn(points);
  18. fnplt(curve);
  19. hold on;
  20. plot(points(1,:),points(2,:),'yo')
  21. hold off
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2011-4-30 07:52:15 | 显示全部楼层 来自 大连理工大学
4# messenger
哦 谢谢 我给出的点 只想拟合出长方形 但是从拟合效果来看 似乎不是很好 是不是插值函数 不应该选择三次样条
回复 不支持

使用道具 举报

发表于 2011-5-1 11:16:44 | 显示全部楼层 来自 安徽淮南
学习一下啊

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2011-5-4 08:33:29 | 显示全部楼层 来自 大连理工大学
4# messenger
怎么求plot拟合的封闭图形的面积 area 只是把这个面积显示出来
回复 不支持

使用道具 举报

发表于 2011-5-4 09:41:40 | 显示全部楼层 来自 英国
本帖最后由 nwcwww 于 2011-5-4 09:43 编辑
4# messenger
怎么求plot拟合的封闭图形的面积 area 只是把这个面积显示出来
dongsheng01 发表于 2011-5-4 08:33


area = 1/2*abs(sum(x.*y([2:end,1])-y.*x([2:end,1])));

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2011-5-4 21:12:58 | 显示全部楼层 来自 大连理工大学
8# nwcwww
x=[-1;1;1;-1;0;0;1;-1];
y=[1; 1; -1; -1;1;-1;0;0];
points=[x;y];
[m,n]=size(y);
x0=mean(x);y0=mean(y); % the center point
r=sqrt((x-x0).^2+(y-y0).^2); % the radius
theta=acos((x-x0)./r); %the angle
index=(y-y0)<0;
theta(index)=2*pi-theta(index); %计算各个点的角度
[theta,i1]=sort(theta);
r1=r(i1);
i=0;s=0;
area = 1/2*abs(sum(x.*y([2:end,1])-y.*x([2:end,1])));
xi=x0+r1.*cos(theta);
yi=y0+r1.*sin(theta);
points=[[xi;xi(1)],[yi;yi(1)]]';
curve=cscvn(points);
fnplt(curve);
hold on;
plot(points(1,:),points(2,:),'o')
area(points(1,:),points(2,:))
hold off
精度不够啊 这是一个长方形 面积应该是4 算出来的是3.5
回复 不支持

使用道具 举报

发表于 2011-5-4 21:28:19 | 显示全部楼层 来自 英国
本帖最后由 nwcwww 于 2011-5-4 22:14 编辑

9# dongsheng01


点要顺时针或者逆时针排列的。
你之前那个例子就没问题,但现在点的顺序比较乱,直接plot(x,y)能看到很多交叉线。
还是这几个点,顺序改变一下就行
>> x=[-1 -1 -1 0 1 1 1 0];y=[-1 0 1 1 1 0 -1 -1];
>> S=1/2*abs(sum(x.*y([2:end,1])-y.*x([2:end,1])))

S =

     4


补充说明下,如果顺序已经排好了你也可以直接用polyarea算面积:
>> x=[-1 -1 -1 0 1 1 1 0];y=[-1 0 1 1 1 0 -1 -1];S=polyarea(x,y)

S =

     4

如果像你9楼那样顺序是乱的,考虑用convhull:
x=[-1;1;1;-1;0;0;1;-1];y=[1; 1; -1; -1;1;-1;0;0];[~,S]=convhull(x,y)

S =

     4
回复 不支持

使用道具 举报

 楼主| 发表于 2011-5-5 08:00:03 | 显示全部楼层 来自 大连理工大学
本帖最后由 dongsheng01 于 2011-5-6 09:41 编辑

10# nwcwww 谢谢解答  已经使用polyarea 函数解决
回复 不支持

使用道具 举报

发表于 2011-5-5 08:39:51 | 显示全部楼层 来自 陕西西安
长见识了,得认真学习
回复 不支持

使用道具 举报

发表于 2011-5-6 08:05:54 | 显示全部楼层 来自 山西太原
学习一下,真是高手如云啊!
回复 不支持

使用道具 举报

发表于 2011-5-7 20:52:59 | 显示全部楼层 来自 陕西西安
恩 是高手啊 我正找呢写了
回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Simapps系列直播

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-10-4 19:24 , Processed in 0.059806 second(s), 20 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表