Friday 7 August 2015

Program for controlling rotation using buttons

NOTE: Please change the path of initgraph() function as per your system's BGI directory.
and click here to get mouse.C file and paste it to your bin folder before running the program below. 

Output:
Computer Graphics, C program to implement buttons


Code begins here: 


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include"mouse.c"
#include<dos.h>
#include<math.h>
float x1=320,y1=140;
float x2,y2;

//to dsiplay the buttons and clock with one hand
void display()
{
showmouseptr();
setcolor(LIGHTRED);
outtextxy(160,70,"Click the buttons to perform respective actions...");

setcolor(GREEN);
rectangle(100,5,200,50);
outtextxy(120,22,"clockwise");

setcolor(LIGHTBLUE);
rectangle(250,5,370,50);
outtextxy(260,22,"anticlockwise");

setcolor(RED);
rectangle(400,5,500,50);
outtextxy(430,22,"reset");

setcolor(CYAN);
circle(320,240,100);
line(320,240,x1,y1);
}

//function for degree to radian conversion
float d2r(float degree)
{
float rad=(M_PI*degree)/180;
return rad;
}

//fucntion of clockwise rotation
void crotate(float degree)
{
float rad=d2r(degree);
x2=(x1-320)*cos(rad)-(y1-240)*sin(rad)+320;
y2=(x1-320)*sin(rad)+(y1-240)*cos(rad)+240;
x1=x2;
y1=y2;
}


//fucntion of anti-clockwise rotation

void arotate(float degree)
{
float rad=d2r(degree);
x2=(x1-320)*cos(rad)+(y1-240)*sin(rad)+320;
y2=(y1-240)*cos(rad)-(x1-320)*sin(rad)+240;

x1=x2;
y1=y2;
}

//fucntion to reset clock 
void reset()
{
x1=320;
y1=142;
cleardevice();
}


void main()
{
int x,y,button;
int gd=DETECT,gm;
int i;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI");

 display();

while(!kbhit())
{
getmousepos(&x,&y,&button);

//if left clicked
if(button==1){

if(x>100 && x<200 && y>5 && y<50)
{
crotate(15);
delay(100);
cleardevice();
display();
}

if(x>250 && x<370 && y>5 && y<50)
{
arotate(15);
delay(100);
cleardevice();
display();
}

if(x>400 && x<500 && y>5 && y<50)
{
reset();
display();
}

}
}
closegraph();
}

No comments:

Post a Comment