Azzera filtri
Azzera filtri

how to move a shape (or a figure) based on a specific motion equation to a point of the graph in real time?

3 visualizzazioni (ultimi 30 giorni)
I want to move a shape (or a figure) based on a specific motion equation to a point of the graph in real time. What code should I write to move a shape in the graph?
same as picture:

Risposta accettata

Hassaan
Hassaan il 30 Dic 2023
Includes the vector field from your original code snippet, and it adds functionality to move a rectangle shape across the field. This code assumes a simple linear motion equation for demonstration purposes, which you will need to replace with your actual motion equation
% Assuming U and V contain the components of the vectors in the vector field
[U, V] = meshgrid(linspace(-0.5, 4.5, 100), linspace(0.5, 4.5, 100));
% Calculate the magnitude of the vectors
M = sqrt(U.^2 + V.^2);
% Find the index of the maximum magnitude
[maxM, maxIdx] = max(M(:));
[maxRow, maxCol] = ind2sub(size(M), maxIdx);
% Get the corresponding X and Y coordinates
maxX = U(maxRow, maxCol);
maxY = V(maxRow, maxCol);
% Plot the vector field
figure;
quiver(U, V, U, V, 'b');
hold on;
% Highlight the maximum magnitude point
plot(maxX, maxY, 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
% Optionally, add a text label
text(maxX, maxY, sprintf('Max Magnitude: %.2f', maxM), ...
'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
% Define the time parameters
startTime = 0;
endTime = 10;
dt = 1; % time step
% Initial position of the shape (rectangle for example)
shapeX = 3;
shapeY = 1.5;
width = 0.5;
height = 0.3;
rectangle = [shapeX-width/2, shapeY-height/2, width, height];
% Plot the initial shape using patch
shapeHandle = patch('Vertices', [rectangle(1), rectangle(2); ...
rectangle(1)+rectangle(3), rectangle(2); ...
rectangle(1)+rectangle(3), rectangle(2)+rectangle(4); ...
rectangle(1), rectangle(2)+rectangle(4)], ...
'Faces', [1, 2, 3, 4], ...
'FaceColor', 'green');
% Define the motion equation (this is an example, you need your own motion equation)
motionEquation = @(x, y, t) deal(x + 0.1*t, y); % Linear motion equation
% Animation loop
for t = startTime:dt:endTime
% Update the position based on the motion equation
[newX, newY] = motionEquation(shapeX, shapeY, t);
% Update the vertices of the shape
newRectangle = [newX-width/2, newY-height/2, width, height];
set(shapeHandle, 'Vertices', [newRectangle(1), newRectangle(2); ...
newRectangle(1)+newRectangle(3), newRectangle(2); ...
newRectangle(1)+newRectangle(3), newRectangle(2)+newRectangle(4); ...
newRectangle(1), newRectangle(2)+newRectangle(4)]);
% Update the plot
drawnow;
% You can add a pause here to slow down the animation, if necessary
pause(dt);
end
hold off;
Here's what the code does:
Creates a vector field using quiver.
Highlights the maximum magnitude point in the vector field.
Initializes the position and properties of a rectangular shape using patch.
Animates the movement of the shape across the graph using a linear motion equation. The motion equation in this example moves the shape horizontally across the graph at a constant speed.
Please replace motionEquation with your actual motion equation to reflect the desired movement. Additionally, the parameters startTime, endTime, and dt can be adjusted based on the time scale of your simulation.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

Più risposte (0)

Categorie

Scopri di più su Animation in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by