From cd09f137d1e5df4a5937e4a103f83348a18138bc Mon Sep 17 00:00:00 2001 From: Stefan Sava Date: Sat, 20 Oct 2018 17:28:30 +0300 Subject: [PATCH] Implement the deCasteljau Algorithm and plot the Bezier Curve using the resulted points --- matlab/interpolations/Bezier.m | 13 ++++++++++++ matlab/interpolations/deCasteljau.m | 33 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 matlab/interpolations/Bezier.m create mode 100644 matlab/interpolations/deCasteljau.m diff --git a/matlab/interpolations/Bezier.m b/matlab/interpolations/Bezier.m new file mode 100644 index 0000000..348490f --- /dev/null +++ b/matlab/interpolations/Bezier.m @@ -0,0 +1,13 @@ +function [] = Bezier(v, dim) + result = zeros(dim, 2); + i = 1; + for u = linspace(0,1,dim) + result(i,:) = deCasteljau(v, length(v) - 1, 0, u); + i++; + endfor + + plot (result(1:dim,1), result(1:dim,2), 'r','LineWidth',2) + hold + plot(v(:,1), v(:,2),'LineWidth',2) + title('Bezier Curve for given points') +endfunction \ No newline at end of file diff --git a/matlab/interpolations/deCasteljau.m b/matlab/interpolations/deCasteljau.m new file mode 100644 index 0000000..7b4318a --- /dev/null +++ b/matlab/interpolations/deCasteljau.m @@ -0,0 +1,33 @@ +## Copyright (C) 2018 Stefan +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{retval} =} Bezier (@var{input1}, @var{input2}) +## +## @seealso{} +## @end deftypefn + +## Author: Stefan +## Created: 2018-10-20 + +#Finding a point on a Bezier Curve: DeCasteljau's Algorithm +function [result] = deCasteljau (v,i,j,t) + if i == 0 + result = v(j + 1, :); + %v(j,0) = x coordinate; v(j,1) = y coordinate; + else + result = (1 - t) * deCasteljau(v,i - 1,j,t) + t * deCasteljau(v,i - 1, j + 1,t); + endif +endfunction