SlideShare a Scribd company logo
1



            /* Bresenham's Line drawing program */
#include<stdio.h>

#include<math.h>

#include<dos.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,xs,ys,xe,ye,dx,dy,x,y,d,inc1,inc2,xend,yend;

float m;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter line starting & ending end points");

scanf("%d%d%d%d",&xs,&ys,&xe,&ye);

dx=(xe-xs);

dy=(ye-ys);

if(dx==0)

{

x=xs;

if(dy<0)

{

y=ye;

yend=ys;

}

else
2


{

y=ys;

yend=ye;

}

while(y<=yend)

{

    delay(10);

putpixel(x,y,GREEN);

if(d>=0)

{

d=d+inc2;

}

else

d=d+inc1;

y=y+1;

}

}

if(dx!=0)

{

m=(float) dy/dx;

if(m<=1)

{

inc1=2*abs(dy);

inc2=2*abs(dy-dx);

d=inc1-abs(dx);
3


if(dx<0)

{

x=xe;

y=ye;

xend=xs;

}

else

{

x=xs;

y=ys;

xend=xe;

}

while(x<=xend)

{ delay(10);

putpixel(x,y,GREEN);

if(d>=0)

{

y=y+1;

d=d+inc2;

}

else

d=d+inc1;

x=x+1;

}

}
4


else if(m>1)

{

inc1=2*dx;

inc2=2*(dx-dy);

d=inc1-dy;

if(dy<0)

{

x=xe;

y=ye;

xend=xs;

}

else

{

x=xs;

y=ys;

xend=xe;

}

while(x<=xend)

{

putpixel(x,y,RED);

if(d>=0)

{

x=x+1;

d=d+inc2;

}
5


else

d=d+inc1;

y=y+1;

}

}}

getch();

}


              /* Simple DDA Line drawing program */
#include<stdio.h>

#include<process.h>

#include<math.h>

#include<dos.h>

#include<conio.h>

#include<graphics.h>

float round(float r)

{

return(r+0.5);

}

void main()

{

int gdriver=DETECT,gmode,xs,ys,xe,ye,dx,dy,length,xend,yend;

float x,y,xinc,yinc;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter line starting & ending end points");
6


scanf("%d%d%d%d",&xs,&ys,&xe,&ye);

dx=xe-xs;

dy=ye-ys;

if(dx<0)

{

x=xe;

xend=xs;

}

else

{

x=xs;

xend=xe;

}

if(dy<0)

{

y=ye;

yend=ys;

}

else

{

y=ys;

yend=ye;

}

dx=abs(xe-xs);

dy=abs(ye-ys);
7


printf("x=%f y=%f",x,y);

if(dy>dx)

length=dy;

else

length=dx;

if(dx==0 && dy==0)

{

putpixel(x,y,YELLOW);

getch();

exit(0);

}

xinc=(float)dx/length;

yinc=(float)dy/length;

if(dx==0)

{

while(y<=yend)

{

putpixel(floor(round(x)),floor(round(y)),RED);

x=x+xinc;

y=y+yinc;

}

}

else if(dx!=0)

{

while(x<=xend)
8


{

putpixel(floor(round(x)),floor(round(y)),BLUE);

x=x+xinc;

y=y+yinc;

}}

getch();

}


      /* Symmetrical DDA Line drawing program */
#include<stdio.h>

#include<process.h>

#include<math.h>

#include<dos.h>

#include<conio.h>

#include<graphics.h>

float round(float r)

{

return(r+0.5);

}

void main()

{

int gdriver=DETECT,gmode,xs,ys,xe,ye,dx,dy,length,xend,yend;

float x,y,xinc,yinc,n;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter line starting & ending end points");
9


scanf("%d%d%d%d",&xs,&ys,&xe,&ye);

dx=xe-xs;

dy=ye-ys;

if(dx<0)

{

x=xe;

xend=xs;

}

else

{

x=xs;

xend=xe;

}

if(dy<0)

{

y=ye;

yend=ys;

}

else

{

y=ys;

yend=ye;

}

dx=abs(xe-xs);

dy=abs(ye-ys);
10


printf("x=%f y=%f",x,y);

if(dy>dx)

length=dy;

else

length=dx;

if(dx==0 && dy==0)

{

putpixel(x,y,YELLOW);

getch();

exit(0);

}

n=log(length)/log(2);

xinc=(float)dx/pow(2,ceil(n));

yinc=(float)dy/pow(2,ceil(n));

if(dx==0)

{

while(y<=yend)

{

putpixel(floor(round(x)),floor(round(y)),RED);

x=x+xinc;

y=y+yinc;

}

}

else if(dx!=0)

{
11


while(x<=xend)

{

putpixel(floor(round(x)),floor(round(y)),BLUE);

x=x+xinc;

y=y+yinc;

}}

getch();

}


     /* Circle drawing program using Trigonometric method*/
#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,r,h,k;

float xend,x,y,temp,thetainc,theta,thetaend,angle;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter centre coordinates and radius of circle");

scanf("%d%d%d",&h,&k,&r);

temp=3.141/180;

thetainc=1.0/r;

theta=0;

thetaend=45;
12


while(theta<=thetaend)

{

angle=temp*theta;

x=r*cos(angle);

y=r*sin(angle);

putpixel(x+h,y+k,RED);

putpixel(y+h,x+k,RED);

putpixel(-y+h,x+k,RED);

putpixel(-x+h,y+k,RED);

putpixel(-x+h,-y+k,RED);

putpixel(-y+h,-x+k,RED);

putpixel(y+h,-x+k,RED);

putpixel(x+h,-y+k,RED);

theta=theta+thetainc;

}

getch();

}


    /* Circle drawing program using Polynomial method*/
#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,r,h,k;
13


float xend,x,y;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter centre coordinates and radius of circle");

scanf("%d%d%d",&h,&k,&r);

xend=r/1.414;

x=0;

y=r;

while(x<=xend)

{

y=sqrt(r*r-x*x);

putpixel(x+h,y+k,RED);

putpixel(y+h,x+k,RED);

putpixel(-y+h,x+k,RED);

putpixel(-x+h,y+k,RED);

putpixel(-x+h,-y+k,RED);

putpixel(-y+h,-x+k,RED);

putpixel(y+h,-x+k,RED);

putpixel(x+h,-y+k,RED);

x++;

}

getch();

}
14



/* Circle drawing program using Bresenham's method*/
#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,r,h,k,x,y,d;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter centre coordinates and radius of circle");

scanf("%d%d%d",&h,&k,&r);

x=0;

y=r;

d=3-2*r;

while(x<=y)

{

putpixel(x+h,y+k,RED);

putpixel(y+h,x+k,RED);

putpixel(-y+h,x+k,RED);

putpixel(-x+h,y+k,RED);

putpixel(-x+h,-y+k,RED);

putpixel(-y+h,-x+k,RED);

putpixel(y+h,-x+k,RED);

putpixel(x+h,-y+k,RED);
15


if(d<0)

d=d+4*x+6;

else

{

d=d+4*(x-y)+10;

y--;

}

x++;

}

getch();

}


/* Circle drawing program using Mid-Point method*/
#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,r,h,k,x,y,d;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter centre coordinates and radius of circle");

scanf("%d%d%d",&h,&k,&r);

x=0;

y=r;
16


d=1-r;

while(x<=y)

{

putpixel(x+h,y+k,RED);

putpixel(y+h,x+k,RED);

putpixel(-y+h,x+k,RED);

putpixel(-x+h,y+k,RED);

putpixel(-x+h,-y+k,RED);

putpixel(-y+h,-x+k,RED);

putpixel(y+h,-x+k,RED);

putpixel(x+h,-y+k,RED);

if(d<0)

d=d+2*x+3;

else

{

d=d+2*(x-y)+5;

y--;

}

x++;

}

getch();

}
17


    /* Ellipse drawing program using Trigonometric method*/
#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,r,h,k,a,b;

float xend,x,y,temp,thetainc,theta,thetaend,angle;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter centre coordinates of ellipse");

scanf("%d%d",&h,&k);

printf("enter major and minor axis of ellipse");

scanf("%d%d",&a,&b);

temp=3.141/180;

thetainc=3.141/(2*a);

theta=0;

thetaend=90;

while(theta<=thetaend)

{

angle=temp*theta;

x=a*cos(angle);

y=b*sin(angle);

putpixel(x+h,y+k,RED);
18


putpixel(-x+h,y+k,RED);

putpixel(-x+h,-y+k,RED);

putpixel(x+h,-y+k,RED);

theta=theta+thetainc;

}

getch();

}


    /* Ellipse drawing program using Polynomial method*/
#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gdriver=DETECT,gmode,r,h,k,a,b;

float xend,x,y;

clrscr();

initgraph(&gdriver,&gmode,"");

printf("enter centre coordinates of ellipse");

scanf("%d%d",&h,&k);

printf("enter major and minor axis of ellipse");

scanf("%d%d",&a,&b);

x=0;

xend=a;

while(x<=xend)
19


{

y=b*sqrt(1-(x*x)/(a*a));

putpixel(x+h,y+k,RED);

putpixel(-x+h,y+k,RED);

putpixel(-x+h,-y+k,RED);

putpixel(x+h,-y+k,RED);

x++;

}

getch();

}




                            Laing Barsky line clipping algorithm
#include<graphics.h>

#include<dos.h>

#include<conio.h>

#include<stdlib.h>



void main()

{

int gd, gm ;

int x1 , y1 , x2 , y2 ;

int wxmin,wymin,wxmax, wymax ;

float u1 = 0.0,u2 = 1.0 ;

int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;

float r1 , r2 , r3 , r4 ;
20


int x11 , y11 , x22 , y22 ;

clrscr();

printf("Enter the windows left xmin , top boundry yminn");

scanf("%d%d",&wxmin,&wymin);

printf("Enter the windows right xmax ,bottom boundry ymaxn");

scanf("%d%d",&wxmax,&wymax);

printf("Enter line x1 , y1 co-ordinaten");

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

printf("Enter line x2 , y2 co-ordinaten");

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

printf("liang barsky express these 4 inequalities using lpk<=qpkn");

p1 = -(x2 - x1 ); q1 = x1 - wxmin ;

p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;

p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;

p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;

printf("p1=0 line is parallel to left clippingn");

printf("p2=0 line is parallel to right clippingn");

printf("p3=0 line is parallel to bottom clippingn");

printf("p4=0 line is parallel to top clippingn");



if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||

( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||

( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||

( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )

{
21


printf("Line is rejectedn");

getch();

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"c:tcbgi");

setcolor(RED);

rectangle(wxmin,wymax,wxmax,wymin);

setcolor(BLUE);

line(x1,y1,x2,y2);

getch();

setcolor(WHITE);

line(x1,y1,x2,y2);

getch();

}

else

{

if( p1 != 0.0 )

{

r1 =(float) q1 /p1 ;

if( p1 < 0 )

u1 = max(r1 , u1 );

else

u2 = min(r1 , u2 );

}

if( p2 != 0.0 )

{
22


r2 = (float ) q2 /p2 ;

if( p2 < 0 )

u1 = max(r2 , u1 );

else

u2 = min(r2 , u2 );



}

if( p3 != 0.0 )

{

r3 = (float )q3 /p3 ;

if( p3 < 0 )

u1 = max(r3 , u1 );

else

u2 = min(r3 , u2 );

}

if( p4 != 0.0 )

{

r4 = (float )q4 /p4 ;

if( p4 < 0 )

u1 = max(r4 , u1 );

else

u2 = min(r4 , u2 );

}



if( u1 > u2 )
23


printf("line rejectedn");

else

{

x11 = x1 + u1 * ( x2 - x1 ) ;

y11 = y1 + u1 * ( y2 - y1 ) ;



x22 = x1 + u2 * ( x2 - x1 );

y22 = y1 + u2 * ( y2 - y1 );



printf("Original line cordinatesn");

printf("x1 = %d , y1 = %d, x2 = %d, y2 = %dn",x1,y1,x2,y2);

printf("Windows coordinate are n");

printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax);



printf("New coordinates are n");

printf("x1 = %d, y1 = %d,x2 = %d , y2 = %dn",x11,y11,x22,y22);

detectgraph(&gd,&gm);

initgraph(&gd,&gm,"C:TCBGI");

setcolor(2);

rectangle(wxmin,wymax,wxmax,wymin);

setcolor(1);

line(x1,y1,x2,y2);

getch();

setcolor(0);

line(x1,y1,x2,y2);
24


setcolor(3);

line(x11,y11,x22,y22);

getch();

}

}

}


                                    2d transformation
#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<graphics.h>

int x1,y1,x2,y2;

void translation()

{

int tx,ty,xn1,xn2,yn1,yn2;

printf("Enter the Translation Vector:");

scanf("%dn%d",&tx,&ty);

cleardevice();

outtextxy(400,100,"TRANSLATION");

xn1=x1+tx;

yn1=y1+ty;

xn2=x2+tx;

yn2=y2+ty;

line(x1,y1,x2,y2);

line(xn1,yn1,xn2,yn2);
25


getch();

}

void scaling()

{

int xn1,yn1,xn2,yn2;

float sx,sy;

printf("Enter the Sacling Factor:");

scanf("%f%f",&sx,&sy);

cleardevice();

outtextxy(300,200,"SCALING");

xn1=x1*sx;

yn1=y1*sy;

xn2=x2*sx;

yn2=y2*sy;

line(x1,y1,x2,y2);

line(xn1,yn1,xn2,yn2);

getch();

}

void rotation()

{

int r;

float rx,xn1,yn1,xn2,yn2;

printf("Enter the Angle for Rotation:");

scanf("%d",&r);

cleardevice();
26


outtextxy(500,200,"ROTATION");

rx=(r*3.14)/180;

xn1=x1*cos(rx)-y1*sin(rx);

yn1=y1*cos(rx)+x1*sin(rx);

xn2=x2*cos(rx)-y2*sin(rx);

yn2=y2*cos(rx)+x2*sin(rx);

line(x1,y1,x2,y2);

line(xn1,yn1,xn2,yn2);

getch();

}

void shearing()

{

int sh;

float xn1,yn1,xn2,yn2;

printf("Enter the value of Shearing");

scanf("%d",&sh);

cleardevice();

outtextxy(500,100,"SHEARING");

xn1=x1+sh*y1;

yn1=y1;

xn2=x2+sh*y2;

yn2=y2;

line(x1,y1,x2,y2);

line(xn1,yn1,xn2,yn2);

getch();
27


}

void reflection()

{

int xn1,yn1,xn2,yn2;

cleardevice();

outtextxy(300,100,"REFLECTION");

if((x1<y1)||(x2<y2))

{

xn1=x1+50;

xn2=x2+50;

yn1=y1;

yn2=y2;

}

else

{

xn1=x1;

xn2=x2;

yn1=y1+50;

yn2=y2+50;

}

line(x1,y1,x2,y2);

line(xn1,yn1,xn2,yn2);

getch();

}

void get()
28


{

printf("Enter the Co-ordinates x1,y1,x2,y2:");

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

cleardevice();

outtextxy(200,100,"ORIGINAL OBJECT");

line(x1,y1,x2,y2);

getch();

}

void main()

{

int ch,gd=DETECT,gm;

initgraph(&gd,&gm,"C:/tc/bgi");

get();

do

{

cleardevice();

outtextxy(10,10,"1.Translation");

outtextxy(10,20,"2.Scaling");

outtextxy(10,30,"3.Rotation");

outtextxy(10,40,"4.Shearing");

outtextxy(10,50,"5.Reflection");

outtextxy(10,60,"6.Exit");

outtextxy(10,70,"Enter Your Choice:");

scanf("%d",&ch);

switch(ch)
29


{

case 1:

{

translation();

break;

}

case 2:

{

scaling();

break;

}

case 3:

{

rotation();

break;

}

case 4:

{

shearing();

break;

}

case 5:

{

reflection();

break;
30


}

case 6:

{

exit(0);

break;

}

}

}

while(ch<6);

}




                        2d Viewing Transformation
#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gm,gr,xwmin,ywmin,xwmax,

ywmax,xvmin,yvmin,xvmax,yvmax,xw,yw,xv,yv,sx,sy;

clrscr();

detectgraph(&gm,&gr);

initgraph(&gm,&gr,"d:tcBGI");

printf("Enter the window min coordinate(xwmin,ywmin):n");

scanf("%d%d",&xwmin,&ywmin);

printf("Enter the window max coordinate(xwmax,ywmax):n");
31


scanf("%d%d",&xwmax,&ywmax);

printf("Enter the viewport min coordinate(xvmin,yvmin):n");

scanf("%d%d",&xvmin,&yvmin);

printf("Enter the viewport max coordinate(xvmax,yvmax):n");

scanf("%d%d",&xvmax,&yvmax);

sx=(xvmax-xvmin)/(xwmax-xwmin);

sy=(yvmax-yvmin)/(ywmax-ywmin);

printf("Enter the point coordinate(xw,yw) in the window:n");

scanf("%d%d",&xw,&yw);

xv=(sx*(xw-xwmin))+xvmin;

yv=(sy*(yw-ywmin))+yvmin;

cleardevice();

getch();

rectangle(xwmin,ywmax,xwmax,ywmin);



putpixel(xw,yw,15); getch();

cleardevice();getch();

rectangle(xvmin,yvmax,xvmax,yvmin);

putpixel(xv,yv,15);

getch();

}

More Related Content

PDF
Computer graphics lab report with code in cpp
DOCX
Graphics practical lab manual
PPTX
Bresenham circle
PPTX
Chapter 3 Output Primitives
PPT
Circle drawing algo.
DOC
COMPUTER GRAPHICS LAB MANUAL
PPTX
DOC
Computer graphics
Computer graphics lab report with code in cpp
Graphics practical lab manual
Bresenham circle
Chapter 3 Output Primitives
Circle drawing algo.
COMPUTER GRAPHICS LAB MANUAL
Computer graphics

What's hot (20)

PPT
Object and class
PPTX
Kruskal Algorithm
PDF
LET US C (5th EDITION) CHAPTER 1 ANSWERS
DOCX
Computer Graphics Lab File C Programs
DOCX
Java Programs Lab File
PPTX
Static keyword ppt
PPT
bresenham circle algorithm .ppt
PPT
Ellipses drawing algo.
PPTX
BRESENHAM’S LINE DRAWING ALGORITHM
DOCX
Java practical
DOC
Network lab manual
PPTX
Fractional Knapsack Problem
PPTX
Mid-Point Cirle Drawing Algorithm
PPTX
Computer Graphics
PPT
Boundary fill algm
PPTX
Bressenham’s Midpoint Circle Drawing Algorithm
PPTX
07. Virtual Functions
PDF
design and analysis of algorithm Lab files
PPTX
Android - ADB
PPTX
Member Function in C++
Object and class
Kruskal Algorithm
LET US C (5th EDITION) CHAPTER 1 ANSWERS
Computer Graphics Lab File C Programs
Java Programs Lab File
Static keyword ppt
bresenham circle algorithm .ppt
Ellipses drawing algo.
BRESENHAM’S LINE DRAWING ALGORITHM
Java practical
Network lab manual
Fractional Knapsack Problem
Mid-Point Cirle Drawing Algorithm
Computer Graphics
Boundary fill algm
Bressenham’s Midpoint Circle Drawing Algorithm
07. Virtual Functions
design and analysis of algorithm Lab files
Android - ADB
Member Function in C++
Ad

Similar to Cg my own programs (20)

DOCX
C graphics programs file
DOC
Computer graphics
DOCX
Computer graphics File for Engineers
DOCX
Computer graphics
DOCX
Computer graphics lab assignment
DOCX
Computer Graphics and Multimedia lab report
DOCX
Graphics programs
DOCX
Graphics point clipping c program
PDF
Computer graphics lab manual
PPT
PDF
10CSL67 CG LAB PROGRAM 5
PDF
Open GL T0074 56 sm2
PDF
Open GL 09 scan conversion
DOCX
Rasterisation of a circle by the bresenham algorithm
DOCX
Rasterisation of a circle by the bresenham algorithm
PPTX
Output primitives in Computer Graphics
PDF
Computer graphics lab manual
PDF
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
DOCX
Graphic Design Lab File.docx
C graphics programs file
Computer graphics
Computer graphics File for Engineers
Computer graphics
Computer graphics lab assignment
Computer Graphics and Multimedia lab report
Graphics programs
Graphics point clipping c program
Computer graphics lab manual
10CSL67 CG LAB PROGRAM 5
Open GL T0074 56 sm2
Open GL 09 scan conversion
Rasterisation of a circle by the bresenham algorithm
Rasterisation of a circle by the bresenham algorithm
Output primitives in Computer Graphics
Computer graphics lab manual
CD504 CGM_Lab Manual_004e08d3838702ed11fc6d03cc82f7be.pdf
Graphic Design Lab File.docx
Ad

More from Amit Kapoor (17)

PPT
Shading
DOCX
Tweening and morphing
DOCX
Study from where
PPT
Projection
DOC
Display devices
DOC
Computer graphics syllabus
PPTX
Animation
DOCX
3 d modeling of objects
PDF
Essential c
PDF
C aptitude scribd
PDF
C in7-days
DOCX
C interview question answer 2
DOCX
C interview question answer 1
DOCX
C notes mca i sem 2011
DOCX
C tutorials
PPT
User defined data type
PPT
2 d transformations by amit kumar (maimt)
Shading
Tweening and morphing
Study from where
Projection
Display devices
Computer graphics syllabus
Animation
3 d modeling of objects
Essential c
C aptitude scribd
C in7-days
C interview question answer 2
C interview question answer 1
C notes mca i sem 2011
C tutorials
User defined data type
2 d transformations by amit kumar (maimt)

Cg my own programs