C: Graphics in Dev-C++ - Applications resolved


1) Draw a filled rectangle.
#include <graphics.h>
int main( )
{
    initwindow(600, 600, "Rectangle");
    bar(500, 500, 100, 100); 
    while (!kbhit( ))
    {
        delay(200);
    }
    return 0;
}
2) Draw a rectangle unfilled.
#include <graphics.h>
int main( )
{
    initwindow(600, 600, "Rectangle");
    rectangle(50,275,150,400);
    setcolor (15);
    while (!kbhit( ))
    {
        delay(200);
    }
    return 0;
}
 
3) Draw a circle.
#include <graphics.h>
int main( )
{
    initwindow(400, 300, "Circle");
    circle(100, 50, 40);
    while (!kbhit( ))
    {
        delay(200);
    }
    return 0;
}
 
4) Draw concentric circles.
#include <graphics.h>
 
int main()
{ 
   int gd = DETECT, gm;
   int x = 320, y = 240, radius;
 
   initgraph(&gd, &gm, "C:\\TC\\BGI");
 
   for ( radius = 25; radius <= 125 ; radius = radius + 20)
      circle(x, y, radius);
 
   getch();
   closegraph();
   return 0;
}
 
5) Draw a rectangle with upper left corner, upper left corner of the screen.
#include <graphics.h>
int main( )
{
    initwindow(600, 600, "Rectangle");
    rectangle(0,0,20,50);
 
    while (!kbhit( ))
    {
        delay(200);
    }
    return 0;
}
6) Draw a rectangle with upper left corner, upper left corner of the screen. Length and width to introduce from the keyboard.
#include <graphics.h> 	// primitive graphics library
#include <stdlib.h>
int main()
{ 
    
   int graphdriver, graphmode;
   int left,top,right,bottom;
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   left=top=0;   
   printf("Enter right: ", right);
   scanf("%d", &right);
   printf("Enter bottom: ", bottom);
   scanf("%d", &bottom);
   rectangle(left,top,right,bottom);
   getch();
   closegraph();
   return 0;
}
7) Draw a rectangle with lower-left corner in the upper left corner of the screen. Length and width to introduce from the keyboard.
>#include <graphics.h> 	// primitive graphics library
#include <stdlib.h>
#include <dos.h>
#include <conio.h> 
int main()
{ 
    int graphdriver, graphmode;
    int x,y;
    
   graphdriver = DETECT;
 
   initgraph(&graphdriver, &graphmode, " ");
   
   printf("Enter length of rectangle: ", x);
   scanf("%d", &x);
   
   printf("Enter the width of the rectangle: ", y);
   scanf("%d", &y);
   //initwindow(900, 900, "Rectangle");
   rectangle(0,0,x,y);
 
   getch();
   closegraph();
   return 0;
}
 
 
OR
 
#include <graphics.h> 	// primitive graphics library
#include <stdlib.h> 
int main()
{ 
   int graphdriver, graphmode;
   int left,top,right,bottom;
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   left=0;
   bottom=getmaxy();
   printf("Enter right: ", right);
   scanf("%d", &right);
   printf("Enter bottom: ", top);
   scanf("%d", &top);
   rectangle(left,top,right,bottom);
   getch();
   closegraph();
   return 0;
}
8) Draw a rectangle with all sides of entered from the keyboard.
#include <graphics.h>	 
#include <stdlib.h> 
int main()
{ 
   int graphdriver, graphmode;
   int left,top,right,bottom; 
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   printf("Enter left: ", left);
   scanf("%d", &left);
   printf("Enter top: ", top);
   scanf("%d", &top);  
   printf("Enter right: ", right);
   scanf("%d", &right);
   printf("Enter bottom: ", bottom);
   scanf("%d", &bottom);
   rectangle(left,top,right,bottom);
   getch();
   closegraph();
   return 0;
}
9) Draw a circle with the abscissa, ordinate and radius entered from the keyboard.
#include <graphics.h> 	
#include <stdlib.h>
int main()
{ 
   int graphdriver, graphmode;
   int x,y,radius;
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   printf("Enter x: ", x);
   scanf("%d", &x);
   printf("Enter y: ", y);
   scanf("%d", &y);
   printf("Enter radius: ", radius);
   scanf("%d", &radius);
   circle(x,y,radius);
   getch();
   closegraph();
   return 0;
}
10) Draw n circles. Abscissa, ordinate and rays are given from the keyboard. Drawing will be done only if the circle is entirely in screen space.
#include "graphics.h"
#include "conio.h"
#include "stdlib.h" 
int main()
{ 
   int graphdriver, graphmode;
   int x[30],y[30],radius[30];
   int n;  
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   printf("Enter the number of circles: ");
   scanf("%d", &n);
   for (int i=0; i<n; i++)
   {
       printf("Circle %d: ",i+1);
       printf("Enter x: ");
       scanf("%d", &x[i]);
       printf("Enter y: ");
       scanf("%d", &y[i]);
       printf("Enter radius: ");
       scanf("%d", &radius[i]);
   }
    
   for (int i=0; i<n; i++)
   if(x[i]>0 && x[i]<getmaxx() && y[i]>0 && y[i]<getmaxy() && (x[i]+radius[i])<getmaxx() && (x[i]>radius[i]) && (y[i]+radius[i])<getmaxy() && (y[i]>radius[i]))
   circle(x[i],y[i],radius[i]);
   getch();
   closegraph();
   return 0;
}
11) Draw an ellipse.
#include <graphics.h>
#include <stdlib.h>
int main()
{ 
   int graphdriver, graphmode;
   int x, y, stangle, endangle, xradius, yradius;
   int n;
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   ellipse(500, 100,0,360, 100,50);
   getch();
   closegraph();
   return 0;
}
12) Draw an ellipse with all data entered from the keyboard.
#include <graphics.h> 	
#include <stdlib.h>
int main()
{ 
   int graphdriver, graphmode;
   int x, y, stangle, endangle, xradius, yradius;
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   printf("Enter x: ");
   scanf("%d", &x);
      printf("Enter y: ");
   scanf("%d", &y);
      printf("Enter stangle: ");
   scanf("%d", &stangle);
      printf("Enter endangle: ");
   scanf("%d", &endangle);
      printf("Enter xradius: ");
   scanf("%d", &xradius);
      printf("Enter yradius: ");
   scanf("%d", &yradius);
   ellipse(x, y, stangle, endangle, xradius, yradius);
   getch();
   closegraph();
   return 0;
}
13) To draw an ellipse that is centered in the center of the screen and all the data entered from the keyboard. Drawing will be done only in case design fits into screen space.
#include <graphics.h> 
#include <stdlib.h>
int main()
{ 
   int graphdriver, graphmode;
   int x, y, stangle, endangle, xradius, yradius;
   graphdriver = DETECT;
   initgraph(&graphdriver, &graphmode, " ");
   x=getmaxx()/2;
   y=getmaxy()/2;
      printf("Enter stangle: ");
   scanf("%d", &stangle);
      printf("Enter endangle: ");
   scanf("%d", &endangle);
      printf("Enter xradius: ");
   scanf("%d", &xradius);
      printf("Enter yradius: ");
   scanf("%d", &yradius);
   if ( (xradius<getmaxy()/2) && (yradius<getmaxx()/2) )
   {
    ellipse(x, y, 0, 350, xradius, yradius);
    getch();
    closegraph();
   }
   else
   {
	closegraph();
	printf("\n\nEllipse can not be drawn in space screen.\n");
	getch();
   }
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.