Graphics |
Example -- Transforming a Hierarchy of Objects
This example creates a hierarchy of hgtransform objects, which are then transformed in sequence to create a cube from six squares. The example illustrates how you can parent hgtransform objects to other hgtransform objects to create a hierarchy and how transforming members of a hierarchy affects subordinate members.
The following picture illustrates the hierarchy.
Note If you are using the MATLAB Help browser, you can run this example or open it in the MATLAB editor. |
Renderer
property to zbuffer
.
set(gcf,'DoubleBuffer','on');
set(gcf,'Renderer','zbuffer');
% Set axis limits and view
set(gca,'XLim',[0 4], 'YLim',[0 4], 'ZLim', [0 3]);
view(3); axis equal; grid on
t(1) = hgtransform; t(2) = hgtransform('parent',t(1)); t(3) = hgtransform('parent',t(2)); t(4) = hgtransform('parent',t(3)); t(5) = hgtransform('parent',t(4)); t(6) = hgtransform('parent',t(5));
set
. The objects are
then translated to the desired positions on screen.
% Patch data
X = [0 0 1 1]; Y = [0 1 1 0]; Z = [0 0 0 0];% Text data
Xtext = .5; Ytext = .5; Ztext = .15;% Parent corresponding pairs of objects (patch and text)
% into the object hierarchy
p(1) = patch('FaceColor','red','Parent',t(1)); txt(1) = text('String','Bottom','Parent',t(1)); p(2) = patch('FaceColor','green','Parent',t(2)); txt(2) = text('String','Right','Parent',t(2)); p(3) = patch('FaceColor','blue','Parent',t(3)); txt(3) = text('String','Back','Color','white','Parent',t(3)); p(4) = patch('FaceColor','yellow','Parent',t(4)); txt(4) = text('String','Top','Parent',t(4)); p(5) = patch('FaceColor','cyan','Parent',t(5)); txt(5) = text('String','Left','Parent',t(5)); p(6) = patch('FaceColor','magenta','Parent',t(6)); txt(6) = text('String','Front','Parent',t(6));% Set the patch x, y, and z data
set(p,'XData',X,'YData',Y,'ZData',Z)% Set the position and alignment of the text
set(txt,'Position',[Xtext Ytext Ztext],... 'HorizontalAlignment','center',... 'VerticalAlignment','middle')
% Set up initial translation transform matrices
% Translate 1 unit in x
Tx = makehgtform('translate',[1 0 0]);% Translate 1 unit in y
Ty = makehgtform('translate',[0 1 0]);% Set the Matrix property of each hgtransform object (2-6)
set(t(2),'Matrix',Tx); drawnow set(t(3),'Matrix',Ty); drawnow set(t(4),'Matrix',Tx); drawnow set(t(5),'Matrix',Ty); drawnow set(t(6),'Matrix',Tx);
Transforming Objects | Object Properties |
© 1994-2005 The MathWorks, Inc.