1## Copyright (C) 2021 David Legland
2## All rights reserved.
3##
4## Redistribution and use in source and binary forms, with or without
5## modification, are permitted provided that the following conditions are met:
6##
7##     1 Redistributions of source code must retain the above copyright notice,
8##       this list of conditions and the following disclaimer.
9##     2 Redistributions in binary form must reproduce the above copyright
10##       notice, this list of conditions and the following disclaimer in the
11##       documentation and/or other materials provided with the distribution.
12##
13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23##
24## The views and conclusions contained in the software and documentation are
25## those of the authors and should not be interpreted as representing official
26## policies, either expressed or implied, of the copyright holders.
27
28function varargout = drawBezierCurve(points, varargin)
29%DRAWBEZIERCURVE Draw a cubic bezier curve defined by 4 control points.
30%
31%   drawBezierCurve(POINTS)
32%   Draw the Bezier curve defined by the 4 control points stored in POINTS.
33%   POINTS is either a 4-by-2 array (vertical concatenation of control
34%   points coordinates), or a 1-by-8 array (horizontal concatenation of
35%   control point coordinates).
36%
37%   drawBezierCurve(..., PARAM, VALUE)
38%   Specifies additional drawing parameters, see the line function for
39%   details.
40%
41%   drawBezierCurve(AX, ...);
42%   Spcifies the handle of the axis to draw on.
43%
44%   H = drawBezierCurve(...);
45%   Return a handle to the created graphic object.
46%
47%
48%   Example
49%     drawBezierCurve([0 0;5 10;10 5;10 0]);
50%     drawBezierCurve([0 0;5 10;10 5;10 0], 'linewidth', 2, 'color', 'g');
51%
52%   See also
53%     drawPolyline, cubicBezierToPolyline
54%
55% ------
56% Author: David Legland
57% e-mail: david.legland@grignon.inra.fr
58% Created: 2011-03-16,    using Matlab 7.9.0.529 (R2009b)
59% Copyright 2011 INRA - Cepia Software Platform.
60
61%   HISTORY
62%   2011-10-11 add management of axes handle
63
64% extract handle of axis to draw on
65if isAxisHandle(points)
66    ax = points;
67    points = varargin{1};
68    varargin(1) = [];
69else
70    ax = gca;
71end
72
73% default number of discretization steps
74N = 64;
75
76% check if discretization step is specified
77if ~isempty(varargin)
78    var = varargin{1};
79    if length(var) == 1 && isnumeric(var)
80        N = round(var);
81        varargin(1) = [];
82    end
83end
84
85% convert control coordinates to polyline
86poly = cubicBezierToPolyline(points, N);
87
88% draw the curve
89h = drawPolyline(ax, poly, varargin{:});
90
91% eventually return a handle to the created object
92if nargout > 0
93    varargout = {h};
94end
95