问题描述
我想制作动画球(由图片在此给出)从原点开始并经过x矢量,y矢量,z矢量(分别为nX1)给定的轨迹.我知道我需要使用getframe命令,但不知道如何在轴上移动图片.我知道我可以通过定义新轴来将图片放在一个角上,例如(摘自MATLAB官方论坛):
I want to make animation of ball (given by the picture here) which starts from origin and goes through a track given by x-vector, y-vector, z-vector (each of nX1).I know I need to use the getframe command but I don't know how to move the picture on the axis. I know that I can put a picture in one of the corner by defining new axis, e.g (exmaple taken from MATLAB offical forum):
numberOfDataPoints = 200;
sampleData = 100*rand(1,numberOfDataPoints);
plot(sampleData);
xlim([1, numberOfDataPoints]);
hold on;
plot(sampleData);
xlim([1, numberOfDataPoints]);
axes1Position = get(gca, 'Position');
logoSizeX = 0.1;
logoSizeY = 0.1;
% Position the logo in the upper right.
x1 = axes1Position(1) + axes1Position(3) - logoSizeX;
y1 = axes1Position(2) + axes1Position(4) - logoSizeY;
hAxis2 = axes('Position', [x1 y1 logoSizeX logoSizeY]);
axis off;
imshow(ball.jpeg);
但是由于我不想创建单独的轴,所以这无济于事.如何定义我的球在给定轴上的运动?
but since I don't want to create seperate axis, this does not help. How can I define movement of my ball on a given axis?
推荐答案
您可以通过存储图像绘制函数返回的句柄并设置其'XData'
,'YData'
和'ZData'
属性来移动对象.这是一个小例子;本示例使用warp
在球形表面(使用sphere
生成)上绘制图像,然后将其围绕随机路径移动.
You can move the object by storing the handle returned by the image drawing function and setting its 'XData'
, 'YData'
, and 'ZData'
properties. Here is a little example; this example uses warp
to draw the image on a spherical surface (generated using sphere
), and then moves it around a random path.
close all;
% Load image
[img, imgMap] = imread('peppers.png');
sphereImgSize = min(size(img, 1), size(img, 2));
sphereImg = img(1:sphereImgSize, 1:sphereImgSize, :);
% Generate sphere vertices
[X, Y, Z] = sphere(sphereImgSize);
lims = [-10 10];
figure;
axes;
hImg = warp(X, Y, Z, sphereImg); % NOTE: Store handle returned
xlim(lims);
ylim(lims);
zlim(lims);
axis square;
% Set up movement path
nFrames = 100;
randPathFun = @()rand(nFrames, 1) * diff(lims) + lims(1);
pathX = randPathFun();
pathY = randPathFun();
pathZ = randPathFun();
% Move the image by setting its 'XData' and 'YData' properties
for ii = 1:nFrames
xData = X + pathX(ii);
yData = Y + pathY(ii);
zData = Z + pathZ(ii);
set(hImg, 'XData', xData, 'YData', yData, 'ZData', zData);
pause(0.1);
end
这篇关于将图片放置在MATLAB图形的轴上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!