Filter löschen
Filter löschen

Rotate geometry at one specified point

2 Ansichten (letzte 30 Tage)
ka chun yick
ka chun yick am 20 Aug. 2021
Bearbeitet: Wan Ji am 21 Aug. 2021
Hi,
I wanted to rotate the inner rectangle (only) to 45 degree, which (p1(1),p1(2)) as the fixed point as rotation. I am not sure how to do it by the coordinates - the points are the same as before. This is my code:
function Nano_Sphere_Dat_Generator
clear
clc
clf
%set value of geometric parameters
a=0.0;
b=0.0;
c=1000;
plate_length=30000;
plate_width=1000;
pos_CNN=10000;
channel_length=100000;
channel_width=60000;
upstream_dist=30000;
CNN_width=0.001;
%create the outer boundary
p1(1)=a-upstream_dist;
p1(2)=-channel_width/2;
p2(1)=a-upstream_dist+channel_length;
p2(2)=-channel_width/2;
p3(1)=a-upstream_dist+channel_length;
p3(2)=channel_width/2;
p4(1)=a-upstream_dist;
p4(2)=channel_width/2;
plot([p1(1),p2(1)],[p1(2),p2(2)])
hold on
plot([p2(1),p3(1)],[p2(2),p3(2)])
plot([p3(1),p4(1)],[p3(2),p4(2)])
plot([p4(1),p1(1)],[p4(2),p1(2)])
axis equal
%create the plate geometry
p5(1)=a;
p5(2)=b;
p6(1)=a+pos_CNN;
p6(2)=b;
p7(1)=a+pos_CNN+CNN_width;
p7(2)=b;
p8(1)=a+plate_length;
p8(2)=b;
p9(1)=a+plate_length;
p9(2)=b-plate_width;
p10(1)=a
p10(2)=b-plate_width;
x = [p6(1) p7(1) p8(1) p9(1) p10(1)];
y = [p6(2) p7(2) p8(2) p9(2) p10(2)];
h = scatter(x,y);
h1 = rotate(h,15,[0 0 0])
scatter(p5(1),p5(2))
scatter(p6(1),p6(2))
scatter(p7(1),p7(2))
scatter(p8(1),p8(2))
scatter(p9(1),p9(2))
scatter(p10(1),p10(2))

Antworten (1)

Wan Ji
Wan Ji am 20 Aug. 2021
Here I added the rotation method for you
function Nano_Sphere_Dat_Generator
clear
clc
clf
%set value of geometric parameters
a=0.0;
b=0.0;
c=1000;
plate_length=30000;
plate_width=1000;
pos_CNN=10000;
channel_length=100000;
channel_width=60000;
upstream_dist=30000;
CNN_width=0.001;
%create the outer boundary
p1(1)=a-upstream_dist;
p1(2)=-channel_width/2;
p2(1)=a-upstream_dist+channel_length;
p2(2)=-channel_width/2;
p3(1)=a-upstream_dist+channel_length;
p3(2)=channel_width/2;
p4(1)=a-upstream_dist;
p4(2)=channel_width/2;
plot([p1(1),p2(1),p3(1), p4(1), p1(1)],[p1(2),p2(2),p3(2),p4(2),p1(2)],'r-')
hold on
axis equal
%create the plate geometry
p5(1)=a;
p5(2)=b;
p6(1)=a+pos_CNN;
p6(2)=b;
p7(1)=a+pos_CNN+CNN_width;
p7(2)=b;
p8(1)=a+plate_length;
p8(2)=b;
p9(1)=a+plate_length;
p9(2)=b-plate_width;
p10(1)=a;
p10(2)=b-plate_width;
% Here is how to rotate
x = [p5(1),p6(1) p7(1) p8(1) p9(1) p10(1)];
y = [p5(2),p6(2) p7(2) p8(2) p9(2) p10(2)];
xr = x - p1(1);
yr = y - p1(2);
theta = 45 * pi /180; % rotate with 45 degrees
xrot = xr*cos(theta) - yr*sin(theta) + p1(1);
yrot = xr*sin(theta) + yr*cos(theta) + p1(2);
scatter(x,y,'g'); % before rotation
scatter(xrot,yrot,'c'); % scatter the points after rotate
legend('plate boundary', 'before rotation','after rotation')
The figure becomes
  3 Kommentare
Adam Danz
Adam Danz am 20 Aug. 2021
+1
An alternative method that uses the values stored in the scatter handle,
h = scatter(x,y);
zRotMat = @(th)[cos(th) -sin(th); sin(th) cos(th)];
xyRot = zRotMat(45*pi/180) * ([h.XData; h.YData]-p1(:)) + p1(:);
set(h, 'XData', xyRot(1,:), 'YData', xyRot(2,:))
Wan Ji
Wan Ji am 20 Aug. 2021
Bearbeitet: Wan Ji am 21 Aug. 2021
there are 2 place to change the code
xr = x - p10(1);
yr = y - p10(2);
theta = 45 * pi /180; % rotate with 45 degrees
xrot = xr*cos(theta) - yr*sin(theta) + p10(1);
yrot = xr*sin(theta) + yr*cos(theta) + p10(2);

Melden Sie sich an, um zu kommentieren.

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by