how to plote a square that rotate around a square

1 次查看(过去 30 天)
hi !
i want to creat a square the rotate around a square
pleace help me in it

采纳的回答

Image Analyst
Image Analyst 2020-7-12
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.

更多回答(1 个)

KSSV
KSSV 2020-7-12
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
  1 个评论
Muhammad Ikram
Muhammad Ikram 2020-7-12
hi sir
this code is not working it only show a square
i want a program that creat a square on the boundry of an other square tha move along the boundry of that square

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by