You are on page 1of 25

COMPUTER GRAPHICS PRATICAL FILE

SUBMITTED BY: SUBMITTED TO:


SANDEEP SAINI SHRUTI MAM
ROLL NO-2145027
MSC IT 3Rd SEM
S.NO PROGRAM PAGE
1 program To draw a line using Direct
method.
2 DDA Line Drawing
3 Program to draw a circle using
Midpoint Algorithm
4  Breseham's Line Drawing
program
5 Bresenham's Circle Drawing
algorithm
6 Program to draw a ARC
7 Program to draw a RECTANGLE
8 Program to draw a CIRCLE
9 Program to draw 3 LINEs:
10 Program to draw ECILPSE
11 Program to clear screen in graphics
mode and set current position to(0,0)

12 CIRCLE DRAWING USING


( POLYNOMIAL METHOD ALGORITHM)
1program To draw a line using Direct method.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int x1,y1,x2,y2,x,y,dy,dx,b;
float m;
cout<<“\n\t\t**Line drawing using Direct OR Polynomial method**\n”;
cout<<“\nenter the x coordinates(x1,x2)\n”;
cin>>x1>>x2;
cout<<“\nenter the y coordinates(y1,y2)\n”;
cin>>y1>>y2;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
b=y1-(m*x1);
x=x1;
y=y1;
int gd=DETECT,gm;
initgraph(&gd,&gm,””);
setbkcolor(WHITE);
if(m<1)
while(x<=x2)
{

putpixel(x,int(y+0.5),8);
x++;
y=(m*x)+b;
}
else
{
while(y<=y2)
{
putpixel(int(x+0.5),y,8);
y++;
x=(y-b)/m;
}
}
setcolor(8);
outtextxy(100,60,”Line using polynomial method”);
getch();
}
2DDA Line Drawing
#include<graphics.h> 
#include<conio.h>  
#include<stdio.h>  
void main()  
{  
    intgd = DETECT ,gm, i;  
    float x, y,dx,dy,steps;  
    int x0, x1, y0, y1;  
    initgraph(&gd, &gm, "C:\\TC\\BGI");  
    setbkcolor(WHITE);  
    x0 = 100 , y0 = 200, x1 = 500, y1 = 300;  
    dx = (float)(x1 - x0);  
    dy = (float)(y1 - y0);  
    if(dx>=dy)  
           {  
        steps = dx;  
    }  
    else  
           {  
        steps = dy;  
    }  
    dx = dx/steps;  
    dy = dy/steps;  
    x = x0;  
    y = y0;  
    i = 1;  
    while(i<= steps)  
    {  
        putpixel(x, y, RED);  
        x += dx;  
        y += dy;  
        i=i+1; 
}  
    getch();  
    closegraph();  
}  

OUTPUT OFF DDA:


3Program to draw a circle using Midpoint Algorithm:
#include <graphics.h>  
#include <stdlib.h>  
#include <math.h>  
#include <stdio.h>  
#include <conio.h>  
#include <iostream.h>  
  
class bresen  
{  
    float x, y,a, b, r, p;  
    public:  
    void get ();  
    void cal ();  
};  
    void main ()  
    {  
    bresen b;  
    b.get ();  
    b.cal ();  
    getch ();  
   }  
    Void bresen :: get ()  
   {  
    cout<<"ENTER CENTER AND RADIUS";  
     cout<< "ENTER (a, b)";  
    cin>>a>>b;  
    cout<<"ENTER r";  
    cin>>r;  
}  
void bresen ::cal ()  
{  
    /* request auto detection */  
    int gdriver = DETECT,gmode, errorcode;  
    int midx, midy, i;  
    /* initialize graphics and local variables */  
    initgraph (&gdriver, &gmode, " ");  
    /* read result of initialization */  
    errorcode = graphresult ();  
    if (errorcode ! = grOK)    /*an error occurred */  
    {  
        printf("Graphics error: %s \n", grapherrormsg (errorcode);  
        printf ("Press any key to halt:");  
        getch ();  
        exit (1); /* terminate with an error code */  
    }  
    x=0;  
    y=r;  
    putpixel (a, b+r, RED);  
    putpixel (a, b-r, RED);  
    putpixel (a-r, b, RED);  
    putpixel (a+r, b, RED);  
    p=5/4)-r;  
    while (x<=y)  
    {  
        If (p<0)  
        p+= (4*x)+6;  
        else  
        {  
            p+=(2*(x-y))+5;  
            y--;  
        }  
        x++;  
        putpixel (a+x, b+y, RED);  
        putpixel (a-x, b+y, RED);  
        putpixel (a+x, b-y, RED);  
        putpixel (a+x, b-y, RED);  
        putpixel (a+x, b+y, RED);  
        putpixel (a+x, b-y, RED);  
        putpixel (a-x, b+y, RED);  
        putpixel (a-x, b-y, RED);  
    }  
}  
OUTPUT OF MIDPOINT CIRCLE:
4 Breseham's Line Drawing program

#include<graphics.h>
#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void main()
{
 float x,y,x1,y1,x2,y2,dx,dy,e;
 int i,gd=DETECT,gm;
 clrscr();
/* Read two end points of line */
 cout<<"Enter the value of x1 :\t";
 cin>>x1;
 cout<<"Enter the value of y1 :\t";
 cin>>y1;
 cout<<"Enter the value of x2 :\t";
 cin>>x2;
 cout<<"Enter the value of y2 :\t";
 cin>>y2;
/* Initialise graphics mode*/
 initgraph(&gd,&gm,"c:/tc/bgi");

   dx=abs(x2-x1);
   dy=abs(y2-y1);
/* Initialise starting point
-----------------------------*/
   x = x1;
   y = y1;
/* Initialise decision variable
-------------------------------- */
   e=2*dy-dx;
   i=1;    /* Initialise loop counter */
do
 {
  putpixel(x,y,WHITE);
  delay(60);
  while(e >= 0)
     {
      y=y+1;
      e=e-2*dx;
     }
  x=x+1;
  e=e+2*dy;
  i=i+1;
 }
while(i <= dx);
 getch();
 closegraph();
}

OUTPUT OF BRESENHAM LINE DRWAING :


5Bresenham's Circle Drawing algorithm*/
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<iostream.h>
void main()
{
float d;
int gd,gm,x,y,midx,midy;
int r;
clrscr();

/* Read the radius of the circle


---------------------------------- */
cout<<"\tEnter the radius of a circle :";
cin>>r;

/* Initialise graphics mode


------------------------------*/
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
/* Initialise starting points
-------------------------------*/
x = 0;
y = r;
midx=getmaxx()/2;
midy=getmaxy()/2;
/* initialise the decision variable
---------------------------------------*/
d = 3 - 2 * r;
do
{

putpixle(midx+x,midy-y.BLUE);
putpixel(midx+y,midy+x,GREEN);
putpixel(midx+y,midy-x,RED);
putpixel(midx+x,midy-y,ORANGE);
putpixel(midx-x,midy-y,PURPLE);
putpixel(midx-y,midy+x,GREY);
putpixel(midx-x,midy+y,LIGHTRED);
if (d <= 0)
{
d = d + 4*x + 6;
}
else
{
d = d + 4*(x-y) + 10;
y = y - 1;
}
x = x + 1;
delay(50);  /* Delay is purposely inserted to see
 observe the circle drawing process */
}
while(x < y);
getch();
closegraph();
}

OUTPUT OF BRESENHAM CIRCLE :


6Program to draw a ARC:
#include <graphics.h>
int main()
{
    int gd = DETECT, gm;
  
    int x = 250;
    int y = 250;
  
    
    int start_angle = 155;
    int end_angle = 300;
  
    
    int radius = 100;
  
    initgraph(&gd, &gm, "");
  
    
    arc(x, y, start_angle, end_angle, radius);
  
    getch();
  
    closegraph();
  
    return 0;
}

OUTPUT :
7Program to draw a RECTANGLE:
#include <graphics.h>
  
int main()
{
    int gd = DETECT, gm;
  
    
    int left = 150, top = 150;
    int right = 450, bottom = 450;
  
    
    initgraph(&gd, &gm, "");
  
   
    rectangle(left, top, right, bottom);
  
    getch();
  
    closegraph();
  
    return 0;
}

OUTPUT :
8 Program to draw a CIRCLE:

#include <graphics.h>
  
int main()
{
    int gd = DETECT, gm;
  
   initgraph(&gd, &gm, "");
  
   circle(250, 200, 50);
  
    getch();
  
    closegraph();
  
    return 0;
}

OUTPUT :
9 Program to draw 3 LINEs:

#include <graphics.h>
  
int main()
{
    int gd = DETECT, gm;
  
    
    initgraph(&gd, &gm, "");
  
    line(150, 150, 450, 150);
  
    
    line(150, 200, 450, 200);
  
    line(150, 250, 450, 250);
  
    getch();
  
    closegraph();
}

OUTPUT :
10 Program to draw ECILPSE:
#include <graphics.h>
  
int main()
{
    
    int gd = DETECT, gm;
  
   
    int x = 250, y = 200;
  
    
    int start_angle = 0;
    int end_angle = 360;
  
  
    int x_rad = 100;
    int y_rad = 50;
  
    
    initgraph(&gd, &gm, "");
  
    ellipse(x, y, start_angle,
     end_angle, x_rad, y_rad);
  
    getch();
  
    closegraph();
  
    return 0;
}

OUTPUT :
11 Program to clear screen in graphics mode and set current
position to(0,0)

#include <graphics.h>
  
int main()
{
    
    int gd = DETECT, gm;
  
    
    initgraph(&gd, &gm, "");
  
    setbkcolor(GREEN);
  
    
    outtext("Press any key to clear the screen.");
  
    getch();
  
    cleardevice();
  
    outtext("Press any key to exit...");
  
    getch();
  
    closegraph();
  
    return 0;
}

OUTPUT :
12 CIRCLE DRAWING USING ( POLYNOMIAL
METHOD ALGORITHM)

#include<graphics.h>  
#include<conio.h>  
#include<math.h>  
voidsetPixel(int x, int y, int h, int k)  
{  
    putpixel(x+h, y+k, WHITE);  
    putpixel(x+h, -y+k, WHITE);  
    putpixel(-x+h, -y+k, WHITE);  
    putpixel(-x+h, y+k, WHITE);  
    putpixel(y+h, x+k, WHITE);  
    putpixel(y+h, -x+k, WHITE);  
    putpixel(-y+h, -x+k, WHITE);  
    putpixel(-y+h, x+k, WHITE);  
}  
main()  
{  
    intgd=0, gm,h,k,r;  
    double x,y,x2;  
    h=200, k=200, r=100;  
    initgraph(&gd, &gm, "C:\\TC\\BGI ");  
    setbkcolor(WHITE);  
    x=0,y=r;  
    x2 = r/sqrt(2);  
    while(x<=x2)  
    {  
        y = sqrt(r*r - x*x);  
        setPixel(floor(x), floor(y), h,k);  
        x += 1;  
    }  
    getch();  
    closegraph();  
    return 0;  
}  

OUTPUT :

You might also like