]> Creatis software - CreaPhase.git/blob - octave_packages/m/plot/private/__clabel__.m
update packages
[CreaPhase.git] / octave_packages / m / plot / private / __clabel__.m
1 ## Copyright (C) 2008-2012 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{h} =} __clabel__ (@var{c}, @var{v}, @var{hparent}, @var{label_spacing}, @var{z}, @var{varargin})
21 ## Undocumented internal function.
22 ## @end deftypefn
23
24 function h = __clabel__ (c, v, hparent, label_spacing, z, varargin)
25   ## FIXME
26   ## Assume that the plot size is 4 by 3 inches.
27   lims = axis ();
28   xspacing = 72 * 4 / abs(lims(1) - lims(2));
29   yspacing = 72 * 3 / abs(lims(3) - lims(4));
30
31   if (isscalar (hparent) && ishandle(hparent)
32       && strcmp (get (hparent, "type"), "hggroup"))
33     x = get (hparent, "xdata");
34     xmin = min (x(:));
35     xmax = max (x(:));
36     y = get (hparent, "ydata");
37     ymin = min (y(:));
38     ymax = max (y(:));
39   else
40     i1 = 1;
41     while (i1 < length (c))
42       clev = c(1,i1);
43       clen = c(2,i1);
44       p = c(:, i1+1:i1+clen);
45
46       xmin = min (c(1,:));
47       xmax = max (c(1,:));
48       ymin = min (c(2,:));
49       ymax = max (c(2,:));
50
51       i1 += clen+1;
52     endwhile
53   endif
54
55   ## Decode contourc output format and place labels.
56   i1 = 1;
57   h = [];
58   while (i1 < length (c))
59     clev = c(1,i1);
60     clen = c(2,i1);
61
62     if (!isempty (v) && ! any (find (clev == v)))
63       i1 += clen+1;
64       continue;
65     endif
66
67     p = c(:, i1+1:i1+clen) .* repmat ([xspacing; yspacing], 1, clen);
68     d = sqrt (sumsq (diff (p, 1, 2)));
69     cumd = cumsum (d);
70     td = sum(d);
71     ntag = ceil (td / label_spacing);
72
73     if (all (c(:,i1+1) == c(:,i1+clen)))
74       Spacing = td / ntag;
75       pos = Spacing / 2 + [0:ntag-1] * Spacing;
76     else
77       pos = zeros(1, ntag);
78       pos(1) = (td - label_spacing * (ntag - 1)) ./ 2;
79       pos(2:ntag) = pos(1) + [1:ntag-1] * label_spacing;
80     endif
81
82     j1 = 2;
83     tlabel = sprintf ("%g", clev);
84     for i = 1 : ntag
85       tagpos = pos(i);
86
87       while (j1 < clen && cumd(j1) < tagpos)
88         j1++;
89       endwhile
90       tpos = sum(c(:,i1+j1-1:i1+j1), 2) ./ 2;
91
92       if (tpos(1) != xmin &&  tpos(1) != xmax
93           && tpos(2) != ymin &&  tpos(2) != ymax)
94         trot = 180 / pi * atan2 (diff (c(2,i1+j1-1:i1+j1)),
95                                  diff (c(1,i1+j1-1:i1+j1)));
96
97         if (ischar (z))
98           ht = text (tpos(1), tpos(2), clev, tlabel, "rotation", trot,
99                      "parent", hparent, "horizontalalignment", "center",
100                      "userdata", clev, varargin{:});
101         elseif (!isempty (z))
102           ht = text (tpos(1), tpos(2), z, tlabel, "rotation", trot,
103                      "parent", hparent, "horizontalalignment", "center",
104                      "userdata", clev, varargin{:});
105         else
106           ht = text (tpos(1), tpos(2), tlabel, "rotation", trot,
107                      "parent", hparent, "horizontalalignment", "center",
108                      "userdata", clev, varargin{:});
109         endif
110         h = [h; ht];
111       endif
112     endfor
113     i1 += clen+1;
114   endwhile
115 endfunction