C Graphics Program in C programming


Top Graphics C Program

A Sample Program for Graphics in C Programming

 #include<stdio.h>

#include <conio.h>

#include<graphics.h>

void main()

{

 int gd = DETECT, gm;

initgraph (&gd, &gm, "C:\\TURBOC3\\BGI");// Here follow the path of c software in local hard drive

//Your Code goes Here - CodeHub

getch();

closegraph();

} 

Write a C Program to Draw a Line using graphics.

 

#include <stdio.h>

#include <graphics.h>

#include <conio.h>

void main ()

{

  int gd = DETECT, gm;

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

  line (90, 90, 180, 180);

 getch ();

 closegraph ();

} 

Write a C Program to Draw a Triangle Using graphics.

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

 

void main() {

   int gd = DETECT, gm;

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

 

   line(280, 90, 180, 180);

   line(280, 90, 180, 180);     

   line(280, 90, 180, 180);

 

   getch();

   closegraph();

} 

Write a C Program to Draw a Line, Circle, Rectangle, Ellipse, Bar, Arc Using graphics.

 

#include <stdio.h>

#include <graphics.h>

#include <conio.h>

void main()

{

 int gd=DETECT, gm;

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

setcolor(4);

 

line(220, 240, 370, 240);

circle(280, 240, 60);

rectangle(220, 140, 370, 340;

ellipse(280, 240, 0, 350, 70, 40);

bar(220, 140, 90, 180);

arc(280, 240, 0, 100, 60);

 

getch();

closegraph();

} 

Write a C Program to Draw a Line, bar Rectangle, Circle and Ellipse display text on the screen Using graphics.

 

#include <stdio.h>

#include <graphics.h>

#include <conio.h>

void main()

{

int gd = DETECT, gm, left=110, top=110, right=220, bottom=220, x= 320, y=160, radius=60;

 

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

 

line(left - 15, top + 160, left + 420, top + 160);

bar(left + 320, top, right + 320, bottom);

rectangle(left, top, right, bottom);

circle(x, y, radius);

ellipse(x, y + 220, 0, 350, 110, 60);

 

outtextxy(left + 110, top + 320, " C Graphics Program Using various function");

 

getch();

closegraph();

return 0;

} 

Write a C Program to Move a Car using graphic function.

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

Void main()
{
   int m, n = 0, gd = DETECT, gm;

 

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

      settextstyle(DEFAULT_FONT, HORIZ_DIR,2);

   outtextxy(25,240,"Press any key to move the car");

   getch();
   setviewport(0,0,639,440,1);

   for (i = 0; i <= 420; i = i + 10, j++)
   {
   
 rectangle(45+i, 270, 140+i, 380);
      rectangle(140+i, 340, 190+i, 390);
      circle(70+i, 400, 12);
      circle(170+i, 400, 12);
      setcolor(j);
      delay(80);

      if (i == 410)
         break;

      clearviewport();
   }

   getch();
   closegraph();
   return 0;
}
 

Write a C Program to Draw a Bar Chart Using graphics.

 

#include <stdio.h>

#include <graphics.h>
#include <conio.h>

void main()
{
   int gd = DETECT, gm;

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

 

   setcolor(YELLOW);


   rectangle(0,25,635,440);


   settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);


   setcolor(green);


   outtextxy(270,0,"Bar Chart");

   setlinestyle(SOLID_LINE,0,2);

 

   line(110,400,110,50);

   line(110,410,580,410);
   line(100,80,110,50);
   line(110,70,100,60);

   line(590,430,600,420);

   outtextxy(90,40,"Y");

   outtextxy(600,400,"X");
   outtextxy(90,405,"O");

 

   setfillstyle(LINE_FILL, yellow);
   bar(160,180,180,410);

 

   setfillstyle(XHATCH_FILL, white);
   bar(225,140,270,410);

 

   setfillstyle(WIDE_DOT_FILL, black);
   bar(300,180,350,419);

 

   setfillstyle(INTERLEAVE_FILL,MAGENTA);
   bar(370,120,420,420);

 

   setfillstyle(HATCH_FILL,BROWN);
   bar(450,170,510,425);

   getch();
   return 0;
}
 

Write a C program to Draw Circles in Circles Using Graphics.

 

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

void main()
{
  int gd = DETECT, gm, x, y, color, angle = 0;
  struct arccoordstype a, b;

  initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
  delay(1500);

  while (angle <= 360)
   {
      setcolor(BLACK);
    arc(getmaxx()/2, getmaxy()/2, angle, angle + 2, 110);
      setcolor(RED);

getarccoords(&a);
    circle(a.xstart, a.ystart, 30);   

  setcolor(BLACK);
      arc(getmaxx()/2, getmaxy()/2, angle, angle + 2, 140);
      getarccoords(&a);
      setcolor(GREEN);
    circle(a.xstart, a.ystart, 30);      angle = angle+5;
      delay(50);
   }

   getch();


   closegraph();
   return 0;
}
 

Write a C program to print DDA Line Drawing Using Algorithm in graphics.

 

#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

 

#define Oval(val) (int)(val+0.5)

 

void main()

{

   int gd = DETECT, gm;

   void line_dda(int, int, int, int);

   int x1, x2, y1, y2;

   printf("Enter the two values");

   scanf("%d%d%d%d", &x1, &y1, &x2, &y2);

   initgraph(&gd, &gm, "");

   cleardevice();

   line_dda(x1, y1, x2, y2);

   getch();

   closegraph();

}

 

void line_dda(int x1, int y1, int x2, int y2) {

   int Dx = x2 – x1, Dy = y2 – y1, steps, k;

   float x3, y3, X = x1, Y = y1;

   if (abs(Dx) > abs(Dy))

      steps = abs(Dx);

   else

    steps = abs(Dy);

 

   x3 = Dx / (float) steps;

   y3 = Dy / (float) steps;

   putpixel(oval(X), ovao(Y), 6);

 

   for (k = 0; k < steps; k++)

{

 

      X = X + x3;

      Y = Y + y3;

      putpixel(oval(X), oval(Y), 6);

 

   }

} 

Write a C program Using Bresenham's Line Drawing Algorithm in graphics.

 

#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

 

void main()

 {

   int a1, a2, b1, b2;

   int gd = DETECT, gm;

   void bresenham(int, int, int, int);

 

   printf("Enter the two end points:");

   scanf("%d%d%d%d", &a1, &a2, &b1, &b2);

 

   initgraph(&gd, &gm, "");

   cleardevice();

  

   bresenham( a1, a2, b1, b2);

   getch();

   line( a1, a2, b1, b2);

   getch();

   closegraph();

}

 

void bresenham(int a1, int b1, int a2, int b2)

 {

   int da = abs(a1 - a2), db = abs(b1 - b2);

   int p, a, b, i, aend, bend;

   if (da != 0)

 {

      p = 2 * db - da;

      if (a1 > a2)

 {

         a = a2;

         b = b2;

         aend = a1;

      }

      else {

         a = a1;

         b = b1;

         aend = a2;

      }

      putpixel(a, b, 2);

      for (i = a; i < aend; i++)

{

         a += 1;

         if (p < 0)

            p += 2 * db;

         else

            p += 2 * (db - da);

      }

      putpixel(a, b, 2);

   }

   else

 {

      p = 2 * da - db;

      if (b1 > b2)

{

         a = a2;

         b = b2;

         bend = b2;

      }

      putpixel(a, b, 2);

      for (i = b; i < bend; i++)

 {

         b += 1;

         if (p < 0)

            p += 2 * da;

         else {

            a += 1;

            p += 2 * (da - db);

         }

         putpixel(a, b, 2);

      }

   }

} 

Write a C program Using Bresenham's Circle Drawing Algorithm in graphics.

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

 

void main()

{

   int gd = DETECT, gm;

   int x1, y1, r;

   void circle(int, int, int);

   printf("Enter the mid points value and radious value :");

   scanf("%d%d%d", &x, &y, &r);

   initgraph(&gd, &gm, "");

   circle(x1, y1, r);

   getch();

   closegraph();

}

 

void circle(int x2, int y2, int r)

 {

   int x1 = 0, y1 = r, p = 1 - r;

   void clipplot(int, int, int, int);

   clipplot(x2, y2, x1, y1);

   while (x 1< y1)

{

      X1++;

      if (p < 0)

         p += 2 * x1 + 1;

      else {

         y1--;

         p += 2 * (x1 - y1) + 1;

      }

      clipplot(x2, y2, x1, y1);

   }

}

 

void clipplot(int x1ctr, int y1ctr, int x1, int y1)

{

   putpixel(x1ctr + x1, y1ctr + y1, 1);

   putpixel(x1ctr – x1, y1ctr + y1, 1);

   putpixel(x1ctr + x1, y1ctr – y1, 1);

   putpixel(x1ctr – x1, y1ctr – y1, 1);

   putpixel(x1ctr + y1, y1ctr + x1, 1);

   putpixel(x1ctr – y1, y1ctr + x1, 1);

   putpixel(x1ctr + y1, y1ctr – x1, 1);

   putpixel(x1ctr – y1, y1ctr – x1, 1);

   getch();

} 


Comments

Popular posts from this blog

C Program to print swap number using third variables

C Program to open and write to a text file using fprintf.