[英]How do I fill in the area between two lines and a curve that's not straight in MATLAB (the region is not a polygon)
使用matlab的FILL函数创建一个填充区域,该填充区域由具有直边的多边形限制:
不幸的是,这在上图中留下了一个小的白色区域,因为我要填充的区域的边界不是笔直的多边形,而是在左侧具有弯曲的边界。 我有一条曲线(几乎是抛物线,但不完全是抛物线),我想填充两条水平线和曲线本身之间的区域。 我也研究了MATLAB函数IMFILL,但是没有运气。
您需要做的是制作一个带有更多角的多边形,以便它更平滑地拟合曲线:
%# create a parabola and two straight lines
x = -3:0.1:3;
y = x.^2/4;
plot(x,y)
hold on, plot([-3 3],[1 1],'r',[-3 3],[2 2],'r')
%# create a polygon that hugs the parabola
%# note that we need to interpolate separately
%# for positive and negative x
x1 = interp1(y(x<0),x(x<0),1:0.1:2);
%# interpolate in reverse so that the corners are properly ordered
x2 = interp1(y(x>0),x(x>0),2:-0.1:1);
%# fill the area bounded by the three lines
fill([x1,x2],[1:0.1:2,2:-0.1:1],'g')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.