필터 지우기
필터 지우기

Moving points together along a specified path

조회 수: 2 (최근 30일)
Ricardo Duarte
Ricardo Duarte 2021년 6월 15일
편집: Abhinaya Kennedy 2024년 5월 15일
Dear all,
I have a question regarding the displacement of a point through time along a specified path.
For example, I created a moving point, along the following path as presented in the figure below:
This is fine, the point is moving along the path as I wanted.
However, now I would like to move more than one point (two or more), side-by-side along the same path.
For example respecting the following configuration:
If fact, I want that that group of 3 points move together along the specified path.
Thank you in advance for your help,
All the best

답변 (1개)

Abhinaya Kennedy
Abhinaya Kennedy 2024년 5월 15일
편집: Abhinaya Kennedy 2024년 5월 15일
Hi Ricardo,
You can move multiple points along a specified path by specifying a series of points that the path will pass through. You can define an "offsets" matrix that stores the initial difference between each point's position and the first waypoint. This captures the relative positions of the points.
% Define waypoints
waypoints = [0, 0; 1, 1; 2, 0];
% Define initial positions of multiple points (3 points in this example)
points = [0.1, 0.1; 0.2, 0.2; 0.3, 0.3];
% Define initial offsets from the path (one offset vector for each point)
offsets = points - waypoints(1, :); % Assuming all points start at the first waypoint
% Number of time steps
numSteps = 100;
Inside the loop, after finding the target waypoint, you can update the positions of all points by adding the target to their respective offsets. This ensures the points move along the path while maintaining their initial configuration relative to the path.
% Loop over time steps
for i = 1:numSteps
% Calculate progress along the path (0 to 1)
t = (i - 1) / (numSteps - 1);
% Find the current target waypoint based on progress
currentWaypointIndex = floor(t * size(waypoints, 1)) + 1;
% Linear interpolation between waypoints
if currentWaypointIndex < size(waypoints, 1)
target = waypoints(currentWaypointIndex, :) + ...
t * (waypoints(currentWaypointIndex + 1, :) - waypoints(currentWaypointIndex, :));
else
target = waypoints(end, :);
end
% Update positions of all points based on target and initial offset
points = target + offsets;
% Plot the points (optional)
plot(points(:, 1), points(:, 2), 'o');
hold on;
axis([min(waypoints(:, 1)) max(waypoints(:, 1)) min(waypoints(:, 2)) max(waypoints(:, 2))]);
pause(0.01);
hold off;
end
Iterate over a time variable and update the positions of your points based on the path definition.
You will have to modify this code according to your needs.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by