Monday, February 4, 2013

2D Rotation

Let's make a rotation of a square. First, we create the square:

A = [0, 0];

B = [1, 0];

C = [1, 1];

D = [0, 1];

sq = [A;
      B;
      C;
      D;
      A];

plot2d(sq(:,1), sq(:,2), rect = [-.1 -.1 1.1 1.1]);

The result is the following picture.



For rotating this object an angle a = pi/4 (45°), we can do

a = %pi/4;

r = [cos(a) -sin(a);
     sin(a) cos(a)];

sq_r = sq*r;

plot2d(sq_r(:,1), sq_r(:,2));

 The result is


The same technique can be done for rotation any kind of geometric structure.

For another example, look a random structure and its rotation.

sq = rand(10, 2);

plot2d(sq(:,1), sq(:,2), rect = [-.1 -.1 1.1 1.1]);

a = %pi/4;

r = [cos(a) -sin(a);
     sin(a) cos(a)];

sq_r = sq*r;

plot2d(sq_r(:,1), sq_r(:,2), style = 2);


The result is

Take attention the presented rotation method rotates the object using the center of axes (point (0, 0)) as reference. However, it's very simple to change the point of reference just adjusting matrix r, can you do this?

 

1 comment:

Dennish C. C. Kusuma said...

May I know, whats the meaning of sq(:,1) and sq(:,2) and rect = [-.1 -.1 1.1 1.1]0; ?



Thanks,
Dennish C Cahya K
See my blogspot at http://good-for-share.blogspot.com
Kustom Kulture News at http://www.kustomakers.com