Tuesday, June 6, 2017

C Program to create a clock

Graphics in C – Program to create a clock


In C computer language, you can create any program. Only your imagination is the limit. Here is a program in C graphics, which creates a clock. Please type this program in your editor.

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int i=0, j=0, k=0, l=0;
int s, m, h, c, r;
void dial(int x, int y);
void minutehand(int a, int b, int c);
void secondhand(int a, int b, int c);
void hourhand(int a, int b, int c);
void main()
{
      int gd=DETECT, gm;
      int maxx, maxy;
      char str[50];
      int ch1, ch2, ch3, ch4, ch5, ch6;
      int hour, min, sec;
      /* time_t is a variable type */
      time_t t;
      /* time() gets time of day */
      time(&t);
      /* ctime() converts date and
      time to a string */
      strcpy(str, ctime(&t));
      initgraph(&gd, &gm, "c:\\tc\\bgi");
      /* for converting a charactor to
         an integer */
      ch1=str[11]-'0';
      ch2=str[12]-'0';
      ch3=str[14]-'0';
      ch4=str[15]-'0';
      ch5=str[17]-'0';
      ch6=str[18]-'0';
      hour=ch1*10+ch2;
      min=ch3*10+ch4;
      sec=ch5*10+ch6;

      if(hour>=12)
      {
            hour=hour-12;
      }
      s=sec;
      m=min;
      h=hour;
      c=m/12;
      r=m%12;
      maxx=getmaxx();
      maxy=getmaxy();
      while(!kbhit())
      {
            cleardevice();
            dial(maxx, maxy);
            hourhand(maxx, maxy, hour);
            minutehand(maxx, maxy, min);
            secondhand(maxx, maxy, sec);
            delay(1000);
            if(s+i==60)
            {
                  j++;
                  s=0;
            }

            if(i>60)
            {
                    i=i%60;
            }

            l=j;

            if(r+l==12)
            {
                    r=0;
                    k++;
             }

            if(l>12)
            {
                   l=l%12;
            }

            if(k==5)
            {
                    k=0;
            }
      }
      closegraph();
      restorecrtmode();
}

void dial(int x, int y)
{
      setcolor(WHITE);
      setfillstyle(SOLID_FILL, WHITE);
      circle(x/2, y/2, 120);
      floodfill(x/2, y/2, WHITE);
      circle(x/2, y/2, 140);
      setfillstyle(SOLID_FILL, DARKGRAY);
      floodfill(x/2, y/2-130, WHITE);
      setcolor(RED);
      settextstyle(0, 0, 2);
      outtextxy(x/2-13, y/2-110, "12");
      outtextxy(x/2-6, y/2+96, "6");
      outtextxy(x/2-108, y/2-7, "9");
      outtextxy(x/2+96, y/2-7, "3");
      circle(x/2, y/2, 10);
      setfillstyle(SOLID_FILL, RED);
      floodfill(x/2, y/2, RED);
      setcolor(BLACK);
      circle(x/2, y/2-115, 4);
      setfillstyle(SOLID_FILL, BLACK);
      floodfill(x/2, y/2-115, BLACK);
      circle(x/2, y/2+115, 4);
      floodfill(x/2, y/2+115, BLACK);
      circle(x/2-114, y/2, 4);
      floodfill(x/2-114, y/2, BLACK);
      circle(x/2+114, y/2, 4);
      floodfill(x/2+114, y/2, BLACK);
      circle(x/3+45, y/3-15, 4);
      floodfill(x/3+45, y/3-15, BLACK);
      circle(x/3+5, y/3+30, 4);
      floodfill(x/3+5, y/3+30, BLACK);
      circle(x/3+45, 2*y/3+15, 4);
      floodfill(x/3+45, 2*y/3+15, BLACK);
      circle(x/3+5, 2*y/3-30, 4);
      floodfill(x/3+5, 2*y/3-30, BLACK);
      circle(2*x/3-45, y/3-15, 4);
      floodfill(2*x/3-45, y/3-15, BLACK);
      circle(2*x/3-5, y/3+30, 4);
      floodfill(2*x/3-5, y/3+30, BLACK);
      circle(2*x/3-5, 2*y/3-30, 4);
      floodfill(2*x/3-5, 2*y/3-30, BLACK);
      circle(2*x/3-45, 2*y/3+15, 4);
      floodfill(2*x/3-45, 2*y/3+15, BLACK);
}

void secondhand(int a, int b, int sec)
{
      int x1=a/2, y1=b/2;
      int x2, y2;
      /* 2*3.14/60=0.1047 */
      x2=x1+(int)(90 * cos((i-15+sec)*0.1047));
      y2=y1+(int)(90 * sin((i-15+sec)*0.1047));
      setcolor(BLACK);
      setlinestyle(0, 0, 1);
      line(x1, y1, x2, y2);
      i++;
}

void minutehand(int a, int b, int min)
{
      int x1, x2, y1, y2;
      x1=a/2;
      y1=b/2;
      setcolor(BLACK);
      setlinestyle(0, 0, 3);
      x2=x1+(int)(90*cos((min+j-15)*0.1047));
      y2=y1+(int)(90*sin((min+j-15)*0.1047));
      line(x1, y1, x2, y2);
}

void hourhand(int a, int b, int hour)
{
      int x1, y1, x2, y2;
      x1=a/2;
      y1=b/2;
      setcolor(BLACK);
      setlinestyle(0, 0, 3);
      x2=x1+(int)(70*cos(((hour*5)+k+c-15)*0.1047));
      y2=y1+(int)(70*sin(((hour*5)+k+c-15)*0.1047));
      line(x1, y1, x2, y2);
}


Sunday, May 28, 2017

C Program for getting the position of mouse pointer

Graphics in C – Getting Position of Moving Mouse


In C computer language you can get the position of mouse pointer on the screen. There are plenty of services available under interrupt 33h. In the previous post, we have seen mouse pointer moving within a boundary. Now, we’ll see the mouse pointer along with its screen coordinates. Please type the program given below in your editor.

#include<graphics.h>
#include<dos.h>

union REGS in, out;
int mousestart();
void showpointer();
void boundpointer(int x1, int y1, int x2, int y2);
void mouseposition(int *x, int *y);

void main()
{
      int gd = DETECT, gm, maxx, maxy, x, y;
      char str[20];
      initgraph(&gd, &gm, "c:\\tc\\bgi");
      maxx = getmaxx();
      maxy = getmaxy();
      if(mousestart()==0)
      {
            closegraph();
            restorecrtmode();
            printf("\nMouse driver not loaded");
            exit(1);
      }
      while(!kbhit())
      {
            cleardevice();
            rectangle(0, 20, maxx, maxy-20);
            boundpointer(1, 21, maxx-1, maxy-21);
            showpointer();
            mouseposition(&x, &y);
            sprintf(str, "(%d, %d)", x, y);
            outtextxy(x, y, str);
            delay(10);
      }
      closegraph();
      restorecrtmode();
}

int mousestart()
{
      in.x.ax = 0;
      int86(0x33, &in, &out);
      return(out.x.ax);
}

void showpointer()
{
      in.x.ax = 1;
      int86(0x33, &in, &out);
}

void boundpointer(int x1, int y1, int x2, int y2)
{
      in.x.ax = 7;
      in.x.cx = x1;
      in.x.dx = x2;
      int86(0x33, &in, &out);

      in.x.ax = 8;
      in.x.cx = y1;
      in.x.dx = y2;
      int86(0x33, &in, &out);
}

void mouseposition(int *x, int *y)
{
      in.x.ax = 3;
      int86(0x33, &in, &out);
      *x = out.x.cx;
      *y = out.x.dx;
}

Compile and execute this program. In the output, you’ll see that as you move the mouse pointer, the screen coordinates of mouse pointer show up along with the mouse pointer.

In this program, I have declared two variables ‘in’ and ‘out’ of type union REGS. What is union REGS, I have already discussed this in my previous post. To recall the topic, please see here.

Then, I have declared four functions.

int mousestart();
void showpointer();
void boundpointer(int x1, int y1, int x2, int y2);
void mouseposition(int *x, int *y);

In the function main(), I have got maximum screen coordinates by these statements given below-

maxx = getmaxx();
maxy = getmaxy();

Then, I checked whether mouse driver is loaded or not.

if(mousestart()==0)
  {
    closegraph();
    restorecrtmode();
    printf("\nMouse driver not loaded");
    exit(1);
  }

The definition of function mousestart() is given after the function main().  For the explanation of this function, please refer my previous post here.

Now, I have taken a ‘while’ loop, which executes repeatedly until a key is pressed. In this loop, a rectangle has been drawn to define the boundaries of mouse pointer. Then, a function boundpointer() is called, which is defined after the function main(). Function boundpointer() restricts the movement of mouse pointer within the rectangular boundaries.

rectangle(0, 20, maxx, maxy-20);
boundpointer(1, 21, maxx-1, maxy-21);

For the explanation of function boundpointer(), please refer my previous post here.

Now, function showpointer() has been called to show the mouse pointer on the screen. Definition of this function is given after the function main(). For the explanation of this function, please refer my previous post here.

To get the position of mouse pointer on the screen, I have called the function mouseposition().

mouseposition(&x, &y);

In this function, I have passed the addresses of variables ‘x’ and ‘y’. This is so, because, in the definition of this function, the parameters are the pointer variables. This is the way of a function to return more than one value to the calling function. Now, see the function definition given below-

void mouseposition(int *x, int *y)
{
  in.x.ax = 3;
  int86(0x33, &in, &out);
  *x = out.x.cx;
  *y = out.x.dx;
}

To understand this function, please recall the service 3 available under interrupt 33h, which is given below-

Interrupt : 33h
Service : 3
Gets mouse position and button status
Call with AX = 3
Returns BX = Mouse button status (1=Corresponding button  pressed)
Bit 0 à left button is down
Bit 1 à right button is down
Bit 2 à center button is down (if present)
Bits 3-15 à cleared to 0
CX = x coordinate
DX = y coordinate

Therefore, you can see, I have given the value ‘3’ to ‘ax’ variable, called the function int86(), and passed the addresses of variables ‘in’ and ‘out’ in this function. This function returns some values in ‘out’ variable. We know that after executing the service ‘3’ through function int86(), variable ‘cx’ has the value of mouse’s x-coordinate, and variable ‘dx’ will get the value of mouse’s y-coordinate on the screen. These values are assigned to pointer variables ‘x’ and ‘y’. Here, I assume you understand the basics of pointers. To understand this function better, please refer my two recent posts.

After getting mouse pointer’s position on screen, we have formatted these values to a string ‘str’ using function sprintf(). Next, this string is being displayed continuously on this mouse position using function outtextxy() as you move mouse pointer. Function delay() has been used to give a smooth display.

mouseposition(&x, &y);
sprintf(str, "(%d, %d)", x, y);
outtextxy(x, y, str);
delay(10);

Now, the explanation of this simple program is complete. For explanation of the rest of the functions and terms, please refer my previous posts. You can click on labels given in the right side-bar for the explanation of any function.

OUTPUT: