Main Content

복합적인 이동을 위해 변환 중첩

이 예제는 transform 객체가 중첩 계층 구조를 생성하고, 이렇게 생성된 계층 구조는 차례로 변환되어 6개의 정사각형으로 정육면체를 생성합니다. 이 예제에서는 transform 객체의 부모를 다른 transform 객체로 지정하여 계층 구조를 생성하는 방법과 계층 구조의 멤버를 변환할 경우 종속 멤버에 어떻게 영향을 미치는지를 보여줍니다.

다음은 그 계층 구조를 보여주는 그림입니다.

transform_foldbox 함수는 변환 계층 구조를 구현합니다. doUpdate 함수는 각 단계를 렌더링합니다. transform_foldbox.m 파일에 두 함수를 모두 배치하고 transform_foldbox를 실행합니다.

function transform_foldbox
   % Create six square and fold
   % them into a cube
   
   figure
   
   % Set axis limits and view
   axes('Projection','perspective',...
      'XLim',[0 4],...
      'YLim',[0 4],...
      'ZLim',[0 3])
   view(3); axis equal; grid on
   
   % Create a hierarchy of transform objects
   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));
   
   % 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;
   
   % Corresponding pairs of objects (patch and text)
   % are parented 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));
   
   % All the patch objects use the same x, y, and z data
   set(p,'XData',X,'YData',Y,'ZData',Z)
   
   % Set the position and alignment of the text objects
   set(txt,'Position',[Xtext Ytext Ztext],...
      'HorizontalAlignment','center',...
      'VerticalAlignment','middle')
   
   % Display the objects in their current location
   doUpdate(1)
   
   % Set up initial translation transforms
   % Translate 1 unit in x
   Tx = makehgtform('translate',[1 0 0]);
   % Translate 1 unit in y
   Ty = makehgtform('translate',[0 1 0]);
   
   % Translate the unit squares to the desired locations
   % The drawnow and pause commands display
   % the objects after each translation
   set(t(2),'Matrix',Tx);
   doUpdate(1)
   set(t(3),'Matrix',Ty);
   doUpdate(1)
   set(t(4),'Matrix',Tx);
   doUpdate(1)
   set(t(5),'Matrix',Ty);
   doUpdate(1)
   set(t(6),'Matrix',Tx);
   doUpdate(1)
   
   % Specify rotation angle (pi/2 radians = 90 degrees)
   fold = pi/2;
   
   % Rotate -y, translate x
   Ry = makehgtform('yrotate',-fold);
   RyTx = Tx*Ry;
   
   % Rotate x, translate y
   Rx = makehgtform('xrotate',fold);
   RxTy = Ty*Rx;
   
   % Set the transforms
   % Draw after each group transform and pause
   set(t(6),'Matrix',RyTx);
   doUpdate(1)
   set(t(5),'Matrix',RxTy);
   doUpdate(1)
   set(t(4),'Matrix',RyTx);
   doUpdate(1)
   set(t(3),'Matrix',RxTy);
   doUpdate(1)
   set(t(2),'Matrix',RyTx);
   doUpdate(1)
end
   
function doUpdate(delay)
   drawnow
   pause(delay)
end