Need help moving text inside of a figure window
28 次查看(过去 30 天)
显示 更早的评论
Hey, Im trying to move a person's name (using input) and moving around in different directions in a figure window, but the text must hit the edges of the figure window like an old DVD logo type thing. Here is what i got so far:
%
clear
close all
clc
x = 0;
y = 0;
Name = input('Please input your name: ','s');
TH = text(x,y,Name);
axis([-1 1 -1 1]);
for i = 1:360
x = x + 0.01;
y= y + 0.01
set(TH,'Position', [x y 0]);
pause(0.01);
end
Thanks!
0 个评论
采纳的回答
Sandro Lecci
2018-5-23
编辑:Sandro Lecci
2018-5-23
Dear Jacob,
In your code you miss the instruction to change the direction of the text. The text object is defined only by the coordinates of the bottom left corner, so one would need to calculate the size of the text box and modify the value of the edge where the direction has to be changed. Therefore, long names need a "lower edge value" on the right.
clear
close all
clc
x = 0.4;
y = 0.0;
Name = input('Please input your name: ','s');
TH = text(x,y,Name);
axis([-1.3 1.3 -1 1]); %you need a rectangle for the "DVD-effect"
i = 1;
%define initial direction (up-right)
dx = 0.01;
dy = 0.01;
while i == 1
% Change UP-DOWN
if y == 0.95 %instruction to go down
dy = -0.01; %delta y
elseif y == -1%instruction to go up
dy = 0.01;
end
% Change LEFT-RIGHT
if x == -1.3 %instruction to turn right
dx = 0.01;
elseif x == 1.25 % instruction to turn left
dx = -0.01;
end
% Calculate new positions
x = x + dx;
y = y + dy;
x = round(x, 2);
y = round(y, 2);
%set new position
set(TH,'Position', [x y 0]);
pause(0.01);
end
The code above works but is not optimal, I let you find the best parameters.
Best, Sandro
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!