Rigidly translating and mirroring 2D coordinates

4 vues (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 2 Jan 2023
Modifié(e) : Matt J le 2 Jan 2023
Hi! I would like to rigidly translate the coordinates of file curve2.txt so that its center of gravity coincides with the center of gravity of file curve1.txt. I tried to use the command "transltform2d" but it gives me an error.
curve1 = importdata("curve1.txt");
x_curve1 = sum(curve1(:,1))/length(curve1(:,1));
y_curve1 = sum(curve1(:,2))/length(curve1(:,2));
G_curve1 = [x_curve1, y_curve1];
curve2 = importdata("curve2.txt");
x_curve2 = sum(curve2(:,1))/length(curve2(:,1));
y_curve2 = sum(curve2(:,2))/length(curve2(:,2));
G_curve2 = [x_curve2, y_curve2];
tx = G_curve2 - G_curve1;
ty = G_curve2 - G_curve1;
tform = transltform2d(tx, ty);
Error using transltform2d
Expected input to be a scalar.
figure
plot(curve1(:,1),curve1(:,2),'k.', 'MarkerSize', 10)
hold on
plot(G_curve1(:,1),G_curve1(:,2),'k.', 'MarkerSize', 15)
plot(curve2(:,1),curve2(:,2),'r.', 'MarkerSize', 10)
plot(G_curve2(:,1),G_curve2(:,2),'r.', 'MarkerSize', 15)
hold off
grid off
axis equal
xlabel('x')
ylabel('y')
And then I should mirror, with respect to the Y axis, curve2.txt with respect to its center of gravity.

Réponse acceptée

Matt J
Matt J le 2 Jan 2023
Modifié(e) : Matt J le 2 Jan 2023
curve1 = importdata("curve1.txt"); curve1(:,3)=[];
curve2 = importdata("curve2.txt");
[ocurve1,ocurve2]=deal(curve1,curve2); %keep a copy of original curves
G=mean(curve1);
curve2=curve2-mean(curve2)+G; %make centroids the same
curve2(:,2)=curve2(:,2) + 2*(G(2)-curve2(:,2)); %reflect across G (y-direction)
tiledlayout(1,2)
nexttile
plot(ocurve1(:,1), ocurve1(:,2),'k.', 'MarkerSize', 10); hold on
plot(ocurve2(:,1), ocurve2(:,2),'r.', 'MarkerSize', 10); hold off
title Original
grid off
axis equal
xlabel('x')
ylabel('y')
nexttile
plot(curve1(:,1), curve1(:,2),'k.', 'MarkerSize', 10); hold on
plot(curve2(:,1), curve2(:,2),'r.', 'MarkerSize', 10); hold off
title Transformed
grid off
axis equal
xlabel('x')
ylabel('y')

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by