Matlab实现鼠标输入控制顶点的贝塞尔曲线的绘制

%%
%绘制二维的贝塞尔曲线图

figure('name', '绘制贝塞尔曲线');
axis([-10 10 -10 10]); %设置坐标轴范围,xmin xmax ymin ymax
grid on
n = input('input the number: ');
[x, y] = ginput(n)
plot(x, y, 'b')
length_x = length(x);
length_y = length(y);
vertices = zeros(2, length_x);  %需要初始化,否则容易出现length_x只记得最高次
vertices(1, 1 : length_x) = x;
vertices(2, 1 : length_y) = y;
% bezier(vertices)
Roxanne_bezier_1(vertices)
% Roxanne_bezier_2(vertices)
function Roxanne_bezier_2(vertices)

%%
%贝塞尔几何作图法,
%vertices 为绘制的点

NumPoint = size(vertices, 2); %点的个数

x = [];
y = [];

%绘制鼠标所点击的点
plot(vertices(1, :), vertices(2, :), 'b');
hold on;
grid on;

for i = 1: 1: NumPoint
    x(i) = vertices(1, i);
    y(i) = vertices(2, i);
end

x1 = [];
y1 = [];

x2 = [];
y2 = [];
temp = 1;
for u = 0 : 0.001 : 1
    for l = 1: 1: (NumPoint + 1)
        for i = 1: 1: (NumPoint + 1 - l)
            if l == 1
                x1(i, l) = x(i);
                y1(i, l) = y(i);
                %scatter(x1, y1, 5, 'r');
            elseif l ~= 1
                x1(i, l) = (1 - u) * x1(i, l - 1) + u * x1(i + 1, l - 1);
                y1(i, l) = (1 - u) * y1(i, l - 1) + u * y1(i + 1, l - 1);
                %scatter(x1(i, l), y1(i, l), 5, 'r');
            end
        end
    end
    x2(temp) = x1(1, NumPoint);
    y2(temp) = y1(1, NumPoint);
    temp = temp + 1;
end

    hold on;
    grid on;
    xlabel('X');
    ylabel('Y');
    plot(x2, y2, 'g');
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页