]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/drawCenteredEdge.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / drawCenteredEdge.m
1 %% Copyright (c) 2011, INRA
2 %% 2005-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} {@var{h} =} drawCenteredEdge (@var{center}, @var{L}, @var{theta})
36 %% @deftypefnx {Function File} {@var{h} =} drawCenteredEdge (@var{edge})
37 %% @deftypefnx {Function File} {@var{h} =} drawCenteredEdge (@dots{}, @var{name},@var{value})
38 %% Draw an edge centered on a point.
39 %%   
40 %%   drawCenteredEdge(CENTER, L, THETA)
41 %%   Draws an edge centered on point CENTER, with length L, and orientation
42 %%   THETA (given in degrees). Input arguments can also be arrays, that must
43 %%   all have the same number odf rows.
44 %%
45 %%   drawCenteredEdge(EDGE)
46 %%   Concatenates edge parameters into a single N-by-4 array, containing:
47 %%   [XC YV L THETA].
48 %%
49 %%   drawCenteredEdge(..., NAME, VALUE)
50 %%   Also specifies drawing options by using one or several parameter name -
51 %%   value pairs (see doc of plot function for details).
52 %%
53 %%   H = drawCenteredEdge(...)
54 %%   Returns handle(s) to the created edges(s).
55 %%
56 %%   @example
57 %%     % Draw an ellipse with its two axes
58 %%     figure(1); clf;
59 %%     center = [50 40];
60 %%     r1 = 30; r2 = 10;
61 %%     theta = 20;
62 %%     elli = [center r1 r2 theta];
63 %%     drawEllipse(elli, 'linewidth', 2);
64 %%     axis([0 100 0 100]); axis equal;
65 %%     hold on;
66 %%     edges = [center 2*r1 theta ; center 2*r2 theta+90];
67 %%     drawCenteredEdge(edges, 'linewidth', 2, 'color', 'g');
68 %% @end example
69 %%
70 %%   @seealso{edges2d, drawEdge}
71 %% @end deftypefn
72
73 function varargout = drawCenteredEdge(center, len, theta, varargin)
74
75   %% process input variables
76
77   if size(center, 2) == 4
78       % manage edge in single parameter
79       
80       varargin = [{len, theta}, varargin];
81
82       len     = center(:, 3);
83       theta   = center(:, 4);
84       center  = center(:, 1:2);
85
86       N = size(center, 1);    
87
88   else
89       % parameters given in different arguments
90       
91       % size of data
92       NP = size(center, 1);
93       NL = size(len, 1);
94       ND = size(theta, 1);
95       N  = max([NP NL ND]);
96
97       % ensure all data have same size
98       if N > 1
99           if NP == 1, center = repmat(center, [N 1]); end
100           if NL == 1, len = repmat(len, [N 1]); end
101           if ND == 1, theta = repmat(theta, [N 1]); end
102       end
103       
104   end
105
106   % extract drawing options
107   options = varargin(:);
108
109
110   %% Draw edges
111
112   % coordinates of center point
113   xc = center(:, 1);
114   yc = center(:, 2);
115
116   % convert angle to radians
117   theta = theta * pi / 180;
118
119   % computation shortcuts
120   cot = cos(theta);
121   sit = sin(theta);
122
123   % compute starting and ending points
124   x1 = xc - len .* cot / 2;
125   x2 = xc + len .* cot / 2;
126   y1 = yc - len .* sit / 2;
127   y2 = yc + len .* sit / 2;
128
129
130   % draw the edges
131   h = zeros(N, 1);
132   for i = 1:N
133       h(i) = plot([x1(i) x2(i)], [y1(i) y2(i)]);
134   end
135
136   % apply style to edges
137   if ~isempty(options) > 0
138       for i = 1:N
139           set(h(i), options{:});
140       end
141   end
142
143
144   %% Format output
145
146   % process output arguments
147   if nargout > 0
148       varargout = {h};
149   end
150
151 endfunction
152