# How to rotate a whole rectangle by an arbitrary angle around the origin using a transformation matrix?

Suppose, I have a rectangle ABCD. Where, A(0,0), B(7,0), C(7,5) and D(0,5).

I want to rotate the whole rectangle by theta = 50°.

I know that, a rotation transformation matrix can be used to do that.

So, I have done the following:

A' = [0 0 1] ; B' = [7 0 1] ;

C' = [7 5 1] ; D' = [0 5 1] ;

But the output has become skewed:

What should be the correct calculation?

• Your maths looks right. Jul 27, 2015 at 2:53
• @immibis, no my math is wrong! I discovered that. see my answer. I am feeling like heavens!
– user15743
Jul 27, 2015 at 21:34

I have solved the problem.

//Roation around the Origin
//Individual matrices

#include "graphics.h"
#include "Vector2d.h"
#include "Coordinates2d.h"
#include "Polygon2d.h"
#include <math.h>
#include <iostream>

#define PI 3.141
#define DEG 45.00f
#define RAD 6.283185308 / (360.0 / DEG)

int main()
{
////////////////////////////////////////
Coordinates2d::ShowWindow("Roation around the origin(individual matrices)");
////////////////////////////////////////

Matrix a(1,3);
a.SetItem(0,0,0);   a.SetItem(0,1,0);   a.SetItem(0,2,0);
a.Show();

Matrix b(1,3);
b.SetItem(0,0,140); b.SetItem(0,1,0);   b.SetItem(0,2,0);
b.Show();

Matrix c(1,3);
c.SetItem(0,0,140); c.SetItem(0,1,100); c.SetItem(0,2,0);
c.Show();

Matrix d(1,3);
d.SetItem(0,0,0);   d.SetItem(0,1,100); d.SetItem(0,2,0);
d.Show();

Matrix rot(3,3);
rot.SetItem(2,0,0);         rot.SetItem(2,1,0);         rot.SetItem(2,2,1);
rot.Show();

Matrix ma;
ma = a.Multiply(rot);
ma.Show();

Matrix mb;
mb = b.Multiply(rot);
mb.Show();

Matrix mc;
mc = c.Multiply(rot);
mc.Show();

Matrix md;
md = d.Multiply(rot);
md.Show();

Polygon2d poly;
Coordinates2d::Draw(poly, Yellow);

Polygon2d poly2;

Coordinates2d::Draw(poly2, LightGreen);
////////////////////////////////////////
Coordinates2d::Wait();
////////////////////////////////////////
}


• What did you change? Jul 28, 2015 at 1:43
• @immibis, [x y 1] is changed to [x y 0].
– user15743
Jul 28, 2015 at 1:44

Exerpt from the 3D CSG module I wrote for my engine. You should be able to derive from this easily enough.

SOLID_C *Rotate(const V3 &axis,const T angle)
{
V3 rt[3],m[3],mt[3],ax=((V3)axis).normalise(),t=ax*(1.-c);
rt[0][0]=c+t[0]*ax[0]; rt[0][1]=0+t[0]*ax[1]+s*ax[2]; rt[0][2]=0+t[0]*ax[2]-s*ax[1];
rt[1][0]=0+t[1]*ax[0]-s*ax[2]; rt[1][1]=c+t[1]*ax[1]; rt[1][2]=0+t[1]*ax[2]+s*ax[0];
rt[2][0]=0+t[2]*ax[0]+s*ax[1]; rt[2][1]=0+t[2]*ax[1]-s*ax[0]; rt[2][2]=c+t[2]*ax[2];
m[0].set(1,0,0); m[1].set(0,1,0); m[2].set(0,0,1);
mt[0]=m[0]*rt[0][0]+m[1]*rt[0][1]+m[2]*rt[0][2]; mt[1]=m[0]*rt[1][0]+m[1]*rt[1][1]+m[2]*rt[1][2]; mt[2]=m[0]*rt[2][0]+m[1]*rt[2][1]+m[2]*rt[2][2];
M3 mat(mt[0],mt[1],mt[2]);
return Apply(mat);
}

SOLID_C *Apply(const M3 &mat)
{
FACE *f; V3 t; UINT i=(UINT)vl.size(); while (i--) t=vl[i],vl[i]=mat*t;
i=(UINT)fl.size(); while (i--) f=fl[i],t=f->nrm,f->nrm=mat*t,f->dist=f->nrm.dot(vl[f->vl[0]]);
return this;
}

• I have just solved it by myself. I have also posted the code.
– user15743
Jul 27, 2015 at 21:16
• Yeah, I noticed that after I posted mine. Should I delete mine then? (New to SE, sorry for noobness.) Jul 27, 2015 at 21:20
• No need to delete. No problem. I can up vote you.
– user15743
Jul 27, 2015 at 21:21
• Ohh, thanks. I guess it's a case of 'same problem, multiple solutions' for others then. Always nice. Jul 27, 2015 at 21:24

Most of trigonometric functions take radians as input, this may be your issue.

float theta = angleInDegrees*PI/180