连接最终和初始点简单的XY坐标图(绘制封闭曲线/多边形)多边形、曲线、简单、XY

2023-09-07 13:00:08 作者:貪戀迩de溫柔~

说,例如,我有...

Say, for example, I had ...

x = [1 1 2 2];
y = [1 2 2 1];
plot(x, y, 'b-');

我将获得与连接点(1,1),(1,2),和(2,2)线的曲线图。有没有什么办法的最后一点与第一连接,从而完成了广场上的情节?

I will get a plot with lines connecting the points (1,1), (1,2), and (2,2). Is there any way to connect the final point with the first, thus completing the square on the plot?

我也拉动文本分线,因此只需添加另一点1,1是不是一种选择。

I'm also pulling in lines of text with points, so simply adding another point 1,1 is not an option.

推荐答案

impoly 可能是有用的,但是,它创建了一个修改的曲线比剧情更慢。

impoly can be useful, however, it creates a modifiable curve which is slower than plot.

您可以编写一个简单的函数,该函数:

You can write a simple function for that:

function plotc(x,y,varargin)  
    x = [x(:) ; x(1)];   
    y = [y(:) ; y(1)];  
    plot(x,y,varargin{:})  
end

顺便说一句,在(:)冒号运算符是用来作为防御性编程手段。通过这种方式, X 可以是行或列向量。

By the way, the (:) colon operator is used as defensive programming means. In this way, x and y can be either row or column vectors.

varargin 允许使用额外的参数,如:

The varargin allows using additional parameters, like:

 plotc(x,y,'Color','r');
 plotc(x,y,'Parent',a,'LineWidth',2);