Giving a moving point a chance to change path every step?
Mostra commenti meno recenti
Here is the current code I have working, but simply put it will randomise 4 coordinates, b, c, d and e, and then animate drawing a line connecting each of them in a given number of steps.
b1 = randi([0 7e5])
b2 = randi([0 7e5])
c1 = randi([0 7e5])
c2 = randi([0 7e5])
d1 = randi([0 7e5])
d2 = randi([0 7e5])
e1 = randi([0 7e5])
e2 = randi([0 7e5])
a = [0,0];
b = [b1,b2];
c = [c1,c2];
d = [d1,d2];
e = [e1,e2];
% straight line function from a to b
func1 = @(x)a(2) + (a(2)-b(2))/(a(1)-b(1))*(x-a(1));
% straight line function from b to c
func2 = @(x)b(2) + (b(2)-c(2))/(b(1)-c(1))*(x-b(1));
% straight line function from c to d
func3 = @(x)c(2) + (c(2)-d(2))/(c(1)-d(1))*(x-c(1));
% straight line function from d to e
func4 = @(x)d(2) + (d(2)-e(2))/(d(1)-e(1))*(x-d(1));
% determine the x values
x1 = linspace(a(1),b(1),20);
x2 = linspace(b(1),c(1),20);
x3 = linspace(c(1),d(1),20);
x4 = linspace(d(1),e(1),20);
% determine the y values
y1 = func1(x1);
y2 = func2(x2);
y3 = func3(x3);
y4 = func4(x4);
% create the figure
figure;
% get a handle to a plot graphics object
hPlot = plot(NaN,NaN,'ro');
% set the axes limits
% iterate through each point on line
for k=1:length(y1)
% update the plot graphics object with the next position
set(hPlot,'XData',x1(k),'YData',y1(k));
% set dotted line
hold on
plot([x1(1), x1(k)], [y1(1), y1(k)], '--k')
xlim([min(0) max(8e5)]);
ylim([min(0) max(8e5)]);
hold off
% pause for 0.1 seconds
pause(0.1);
end
for k=1:length(y2)
% update the plot graphics object with the next position
set(hPlot,'XData',x2(k),'YData',y2(k));
% set dotted line
hold on
plot([x2(1), x2(k)], [y2(1), y2(k)], '--k')
xlim([min(0) max(8e5)]);
ylim([min(0) max(8e5)]);
hold off
% pause for 0.1 seconds
pause(0.1);
end
for k=1:length(y3)
% update the plot graphics object with the next position
set(hPlot,'XData',x3(k),'YData',y3(k));
% set dotted line
hold on
plot([x3(1), x3(k)], [y3(1), y3(k)], '--k')
xlim([min(0) max(8e5)]);
ylim([min(0) max(8e5)]);
hold off
% pause for 0.1 seconds
pause(0.1);
end
for k=1:length(y4)
% update the plot graphics object with the next position
set(hPlot,'XData',x4(k),'YData',y4(k));
% set dotted line
hold on
plot([x4(1), x4(k)], [y4(1), y4(k)], '--k')
xlim([min(0) max(8e5)]);
ylim([min(0) max(8e5)]);
hold off
% pause for 0.1 seconds
pause(0.1);
end
I'm looking for a way which will allow me to input a target value, say 8e5, and the line will start plotting to that point in a given number steps and each step there is a percentage chance that a new target will be generated and the point will change direction and start moving to the new target which would then repeat a given number of times.
Is this possible?
Thanks
Risposta accettata
Più risposte (2)
Paul Hoffrichter
il 1 Feb 2021
1 voto
You should just run the script directly. You should not try to call selectPath from the command line.
1 Commento
Jack Rimmer
il 1 Feb 2021
Steven Lord
il 1 Feb 2021
1 voto
Rather than creating a large number of small line segments, you may want to look at either the animatedline function (and the addpoints function associated with the lines returned by animatedline) or maybe the comet function.
Categorie
Scopri di più su MATLAB in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
