1 %% Copyright (c) 2011, INRA
2 %% 2004-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
5 %% All rights reserved.
6 %% (simplified BSD License)
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %% list of conditions and the following disclaimer.
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.
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.
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.
35 %% @deftypefn {Function File} {@var{h} =} drawLine (@var{line})
36 %% @deftypefnx {Function File} {@var{h} =} drawLine (@var{line}, @var{param},@var{value})
37 %% Draw the line on the current axis.
39 %% Draws the line LINE on the current axis, by using current axis to clip
40 %% the line. Extra @var{param},@var{value} pairs are passed to the @code{line} function.
41 %% Returns a handle to the created line object. If clipped line is not
42 %% contained in the axis, the function returns -1.
47 %% figure; hold on; axis equal;
48 %% axis([0 100 0 100]);
49 %% drawLine([30 40 10 20]);
50 %% drawLine([30 40 20 -10], 'color', 'm', 'linewidth', 2);
53 %% @seealso{lines2d, createLine, drawEdge}
56 function varargout = drawLine(lin, varargin)
58 % default style for drawing lines
59 varargin = [{'color', 'b'}, varargin];
61 % extract bounding box of the current axis
62 xlim = get(gca, 'xlim');
63 ylim = get(gca, 'ylim');
65 % clip lines with current axis box
66 clip = clipLine(lin, [xlim ylim]);
67 ok = isfinite(clip(:,1));
69 % initialize result array to invalide handles
70 h = -1*ones(size(lin, 1), 1);
73 h(ok) = line(clip(ok, [1 3])', clip(ok, [2 4])', varargin{:});
75 % return line handle if needed
83 %! figure; hold on; axis equal;
84 %! axis([0 100 0 100]);
85 %! drawLine([30 40 10 20]);
86 %! drawLine([30 40 20 -10], 'color', 'm', 'linewidth', 2);
89 %! privpath = [fileparts(which('geom2d_Contents')) filesep() 'private'];
92 %! addpath (privpath,'-end')
93 %! box = [0 100 0 100];
94 %! hf = figure('visible','off');
96 %! line = [30 40 10 0];
97 %! edge = [0 40 100 40];
98 %! hl = drawLine(line);
99 %! assertElementsAlmostEqual(edge([1 3]), get(hl, 'xdata'));
100 %! assertElementsAlmostEqual(edge([2 4]), get(hl, 'ydata'));
101 %! rmpath (privpath);
104 %! addpath (privpath,'-end')
105 %! box = [0 100 0 100];
106 %! hf = figure('visible','off');
108 %! line = [30 40 -10 0];
109 %! edge = [100 40 0 40];
110 %! hl = drawLine(line);
111 %! assertElementsAlmostEqual(edge([1 3]), get(hl, 'xdata'));
112 %! assertElementsAlmostEqual(edge([2 4]), get(hl, 'ydata'));
113 %! rmpath (privpath);
116 %! addpath (privpath,'-end')
117 %! box = [0 100 0 100];
118 %! hf = figure('visible','off');
120 %! line = [30 140 10 0];
121 %! hl = drawLine(line);
122 %! assertEqual(-1, hl);
123 %! rmpath (privpath);
126 %! addpath (privpath,'-end')
127 %! box = [0 100 0 100];
128 %! hf = figure('visible','off');
130 %! line = [30 40 0 10];
131 %! edge = [30 0 30 100];
132 %! hl = drawLine(line);
133 %! assertElementsAlmostEqual(edge([1 3]), get(hl, 'xdata'));
134 %! assertElementsAlmostEqual(edge([2 4]), get(hl, 'ydata'));
135 %! rmpath (privpath);
138 %! addpath (privpath,'-end')
139 %! box = [0 100 0 100];
140 %! hf = figure('visible','off');
142 %! line = [30 40 0 -10];
143 %! edge = [30 100 30 0];
144 %! hl = drawLine(line);
145 %! assertElementsAlmostEqual(edge([1 3]), get(hl, 'xdata'));
146 %! assertElementsAlmostEqual(edge([2 4]), get(hl, 'ydata'));
147 %! rmpath (privpath);
150 %! addpath (privpath,'-end')
151 %! box = [0 100 0 100];
152 %! hf = figure('visible','off');
154 %! line = [140 30 0 10];
155 %! hl = drawLine(line);
156 %! assertEqual(-1, hl);
157 %! rmpath (privpath);
160 %! addpath (privpath,'-end')
161 %! box = [0 100 0 100];
162 %! hf = figure('visible','off');
164 %! line = [80 30 10 10];
165 %! edge = [50 0 100 50];
166 %! hl = drawLine(line);
167 %! assertElementsAlmostEqual(edge([1 3]), get(hl, 'xdata'));
168 %! assertElementsAlmostEqual(edge([2 4]), get(hl, 'ydata'));
169 %! rmpath (privpath);
172 %! addpath (privpath,'-end')
173 %! box = [0 100 0 100];
174 %! hf = figure('visible','off');
176 %! line = [20 70 10 10];
177 %! edge = [0 50 50 100];
178 %! hl = drawLine(line);
179 %! assertElementsAlmostEqual(edge([1 3]), get(hl, 'xdata'));
180 %! assertElementsAlmostEqual(edge([2 4]), get(hl, 'ydata'));
181 %! rmpath (privpath);
184 %! addpath (privpath,'-end')
185 %! box = [0 100 0 100];
186 %! hf = figure('visible','off');
188 %! line = [140 -30 10 10];
189 %! hl = drawLine(line);
190 %! assertEqual(-1, hl);
191 %! line = [-40 130 10 10];
192 %! hl = drawLine(line);
193 %! assertEqual(-1, hl);
194 %! rmpath (privpath);
197 %! addpath (privpath,'-end')
198 %! box = [0 100 0 100];
199 %! hf = figure('visible','off');
204 %! 140 -30 10 10; ...
209 %! hl = drawLine(line);
210 %! assertEqual(4, length(hl));
211 %! assertElementsAlmostEqual(edge(1, [1 3]), get(hl(1), 'xdata'));
212 %! assertElementsAlmostEqual(edge(1, [2 4]), get(hl(1), 'ydata'));
213 %! assertElementsAlmostEqual(edge(2, [1 3]), get(hl(2), 'xdata'));
214 %! assertElementsAlmostEqual(edge(2, [2 4]), get(hl(2), 'ydata'));
215 %! assertEqual(-1, hl(3));
216 %! assertEqual(-1, hl(4));
217 %! rmpath (privpath);