Thursday, January 30, 2014

Rotation in C Language


 

 
Point (X,Y) is to be rotated about the origin by angle theta to location (X',Y')
X' = X * cos(theta) - Y * sin(theta)
Y' = X * sin(theta) + Y *cos(theta)
or P' = R * P where  P' = [  X`,  Y' ],     P  = [X,Y]               
 
R  = |  cos(theta)   -sin(theta) |
     |  sin(theta)   cos(theta)  |
      
Rotation is performed about the origin (0,0) not about the center of the line/polygon/whatever



#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
    int gdriver=DETECT,gmode;

    double midx=350,midy=300;
    double x1,y1,x2,y2,x22,y22;
    int x,y,i;
    double theta,t;
    clrscr();
    initgraph(&gdriver,&gmode,"c:/tc/bgi");
    printf("\n Enter the 1st co-ordinate=");
    scanf("%lf%lf",&x1,&y1);
    printf("\n Enter the 2nd co-ordinate=");
    scanf("%lf%lf",&x2,&y2);

    clrscr();
    line(midx,midy-150,midx,midy+150);
    line(midx+150,midy,midx-150,midy);
    setcolor(2);
    outtextxy(midx,midy," origin");
    line(midx+x1,midy+y1,midx+x2,midy+y2);
    //getch();
    printf("\n Enter the angle of rotation=");
    scanf("%lf",&theta);
    t=theta*0.01744;
    x22=x2*cos(t)-y2*sin(t);
    y22=x2*sin(t)+y2*cos(t);
    line(midx+x1,midy+y1,midx+x22,midy+y22);
    getch();


}

No comments:

Post a Comment