how to plote a square that rotate around a square
1 view (last 30 days)
Show older comments
Muhammad Ikram
on 12 Jul 2020
Commented: Muhammad Ikram
on 12 Jul 2020
hi !
i want to creat a square the rotate around a square
pleace help me in it
0 Comments
Accepted Answer
Image Analyst
on 12 Jul 2020
Try this:
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
numPoints = 10;
bigWidth = 30
smallWidth = 2
cols = linspace(1, bigWidth, numPoints);
rows = linspace(1, bigWidth, numPoints);
bigX = [cols, bigWidth * ones(1, numPoints), fliplr(cols), ones(1, numPoints)];
bigY = [bigWidth * ones(1, numPoints), fliplr(rows), ones(1, numPoints), rows];
plot(bigX, bigY, 'b.-', 'MarkerSIze', 20);
grid on;
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
xlim([-5, 35]);
ylim([-5, 35]);
hold on;
% Go around the points on the square drawing a small square that rotates.
angles = linspace(0, 90, numPoints*4)
for k = 1 : length(bigX)
smallX1 = -smallWidth/2;
smallY1 = -smallWidth/2;
smallX2 = smallWidth/2;
smallY2 = smallWidth/2;
% Make small rectangle centered at the origin, adding a fifth point to close off the rectangle.
smallRect = [smallX1, smallY1; smallX2, smallY1; smallX2, smallY2; smallX1, smallY2; smallX1, smallY1];
angle = angles(k);
rotationMatrix = [cosd(angle) -sind(angle) ;
sind(angle) cosd(angle)] ;
smallRect = smallRect * rotationMatrix ;
% Add offset to center the small rectangle at the large rectangle's point.
smallRect(:, 1) = smallRect(:, 1) + bigX(k);
smallRect(:, 2) = smallRect(:, 2) + bigY(k);
% Get x and y,
x = smallRect(:, 1);
y = smallRect(:, 2);
plot(x, y, 'r-', 'MarkerSIze', 20)
hold on
drawnow
pause(0.3);
end
fprintf('Done running %s.m ...\n', mfilename);
Adapt as needed.
More Answers (1)
KSSV
on 12 Jul 2020
S1 = [1 -1 ; 1 1 ; -1 1 ; -1 -1 ; 1 -1]' ;
S2 = 2*S1;
for i = 0:0.2:2*pi
R = [cos(i) -sin(i) ;
sin(i) cos(i) ] ;
S = R*S2 ;
figure(1)
hold off
plot(S(1,:),S(2,:))
hold on
plot(S1(1,:),S1(2,:))
drawnow
pause
end
See Also
Categories
Find more on Language Fundamentals 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!