How to apply a transformation with makehgtform()?
이전 댓글 표시
I would like to learn how to apply transformations with makehgtform(). This is toy example:
% Create a red octagon using the fill function.
t = (1/16:1/8:1)'*2*pi;
x = cos(t);
y = sin(t);
fill(x,y,'r');
Now instead of x=x/2 and y=y/2 and replot, I would like to apply makehgtform('scale',0.5), but how to use it? how to plot the new octagon?
답변 (1개)
Mike Garrity
2015년 9월 30일
1 개 추천
I just put a post up on the MATLAB graphics blog that covers the two different ways you can use that matrix, and quite a lot of the theory behind the 4x4 representation.
댓글 수: 6
Mr M.
2015년 9월 30일
Mike Garrity
2015년 9월 30일
Ah, so you this means that you want to use the hgtransform technique.
So the basic idea is that you create an instance of the hgtransform object like this:
g = hgtransform;
That got added to the axes, because that's always the default. Now if we add objects to this hgtransform, then they'll get drawn using the transform matrix of the hgtransform object. So if we now do this:
t = (1/16:1/8:1)'*2*pi;
x = cos(t);
y = sin(t);
fill(x,y,'r','Parent',g);
we've told fill to put the object it creates in that hgtransform. This means that if we change the Matrix property, the patch that fill created will be transformed:
g.Matrix = makehgtform('zrotate',pi/3);
Let's look at the tree of objects:
>> ax=gca
ax =
Axes with properties:
XLim: [-1 1]
YLim: [-1 1]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.1300 0.1100 0.7750 0.8150]
Units: 'normalized'
Show all properties
>> ax.Children
ans =
Transform with properties:
Children: [1x1 Patch]
Visible: 'on'
HitTest: 'on'
Matrix: [4x4 double]
Show all properties
>> ax.Children.Children
ans =
Patch with properties:
FaceColor: [1 0 0]
FaceAlpha: 1
EdgeColor: [0 0 0]
LineStyle: '-'
Faces: [1 2 3 4 5 6 7 8]
Vertices: [8x2 double]
Show all properties
So we have an Axes object, which has one child which is an HGTransform object, which has one child which is a Patch object.
If we had done this instead:
clf
fill(x,y,'b')
g = hgtransform;
fill(x,y,'r','Parent',g)
g.Matrix = makehgtform('zrotate',pi/3);
Then the red patch would be rotated (because it's parented to the hgtransform), but the blue one would not (because it's parented directly to the axes). And if we look at the Children of the Axes object, we see this:
>> ax.Children
ans =
2x1 graphics array:
Transform
Patch
Does that help?
Mr M.
2015년 9월 30일
Mike Garrity
2015년 9월 30일
I'm not sure, and I don't have an old version handy. My guess would be that you've copied some of the "dot notation" I used such as g.Matrix and ax.Children. That syntax was introduced in R2014b, so you would need to replace it with calls to set and get. If you use it in older versions, you'll often end up with structs replacing your object handles.
Mr M.
2015년 9월 30일
Mr M.
2015년 9월 30일
카테고리
도움말 센터 및 File Exchange에서 Object Containers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!