]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/drawEllipseArc.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / drawEllipseArc.m
1 %% Copyright (c) 2011, INRA
2 %% 2003-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} = } drawEllipseArc (@var{arc})
36 %% Draw an ellipse arc on the current axis.
37 %%
38 %%   drawEllipseArc(ARC) 
39 %%   draw ellipse arc specified by ARC. ARC has the format:
40 %%     ARC = [XC YC A B THETA T1 T2]
41 %%   or:
42 %%     ARC = [XC YC A B T1 T2] (isothetic ellipse)
43 %%   with center (XC, YC), main axis of half-length A, second axis of
44 %%   half-length B, and ellipse arc running from t1 to t2 (both in degrees,
45 %%   in Counter-Clockwise orientation).
46 %%
47 %%   Parameters can also be arrays. In this case, all arrays are suposed to
48 %%   have the same size...
49 %%
50 %%   @example
51 %%     % draw an ellipse arc: center = [10 20], radii = 50 and 30, theta = 45
52 %%     arc = [10 20 50 30 45 -90 270];
53 %%     figure;
54 %%     axis([-50 100 -50 100]); axis equal;
55 %%     hold on
56 %%     drawEllipseArc(arc, 'color', 'r')
57 %%
58 %%     % draw another ellipse arc, between angles -60 and 70
59 %%     arc = [10 20 50 30 45 -60 (60+70)];
60 %%     figure;
61 %%     axis([-50 100 -50 100]); axis equal;
62 %%     hold on
63 %%     drawEllipseArc(arc, 'LineWidth', 2);
64 %%     ray1 = createRay([10 20], deg2rad(-60+45));
65 %%     drawRay(ray1)
66 %%     ray2 = createRay([10 20], deg2rad(70+45));
67 %%     drawRay(ray2)
68 %% @end example
69 %%
70 %%   @seealso{ellipses2d, drawEllipse, drawCircleArc}
71 %% @end deftypefn
72
73 function varargout = drawEllipseArc(varargin)
74
75   %% Extract input arguments
76
77   % extract dawing style strings
78   styles = {};
79   for i = 1:length(varargin)
80       if ischar(varargin{i})
81           styles = varargin(i:end);
82           varargin(i:end) = [];
83           break;
84       end
85   end
86
87   if length(varargin)==1
88       ellipse = varargin{1};
89       x0 = ellipse(1);
90       y0 = ellipse(2);
91       a  = ellipse(3);
92       b  = ellipse(4);
93       if size(ellipse, 2)>6
94           theta   = ellipse(5);
95           start   = ellipse(6);
96           extent  = ellipse(7);
97       else
98           theta   = zeros(size(x0));
99           start   = ellipse(5);
100           extent  = ellipse(6);
101       end
102       
103   elseif length(varargin)>=6
104       x0 = varargin{1};
105       y0 = varargin{2};
106       a  = varargin{3};
107       b  = varargin{4};
108       if length(varargin)>6
109           theta   = varargin{5};
110           start   = varargin{6};
111           extent  = varargin{7};
112       else
113           theta   = zeros(size(x0));
114           start   = varargin{5};
115           extent  = varargin{6};
116       end
117       
118   else
119       error('drawellipse: please specify center x, center y and radii a and b');
120   end
121
122
123   %% Drawing
124
125   % allocate memory for handles
126   h = zeros(size(x0));
127
128   for i = 1:length(x0)
129       % start and end angles
130       t1 = deg2rad(start);
131       t2 = t1 + deg2rad(extent);
132       
133       % vertices of ellipse
134       t = linspace(t1, t2, 60);
135       
136       % convert angles to ellipse parametrisation
137       sup = cos(t) > 0;
138       t(sup)  = atan(a(i) / b(i) * tan(t(sup)));
139       t(~sup) = atan2(a(i) / b(i) * tan(2*pi - t(~sup)), -1);
140       t = mod(t, 2*pi);
141       
142       % precompute cos and sin of theta (given in degrees)
143       cot = cosd(theta(i));
144       sit = sind(theta(i));
145
146       % compute position of points
147       xt = x0(i) + a(i)*cos(t)*cot - b(i)*sin(t)*sit;
148       yt = y0(i) + a(i)*cos(t)*sit + b(i)*sin(t)*cot;
149       
150       h(i) = plot(xt, yt, styles{:});
151   end
152
153
154   %% Process output arguments
155
156   if nargout > 0
157       varargout = {h};
158   end
159
160 endfunction
161