Home > matGeom > geom2d > cubicBezierToPolyline.m

cubicBezierToPolyline

PURPOSE ^

CUBICBEZIERTOPOLYLINE Compute equivalent polyline from bezier curve control.

SYNOPSIS ^

function varargout = cubicBezierToPolyline(points, varargin)

DESCRIPTION ^

CUBICBEZIERTOPOLYLINE Compute equivalent polyline from bezier curve control.

   POLY = cubicBezierToPolyline(POINTS, N)
   Creates a polyline with N edges from the coordinates of the 4 control
   points stored in POINTS. 
   POINTS is either a 4-by-2 array (vertical concatenation of point
   coordinates), or a 1-by-8 array (horizontal concatenation of point
   coordinates). 
   The result is a (N-1)-by-2 array.

   POLY = cubicBezierToPolyline(POINTS)
   Assumes N = 64 edges as default.

   [X Y] = cubicBezierToPolyline(...)
   Returns the result in two separate arrays for X and Y coordinates.


   Example
     poly = cubicBezierToPolyline([0 0;5 10;10 5;10 0], 100);
     drawPolyline(poly, 'linewidth', 2, 'color', 'g');

   See also
     drawBezierCurve, drawPolyline


 ------
 Author: David Legland
 e-mail: david.legland@grignon.inra.fr
 Created: 2011-10-06,    using Matlab 7.9.0.529 (R2009b)
 Copyright 2011 INRA - Cepia Software Platform.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function varargout = cubicBezierToPolyline(points, varargin)
0002 %CUBICBEZIERTOPOLYLINE Compute equivalent polyline from bezier curve control.
0003 %
0004 %   POLY = cubicBezierToPolyline(POINTS, N)
0005 %   Creates a polyline with N edges from the coordinates of the 4 control
0006 %   points stored in POINTS.
0007 %   POINTS is either a 4-by-2 array (vertical concatenation of point
0008 %   coordinates), or a 1-by-8 array (horizontal concatenation of point
0009 %   coordinates).
0010 %   The result is a (N-1)-by-2 array.
0011 %
0012 %   POLY = cubicBezierToPolyline(POINTS)
0013 %   Assumes N = 64 edges as default.
0014 %
0015 %   [X Y] = cubicBezierToPolyline(...)
0016 %   Returns the result in two separate arrays for X and Y coordinates.
0017 %
0018 %
0019 %   Example
0020 %     poly = cubicBezierToPolyline([0 0;5 10;10 5;10 0], 100);
0021 %     drawPolyline(poly, 'linewidth', 2, 'color', 'g');
0022 %
0023 %   See also
0024 %     drawBezierCurve, drawPolyline
0025 %
0026 %
0027 % ------
0028 % Author: David Legland
0029 % e-mail: david.legland@grignon.inra.fr
0030 % Created: 2011-10-06,    using Matlab 7.9.0.529 (R2009b)
0031 % Copyright 2011 INRA - Cepia Software Platform.
0032 
0033 % default number of discretization steps
0034 N = 64;
0035 
0036 % check if discretization step is specified
0037 if ~isempty(varargin)
0038     var = varargin{1};
0039     if length(var) == 1 && isnumeric(var)
0040         N = round(var);
0041     end
0042 end
0043 
0044 % parametrization variable for bezier (use N+1 points to have N edges)
0045 t = linspace(0, 1, N+1)';
0046 
0047 % rename points
0048 if size(points, 2)==2
0049     % case of points given as a 4-by-2 array
0050     p1 = points(1,:);
0051     c1 = points(2,:);
0052     c2 = points(3,:);
0053     p2 = points(4,:);
0054 else
0055     % case of points given as a 1-by-8 array, [X1 Y1 CX1 CX2..]
0056     p1 = points(1:2);
0057     c1 = points(3:4);
0058     c2 = points(5:6);
0059     p2 = points(7:8);
0060 end    
0061 
0062 % compute coefficients of Bezier Polynomial, using polyval ordering
0063 coef(4, 1) = p1(1);
0064 coef(4, 2) = p1(2);
0065 coef(3, 1) = 3 * c1(1) - 3 * p1(1);
0066 coef(3, 2) = 3 * c1(2) - 3 * p1(2);
0067 coef(2, 1) = 3 * p1(1) - 6 * c1(1) + 3 * c2(1);
0068 coef(2, 2) = 3 * p1(2) - 6 * c1(2) + 3 * c2(2);
0069 coef(1, 1) = p2(1) - 3 * c2(1) + 3 * c1(1) - p1(1);
0070 coef(1, 2) = p2(2) - 3 * c2(2) + 3 * c1(2) - p1(2); 
0071 
0072 % compute position of vertices
0073 x = polyval(coef(:, 1), t);
0074 y = polyval(coef(:, 2), t);
0075 
0076 if nargout <= 1
0077     varargout = {[x y]};
0078 else
0079     varargout = {x, y};
0080 end

Generated on Wed 16-Feb-2022 15:10:47 by m2html © 2003-2019