How to morph a shape into another
Show older comments
I am currently working on morphing a complex closed shape (later I will use patch rather than plot) into another complex shape. For simplicity, I wrote a code which morphed a square shape into a diamond shape. The limitation is that the square has 4 nodes and the corresponding final shape is also 4 nodes. So, it is easy to translate each node using linspace. The code is given below:
close all
clear all
clc
xbox0 = [0 2 2 0 0]; ybox0 = [0 0 2 2 0]; % coordinates of square shape
xdia0 = [4 5 4 3 4]; ydia0 = [0 1 2 1 0]; % coordinates for diamond shape
n = 10; % number steps to transform
%% this loop gives a matrix of the coordinates for each step
for i = 1:length(xbox0)
x1 = xbox0(i);
x2 = xdia0(i);
y1 = ybox0(i);
y2 = ydia0(i);
xpoints = linspace(x1,x2,n)';
ypoints = linspace(y1,y2,n)';
xvector {i} = xpoints;
yvector {i} = ypoints;
end
xfinal = cell2mat(xvector); yfinal = cell2mat(yvector); % converting the stored coordinate cells into a matrix
figure
plot(xbox0,ybox0,'gsquare-') % Plot the original box shape
axis equal
xlim([0 5])
ylim([0 2.5])
hold on
plot(xdia0,ydia0,'gsquare-') % Plot the original diamond shape
%% this loop just animates the intermediate coordinates to the final shape
for j = 1:n
x = xfinal(j,:);
y = yfinal(j,:);
plot(x,y,'rsquare:')
pause(0.1)
end
Now, my problem is that I do not have equal coordinates. For example, I have 6 nodes for the square shape and 5 nodes for the diamond shape. Is there a way to modify (maybe interpolate. I cannot figure out interp1 or 2) into a similar length coordinates and then carry out the aforementioned procedure? Your guidance will be much appreciated
xbox = [0 2 2 2 1 0 0];
ybox = [0 0 1 2 2 2 0];
xdia = [4 4.5 5 4 3 4];
ydia = [0 0.5 1 2 1 0];
figure
hold on
plot(xbox,ybox,'bsquare-')
plot(xdia,ydia,'bsquare-')
axis equal
My thanks in advance. Please forgive me lack of knowledge and correct me if my approach was wrong.
Have a great day
Accepted Answer
More Answers (0)
Categories
Find more on Assembly in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




