]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/drawLine.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / drawLine.m
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>
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} =} drawLine (@var{line})
36 %% @deftypefnx {Function File} {@var{h} =} drawLine (@var{line}, @var{param},@var{value})
37 %% Draw the line on the current axis.
38 %%
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.
43 %%
44 %% Example
45 %%
46 %% @example
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);
51 %% @end example
52 %%
53 %% @seealso{lines2d, createLine, drawEdge}
54 %% @end deftypefn
55
56 function varargout = drawLine(lin, varargin)
57
58   % default style for drawing lines
59   varargin = [{'color', 'b'}, varargin];
60
61   % extract bounding box of the current axis
62   xlim = get(gca, 'xlim');
63   ylim = get(gca, 'ylim');
64
65   % clip lines with current axis box
66   clip = clipLine(lin, [xlim ylim]);
67   ok   = isfinite(clip(:,1));
68
69   % initialize result array to invalide handles
70   h = -1*ones(size(lin, 1), 1);
71
72   % draw valid lines
73   h(ok) = line(clip(ok, [1 3])', clip(ok, [2 4])', varargin{:});
74
75   % return line handle if needed
76   if nargout>0
77       varargout{1}=h;
78   end
79
80 endfunction
81
82 %!demo
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);
87
88 %!shared privpath
89 %! privpath = [fileparts(which('geom2d_Contents')) filesep() 'private'];
90
91 %!test
92 %!  addpath (privpath,'-end')
93 %!  box = [0 100 0 100];
94 %!  hf = figure('visible','off');
95 %!  axis(box);
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);
102
103 %!test
104 %!  addpath (privpath,'-end')
105 %!  box = [0 100 0 100];
106 %!  hf = figure('visible','off');
107 %!  axis(box);
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);
114
115 %!test
116 %!  addpath (privpath,'-end')
117 %!  box = [0 100 0 100];
118 %!  hf = figure('visible','off');
119 %!  axis(box);
120 %!  line = [30 140 10 0];
121 %!  hl = drawLine(line);
122 %!  assertEqual(-1, hl);
123 %!  rmpath (privpath);
124
125 %!test
126 %!  addpath (privpath,'-end')
127 %!  box = [0 100 0 100];
128 %!  hf = figure('visible','off');
129 %!  axis(box);
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);
136
137 %!test
138 %!  addpath (privpath,'-end')
139 %!  box = [0 100 0 100];
140 %!  hf = figure('visible','off');
141 %!  axis(box);
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);
148
149 %!test
150 %!  addpath (privpath,'-end')
151 %!  box = [0 100 0 100];
152 %!  hf = figure('visible','off');
153 %!  axis(box);
154 %!  line = [140 30 0 10];
155 %!  hl = drawLine(line);
156 %!  assertEqual(-1, hl);
157 %!  rmpath (privpath);
158
159 %!test
160 %!  addpath (privpath,'-end')
161 %!  box = [0 100 0 100];
162 %!  hf = figure('visible','off');
163 %!  axis(box);
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);
170
171 %!test
172 %!  addpath (privpath,'-end')
173 %!  box = [0 100 0 100];
174 %!  hf = figure('visible','off');
175 %!  axis(box);
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);
182
183 %!test
184 %!  addpath (privpath,'-end')
185 %!  box = [0 100 0 100];
186 %!  hf = figure('visible','off');
187 %!  axis(box);
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);
195
196 %!test
197 %!  addpath (privpath,'-end')
198 %!  box = [0 100 0 100];
199 %!  hf = figure('visible','off');
200 %!  axis(box);
201 %!  line = [...
202 %!      80 30 10 10; ...
203 %!      20 70 10 10; ...
204 %!      140 -30 10 10; ...
205 %!      -40 130 10 10];
206 %!  edge = [...
207 %!      50 0 100 50; ...
208 %!      0 50 50 100];
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);
218