comparison scripts/plot/__actual_axis_position__.m @ 8890:ae51d068bbd5

__actual_axis_position__.m: New function to determine position of rendered axes.
author Ben Abbott <bpabbott@mac.com>
date Sat, 28 Feb 2009 22:39:33 -0500
parents
children
comparison
equal deleted inserted replaced
8889:665b264b6a50 8890:ae51d068bbd5
1 ## Copyright (C) 2009 Ben Abbott
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} {} __actual_axis_position__ (@var{h})
21 ## @deftypefnx {Function File} {} __actual_axis_position__ (@var{axis_struct})
22 ## Undocumented internal function.
23 ## @end deftypefn
24
25 ## Author: Ben Abbott
26
27 function pos = __actual_axis_position__ (axis_obj)
28 if (ishandle (axis_obj))
29 axis_obj = get (axis_obj);
30 endif
31
32 ## When using {rltb}margin, Gnuplot does not handle the specified
33 ## aspect ratio properly, so handle it here.
34 if (__calc_dimensions__ (axis_obj) == 3)
35 ## FIXME -- this works for "axis square", but has not been
36 ## thoroughly tested for other aspect ratios.
37 aspect_ratio_2d = [max(axis_obj.dataaspectratio(1:2)), ...
38 axis_obj.dataaspectratio(3)/sqrt(2)];
39 else
40 aspect_ratio_2d = axis_obj.dataaspectratio(1:2);
41 endif
42 orig_fig_units = get (axis_obj.parent, "units");
43 orig_fig_position = get (axis_obj.parent, "units");
44 unwind_protect
45 set (axis_obj.parent, "units", "pixels")
46 fig_position = get (axis_obj.parent, "position");
47 pos_in_pixels = axis_obj.position .* fig_position([3, 4, 3, 4]);
48 orig_aspect_ratio_2d = pos_in_pixels(3:4);
49 rel_aspect_ratio_2d = aspect_ratio_2d ./ orig_aspect_ratio_2d;
50 rel_aspect_ratio_2d = rel_aspect_ratio_2d ./ max (rel_aspect_ratio_2d);
51 if (rel_aspect_ratio_2d(1) < rel_aspect_ratio_2d(2));
52 dx = (1.0 - rel_aspect_ratio_2d(1)) * pos_in_pixels(3);
53 pos_in_pixels = pos_in_pixels + dx*[0.5, 0.0, -1.0, 0.0];
54 elseif (rel_aspect_ratio_2d(1) > rel_aspect_ratio_2d(2))
55 dy = (1.0 - rel_aspect_ratio_2d(2)) * pos_in_pixels(4);
56 pos_in_pixels = pos_in_pixels + dy*[0.0, 0.5, 0.0, -1.0];
57 endif
58 pos = pos_in_pixels ./ fig_position([3, 4, 3, 4]);
59 unwind_protect_cleanup
60 set (axis_obj.parent, "units", orig_fig_units)
61 set (axis_obj.parent, "units", orig_fig_position)
62 end_unwind_protect
63
64 endfunction
65
66 function nd = __calc_dimensions__ (obj)
67 kids = obj.children;
68 nd = 2;
69 for i = 1:length (kids)
70 obj = get (kids(i));
71 switch (obj.type)
72 case {"image", "text"}
73 ## ignore as they
74 case {"line", "patch"}
75 if (! isempty (obj.zdata))
76 nd = 3;
77 endif
78 case "surface"
79 nd = 3;
80 case "hggroup"
81 obj_nd = __calc_dimensions__ (obj);
82 if (obj_nd == 3)
83 nd = 3;
84 endif
85 endswitch
86 endfor
87 endfunction
88