How to Create Table Fan by C Graphics Programming Language

table fan using C or C++ programming language

Introduction :

The table fan is an essential household appliance we can see in many homes and offices. It relief us on hot summer days. Creating a simple table fan using the graphics.h library in the C or C++ programming language is a fun and creative project in the graphics programming world. Using the graphic functions of graphics.h library, you can draw a visually appealing representation of a table fan with base, rotating blades and the controlling switch on a graphics screen.

In this program, you can build a simple but dynamic animation of a table fan that has rotating blades and on/off switch which controls the fan within a graphics window. In this article, I shall explore you how to create a table fan using the graphics.h library of the Turbo C++ IDE by C or C++ programming language.

What is table fan :

A device that uses electricity to create airflow for cooling or ventilating purposes is called electric fan. It is a common household appliance that used to circulate air in homes, offices and other indoor spaces. There are various types of electric fan designed for specific applications such as ceiling fan, table or desk fan, floor fan and more.

A table or desk fan is a small and portable electric fan that usually placed on a table or desk. It is typically placed on a table, desk or other flat surface so we called it table fan. It typically consists of blades that rotate to produce airflow by an electric motor. It is covered with a cage or grill for safety. It has an adjustable stand to keep it at different positions on a table or desk. Table fan has various switches by which you can control the speed of the fan like low, medium and high.

Table fan is typically small and lightweight so you can easily move the fan from one place to another place. During hot weather, table fan is generally used for personal comfort from the heat. It is commonly kept in a desk or table in homes, offices and small spaces.

About the program :

This is a simple graphics program of table fan by C or C++ graphics programming language.
When you run this program on your pc, you can see a table fan with various components such as base, stand, blades and switch (on/off) on graphical window. There are two states (“off” and “on”) of the table fan.

The program starts in the “off” state (turned off) so you see a stoped table fan on screen. If you click (mouse click) on the “on” switch, the program switches to the “on” state (turned on). Now, you see the rotation of blades of table fan on the graphics screen. You can also stop the table fan by clicking on “off” switch. The animation of table fan will continue until you press any key. After pressing any key, the program will be stopped.

Explanation of the program :

At first, you have to include several header files such as graphics.h, conio.h, dos.h and math.h in the program. Now, you can declare some variables like “xmax”, “ymax”, “x”, “y”, “i”, “j”, “k”, “l”, “m”, “click”, “x_pos”, “y_pos”, “a”, “graphic_driver” and “graphic_mode” in the main() function. You have to also declare structures of type union REGS variables such as “regs_in” and “regs_out” for mouse pointer input.

After that, initialize the graphics system using initgraph() function with proper parameters (graphic driver, mode and path of the BGI file). Using getmaxx() and getmaxy() function, retrieve the dimensions (maximum x and y coordinates) of the graphics window and store in “xmax” and “ymax” variables respectively. You have to also calculate the center of the table fan and store in “x” and “y” variables respectively.

Then, initialize the mouse by set regs_in.x.ax to 0, and display the mouse by set regs_in.x.ax to 1. You can set the background color of the graphics window using setbkcolor() function. Within the while loop, I use kbhit() function to check any key pressed or not. Here, you can capture mouse events (click and position) by set regs_in.x.ax to 3. Now store the position (x and y coordinates) of the mouse pointer in “x_pos” and “y_pos” variables respectively. You have to also store the click state of the mouse pointer in “click” variable.

There are two if statements in the program. Depending on mouse click on two switches (on/off), change the value of “a” variable. Now, in the if else statement, check the value of “a” variable is 0 means “off” state, you see a stopped table fan display on the screen. Otherwise, you can see a started moving table fan display on the screen.

Using various graphic functions, you can draw the various components of a table fan such as the base, stand, fan blades and the center of the fan. You can make the color of the fan and its components visually appealing and more realistic using setcolor() and setfillstyle() and floodfill() functions. The body and base of the table fan draw using fillellipse() function. Using rectangle() function, you can draw the stand and switches of the fan.

To display the text of switches, you can use settextstyle() and outtextxy() functions. Using line() function, draw the blades of the table fan. You have to use trigonometric functions like sin() and cos() to rotate the fan blades smoothly. Using delay() function, you can control the speed of the fan’s blades. You can use cleardevice() function to clear the screen of the graphics window. At last, close the graphics mode using closegraph() function.

How run the program :

To get started, you will need a Turbo C++ compiler on your pc. At first, you have to install the Turbo C++ IDE on your pc. Now, open the Turbo C++ IDE and create a new file. After that, copy the source code and paste in the file. Save the file with a .c or .cpp extension. If you want to know how to copy paste in the Turbo C++ IDE, just click here.

After successfully compile the program, run it. You can also know how to use graphics.h in Turbo C++ IDE from my other post. I have used mouse pointer input in this program. If you want to know how use the mouse pointer by C or C++ programming language, you can see the post.

Source code :

The following code is the source code of the table fan using C or C++ graphics programming language.

/*Developed by Puskar Jasu*/
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <math.h>
int main(void)
{
  int xmax, ymax, x, y, i = 0, j = 0, k = 0, l = 0, m = 0, click, x_pos, y_pos, a = 0;
  int graphic_driver = DETECT, graphic_mode;
  union REGS regs_in, regs_out;
  initgraph(&graphic_driver, &graphic_mode, "//turboc3/bgi");
  xmax = getmaxx();
  ymax = getmaxy();
  x = xmax / 2;
  y = ymax / 2;
  y = y - 120;
  regs_in.x.ax = 0;
  int86(0x33, &regs_in, &regs_out);
  regs_in.x.ax = 1;
  int86(0x33, &regs_in, &regs_out);
  setbkcolor(6);
  while (!kbhit())
  {
    regs_in.x.ax = 3;
    int86(0x33, &regs_in, &regs_out);
    click = regs_out.x.bx;
    x_pos = regs_out.x.cx;
    y_pos = regs_out.x.dx;
    if ((x_pos >= 240 && x_pos <= 300) && (y_pos >= 270 && y_pos <= 310) && click == 1)
    {
      a = 1;
    }
    if ((x_pos >= 340 && x_pos <= 400) && (y_pos >= 270 && y_pos <= 310) && click == 1)
    {
      a = 0;
      m = 0;
      cleardevice();
    }
    if (a == 0)
    {
      setcolor(1);
      setfillstyle(1, 4);
      fillellipse(x, y, 110, 110);
      setfillstyle(1, 5);
      rectangle(x - 10, y + 110, x + 10, y + 230);
      floodfill(x, y + 200, 1);
      setfillstyle(1, 10);
      fillellipse(x, ymax - 100, 70, 30);
      rectangle(x - 80, y + 150, x - 20, y + 190);
      rectangle(x + 20, y + 150, x + 80, y + 190);
      settextstyle(1, 0, 2);
      outtextxy(x - 70, y + 150, "on");
      outtextxy(x + 30, y + 150, "off");
      setcolor(14);
      i = 100 * sin((90 + m) * 3.14159 / 180);
      j = 100 * cos((90 + m) * 3.14159 / 180);
      k = 100 * sin((50 + m) * 3.14159 / 180);
      l = 100 * cos((50 + m) * 3.14159 / 180);
      line(x, y, x + i, y - j);
      line(x, y, x + k, y - l);
      line(x + i, y - j, x + k, y - l);
      i = 100 * sin((330 + m) * 3.14159 / 180);
      j = 100 * cos((330 + m) * 3.14159 / 180);
      k = 100 * sin((290 + m) * 3.14159 / 180);
      l = 100 * cos((290 + m) * 3.14159 / 180);
      line(x, y, x + i, y - j);
      line(x, y, x + k, y - l);
      line(x + i, y - j, x + k, y - l);
      i = 100 * sin((210 + m) * 3.14159 / 180);
      j = 100 * cos((210 + m) * 3.14159 / 180);
      k = 100 * sin((170 + m) * 3.14159 / 180);
      l = 100 * cos((170 + m) * 3.14159 / 180);
      line(x, y, x + i, y - j);
      line(x, y, x + k, y - l);
      line(x + i, y - j, x + k, y - l);
    }
    else
    {
      setcolor(1);
      setfillstyle(1, 4);
      fillellipse(x, y, 110, 110);
      setfillstyle(1, 5);
      rectangle(x - 10, y + 110, x + 10, y + 230);
      floodfill(x, y + 200, 1);
      setfillstyle(1, 10);
      fillellipse(x, ymax - 100, 70, 30);
      rectangle(x - 80, y + 150, x - 20, y + 190);
      rectangle(x + 20, y + 150, x + 80, y + 190);
      settextstyle(1, 0, 2);
      outtextxy(x - 70, y + 150, "on");
      outtextxy(x + 30, y + 150, "off");
      setcolor(14);
      line(x, y, x + i, y - j);
      line(x, y, x + k, y - l);
      line(x + i, y - j, x + k, y - l);
      i = 100 * sin((90 + m) * 3.14159 / 180);
      j = 100 * cos((90 + m) * 3.14159 / 180);
      k = 100 * sin((50 + m) * 3.14159 / 180);
      l = 100 * cos((50 + m) * 3.14159 / 180);
      line(x, y, x + i, y - j);
      line(x, y, x + k, y - l);
      line(x + i, y - j, x + k, y - l);
      i = 100 * sin((330 + m) * 3.14159 / 180);
      j = 100 * cos((330 + m) * 3.14159 / 180);
      k = 100 * sin((290 + m) * 3.14159 / 180);
      l = 100 * cos((290 + m) * 3.14159 / 180);
      line(x, y, x + i, y - j);
      line(x, y, x + k, y - l);
      line(x + i, y - j, x + k, y - l);
      i = 100 * sin((210 + m) * 3.14159 / 180);
      j = 100 * cos((210 + m) * 3.14159 / 180);
      k = 100 * sin((170 + m) * 3.14159 / 180);
      l = 100 * cos((170 + m) * 3.14159 / 180);
      delay(20);
      m += 20;
      if (m >= 360)
        m = 0;
      cleardevice();
    }
  }
  closegraph();
  return 0;
}

Output :

If you run the program on your pc, you can see the output like below image.

output of table fan by C or C++ programming language

Conclusion :

After completing the above article, you have learned how to create a simple table fan using the graphics.h library of Turbo C++ IDE by C or C++ graphics programming language. The table fan of the program is not perfect. You can add some more code in the program to change the appearance of the table fan and make it perfect. Thank you for visiting my site.

Scroll to Top