]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/drawBezierCurve.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / drawBezierCurve.m
1 %% Copyright (c) 2011, INRA
2 %% 2007-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
4 %%
5 %% All rights reserved.
6 %% (simplified BSD License)
7 %%
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
10 %%
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %%    list of conditions and the following disclaimer.
13 %%     
14 %% 2. Redistributions in binary form must reproduce the above copyright notice, 
15 %%    this list of conditions and the following disclaimer in the documentation
16 %%    and/or other materials provided with the distribution.
17 %%
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% POSSIBILITY OF SUCH DAMAGE.
29 %%
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
33
34 %% -*- texinfo -*-
35 %% @deftypefn {Function File} drawBezierCurve (@var{points})
36 %% @deftypefnx {Command} {Function File} drawBezierCurve (@var{pp})
37 %% @deftypefnx {Command} {Function File} drawBezierCurve (..., @var{param}, @var{value}, ...)
38 %% @deftypefnx {Command} {Function File} {@var{h} =}drawBezierCurve (...)
39 %% Draw a cubic bezier curve defined by the control points @var{points}.
40 %%
41 %% With only one input argument, draws the Bezier curve defined by the 4 control
42 %% points stored in @var{points}. @var{points} is either a 4-by-2 array
43 %% (vertical concatenation of point coordinates), or a 1-by-8 array (horizotnal
44 %% concatenation of point coordinates). The curve could be described by its
45 %% polynomial (output of @code{cbezier2poly}) @var{pp}, which should be a 2-by-4
46 %% array.
47 %%
48 %% The optional @var{param}, @var{value} pairs specify additional drawing
49 %% parameters, see the @code{plot} function for details. The specific parameter
50 %% 'discretization' with an integer associated value defines the amount of
51 %% points used to plot the curve. If the output is requiered, the function
52 %% returns the handle to the created graphic object.
53 %%
54 %% @seealso{cbezier2poly, plot}
55 %% @end deftypefn
56
57 function varargout = drawBezierCurve(points, varargin)
58
59   % default number of discretization steps
60   N = 64;
61
62   % check if discretization step is specified
63   if ~isempty(varargin)
64       [tf idx] = ismember ({'discretization'},{varargin{1:2:end}});
65       if ~isempty(idx)
66         N = varargin{idx+1};
67         varargin(idx:idx+1) = [];
68       end
69   end
70
71   % parametrization variable for bezier (use N+1 points to have N edges)
72   t = linspace(0, 1, N+1);
73   
74   if any(size(points) ~= [2 4])
75     [x y] = cbezier2poly(points,t);
76   else
77     % Got a polynomial description
78     x = polyval(points(1,:),t);
79     y = polyval(points(2,:),t);
80   end
81   
82   % draw the curve
83   h = plot(x, y, varargin{:});
84
85   % eventually return a handle to the created object
86   if nargout > 0
87       varargout = {h};
88   end
89 endfunction
90
91 %!demo
92 %! points = [0 0; 3 1; -2 1; 1 0];
93 %! drawBezierCurve(points);
94 %! hold on
95 %! plot(points([1 4],1),points([1 4],2),'go');
96 %! plot(points([2 3],1),points([2 3],2),'rs');
97 %! line(points([1 2],1),points([1 2],2),'color','k');
98 %! line(points([3 4],1),points([3 4],2),'color','k');
99 %! h = drawBezierCurve(points, 'discretization', 6, 'color','r');
100 %! hold off
101
102 %!shared p
103 %! p = [0 0; 3 1; -2 1; 1 0];
104 %!error(drawBezier())
105 %!error(drawBezier ('discretization'))
106 %!error(drawBezier (p, 'discretization', 'a'))
107 %!error(drawBezier (p(:)))