1
\$\begingroup\$

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°.

enter image description here

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

enter image description here

So, I have done the following:

A' = [0 0 1] enter image description here; B' = [7 0 1] enter image description here;

C' = [7 5 1] enter image description here; D' = [0 5 1] enter image description here;

But the output has become skewed:

enter image description here

What should be the correct calculation?

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

3 Answers 3

1
\$\begingroup\$

I have solved the problem.

enter image description here

//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(0,0,cos(RAD));  rot.SetItem(0,1,sin(RAD));  rot.SetItem(0,2,0);
    rot.SetItem(1,0,-sin(RAD)); rot.SetItem(1,1,cos(RAD));  rot.SetItem(1,2,0); 
    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;
    poly.Add(0, 0);
    poly.Add(140, 0);
    poly.Add(140, 100);
    poly.Add(0, 100);
    Coordinates2d::Draw(poly, Yellow);

    Polygon2d poly2;
    poly2.Add(ma.GetItem(0,0), ma.GetItem(0,1));
    poly2.Add(mb.GetItem(0,0), mb.GetItem(0,1));
    poly2.Add(mc.GetItem(0,0), mc.GetItem(0,1));
    poly2.Add(md.GetItem(0,0), md.GetItem(0,1));

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

enter image description here

enter image description here

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

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)
{
    T a=radians(angle),c=cos(a),s=sin(a);
    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;
}
\$\endgroup\$
4
  • \$\begingroup\$ I have just solved it by myself. I have also posted the code. \$\endgroup\$
    – user15743
    Jul 27, 2015 at 21:16
  • \$\begingroup\$ Yeah, I noticed that after I posted mine. Should I delete mine then? (New to SE, sorry for noobness.) \$\endgroup\$ Jul 27, 2015 at 21:20
  • \$\begingroup\$ No need to delete. No problem. I can up vote you. \$\endgroup\$
    – user15743
    Jul 27, 2015 at 21:21
  • \$\begingroup\$ Ohh, thanks. I guess it's a case of 'same problem, multiple solutions' for others then. Always nice. \$\endgroup\$ Jul 27, 2015 at 21:24
-2
\$\begingroup\$

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

So your theta should be

float theta = angleInDegrees*PI/180
\$\endgroup\$
2
  • \$\begingroup\$ No. That was not the issue. I used that. \$\endgroup\$
    – user15743
    Jul 27, 2015 at 11:42
  • \$\begingroup\$ If that was the problem it would still be a rotation, just not by the right angle. It wouldn't be skewed. \$\endgroup\$ Jul 28, 2015 at 1:43

You must log in to answer this question.