Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generates string for Bezier curves in GeoGebra
% Generates Bezier polynomials in x and y for use in Geogebra
%
%
% Input(s)
% n: Degree of Bezier curve
%
% Output(s)
% cStr: String used to define Bezier curve in Geogebra
%
% Example:
% Start Geogebra, select number of points n, and place n points on the Geogebra
% grid. In this example, n = 5.
% Run curveString:
% cStr = curveString(5)
% Copy and paste string into Geogebra, then move points as desired to shape
% the Bezier curve. Points in Geogebra are now control points for the
% Bezier curve.
%
%
% See also: BezierHull
%
%
% Dependencies: None
%
%
% Written by: John Peach 30-Sep-2020
% Marakesh Sailing Analysis and Design
%
% Revisions:
function cStr = curveString(n)
% First n upper case letters of the alphabet
alpha = upper(char(65:64+n));
% Binomial coefficients
coeff = binomial(n-1);
% Generate x,y polynomial strings
x = ['x(A)(1-t)^' num2str(n-1) ' + '];
y = ['y(A)(1-t)^' num2str(n-1) ' + '];
for k = 2:n-1
c = num2str(coeff(k));
if k > 2
p0 = ['^' num2str(k-1)];
else
p0 = '';
endif
if n-k > 1
p1 = ['^' num2str(n-k)];
else
p1 = '';
endif
Ltr = alpha(k);
x = [x c 'x(' Ltr ')' '(1-t)' p1 '*t' p0 ' + '];
y = [y c 'y(' Ltr ')' '(1-t)' p1 '*t' p0 ' + '];
endfor
x = [x 'x(' alpha(end) ')' 't^' num2str(n-1)];
y = [y 'y(' alpha(end) ')' 't^' num2str(n-1)];
% Combine into curve definition
cStr = ['curve[' x ', ' y ',t,0,1]'];
% Display string
fprintf('%s\n', cStr)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment