How to translate 2d rectangles and rotate them. (new to matlab)

10 views (last 30 days)
I have a project that requires me to:
1) Draw two rectangles about 3 units wide and .4 units tall. The axis points of the bottom left corner of the first rectangle is (2,3) and (5,3) for the second.
2)Rotate the right axis of both rectangles 30 degrees about the left axes of the left rectangle.
3) Then rotate only the right rectangle about its axis -60 degrees.
Any help would be greatly appreciated, and keep in mind that I have zero Matlab experience prior to this. From my own research and experimentation this is what I have come up with so far but I am still unsure if I am doing it correctly.
axis([0 10 0 10]);
%first rectangle along x axis
rect.Vertices=[2 3;5 03;5 3.4;2 3.4];
rect.faces=[1 2 3 4];
theta = pi/6;
R=[cos(theta) sin(theta);-sin(theta) cos(theta)];
patch(rect,'Vertices',rect.Vertices,'FaceColor',[0 .4 0]);
%second rectangle along x axis
rect.Vertices=[5 3;8 3;8 3.4;5 3.4];
rect.faces=[1 2 3 4];
theta = pi/6;
R=[cos(theta) sin(theta);-sin(theta) cos(theta)];
patch(rect,'Vertices',rect.Vertices,'FaceColor',[0 .4 .4]);
%rotates second rectangle
rect.Vertices=[5 3;8 3;8 3.4;45 3.4];
rect.faces=[1 2 3 4];
theta = pi/6;
R=[cos(theta) sin(theta);-sin(theta) cos(theta)];
patch(rect,'Vertices',rect.Vertices*R,'FaceColor',[0 .4 .4]);
%rotates first rectangle
rect.Vertices=[2 3;5 3;5 3.4;2 3.4];
rect.faces=[1 2 3 4];
theta = pi/6;
R=[cos(theta) sin(theta);-sin(theta) cos(theta)];
patch(rect,'Vertices',rect.Vertices*R,'FaceColor',[0 0 .4]);
%rotates second rectangle -60 degrees
rect.Vertices=[5 3;8 3;8 3.4;6 3.4];
rect.faces=[1 2 3 4];
theta = -pi/3;
R=[cos(theta) sin(theta);-sin(theta) cos(theta)];
patch(rect,'Vertices',rect.Vertices*R,'FaceColor',[0 .4 .4]);

Answers (0)

Categories

Find more on Visual Exploration 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!