plot a 2D triangle with three equilateral sides which starts being centered at location (0,0)

2 visualizzazioni (ultimi 30 giorni)
The objective of this project is to plot a 2D triangle with three equal sides of length 2, which starts by being centered at location (0,0), and continuously moves within a square area which is limited between -10 to 10 horizontally and -10 to 10 vertically. The center of the triangle should never exit this 20 x 20 square area. In other words, when the center of the triangle reaches those limits, the triangle should bounce back so that it stays within the 20 x 20 box. At the same time, the triangle should be rotating about its center. You may choose the speed with which the triangle moves, and the speed with which the triangle rotates around its center yourselves. However, the triangle should not be moving just horizontally or just vertically. Also, it should not be moving with the same speed horizontally and vertically.
Below is my code
% PROJECT 1 clc clear all close all
%c=(0,0) xo=[-1 1 0 -1] yo=[-1 -1 1 -1]
% The following two lines rotate the point (xo,yo) % around (0,0) by angle pi/10.
for i=0:0.1:5
x1=xo*cos(pi/10)+yo*sin(pi/10);
y1=-xo*sin(pi/10)+yo*cos(pi/10);
xo=x1;
yo=y1;
xo=xo+i;
yo=yo+i;
% Set the limits of the axes between xmin and xmax % horizontally between ymin and ymax vertically. % Plot
figure(1),plot(x1,y1); set(gca,'xlim',[-10 10],'ylim',[-10 10]);
% Pause function is used to slow down the animation. pause(0.25) end
I've gotten it to move, but not quite the project asks for.
Please help thanks!

Risposta accettata

Birdman
Birdman il 20 Ott 2017
Try the m file. Hope this helps.
  4 Commenti
Tony Nguyen
Tony Nguyen il 20 Ott 2017
thank you
Here is what i had came up with while I was working on this before you sent me what you had came up with
% PROJECT 1 clc clear all close all
xo=[-1 1 0 -1] yo=[-1 -1 1 -1]
x_centroid=mean(xo(1:3)) y_centroid=mean(yo(1:3))
xo=xo-x_centroid yo=yo-y_centroid
step=0.1 i=0 j=0 for j=1:100 i=i+step % The following two lines rotate the point (xo,yo) % around (0,0) by angle pi/10.
x1=xo*cos(pi/10)+yo*sin(pi/10);
y1=-xo*sin(pi/10)+yo*cos(pi/10);
axis equal;
xo=x1;
yo=y1;
x2=xo+i;
y2=yo+i;
j=j+step
x2_centroid=mean(x2(1:3))
y2_centroid=mean(x2(1:3))
x2=xo+j
y2=yo+j
%need two if statements
% Set the limits of the axes between xmin and xmax % horizontally between ymin and ymax vertically. % Plot
figure(1),plot(x2,y2); set(gca,'xlim',[-10 10],'ylim',[-10 10]);
% Pause function is used to slow down the animation. pause(0.1) end
my professor said i should rotate the triangle at its center first, and then add the shift to it which makes it rotate around the limits
Birdman
Birdman il 20 Ott 2017
Modificato: Birdman il 20 Ott 2017
That is also a perspective, but I decided to go on with the fact that x1 and y1 are crossing the borders and we should changing the way of rotation for the triangle, which is much easier for me.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by