Computer Graphics Practical


Published on

Published in: Technology, Art & Photos
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Computer Graphics Practical

  1. 1. PUNJAB COLLEGE OF TECHNICAL EDUCATIONPRACTICAL FILE BC-507SUBMITTED BY: SUBMITTED BY:Ms.Richa Sharma Neha SharmaAssistant Professor (94972346352)P.C.T.E. BCA-5th Sem Page | 0
  2. 2. IndexS.No Contents Page no. Remarks1. Introduction to graphics 22. Applications of graphics 3-53. Graphics In-built functions 6-154. Programs of Algorithm 16-26 a) Direct method of line b) DDA Algorithm c) Bresenham algorithm d) Direct method of Circle e) Polar Circle f) Bresenham Circle g) Midpoint Circle h) Direct Ellipse i) Polar Ellipse j) Mid point Ellipse5. Static Application6. Ist dynamic Application7. IInd Dynamic Application Page | 1
  3. 3. 1. Introduction To GraphicsComputer graphics are graphics created using computers and, more generally, therepresentation and manipulation of image data by a computer with help from specializedsoftware and hardware.The development of computer graphics has made computers easier to interact with, andbetter for understanding and interpreting many types of data. Developments in computergraphics have had a profound impact on many types of media and have revolutionizedanimation, movies and the video game industry.the term computer graphics refers to several different things: the representation and manipulation of image data by a computer the various technologies used to create and manipulate images the images so produced, and the sub-field of computer science which studies methods for digitally synthesizing and manipulating visual content Page | 2
  4. 4. 2. Application of GraphicsComputer graphics may be used in the following areas: COMPUTER AIDED DESIGN PRESENTATION GRAPHICS COMPUTER ART ENTERTAINMENT EDUCATION AND TRAINING VISUALIZATION IMAGE PROCESSING GRAPHICAL USER INTERFACE VIDEO GAMESComputers have become a powerful tool for the rapid and economical production ofpictures. Advances in computer technology have made interactive computer graphics apractical tool. Today, computer graphics is used in the areas as science, engineering,medicine, business, industry, government, art, entertainment, advertising, education, andtraining.COMPUTER AIDED DESIGN A major use of computer graphics is in design processes, particularly forengineering and architectural systems. For some design applications; objects are firstdisplayed in a wireframe outline form that shows the overall sham and internal features ofobjects. Software packages for CAD applications typically provide the designer with a multi-window environment. Each window can show enlarged sections or different views ofobjects. Standard shapes for electrical, electronic, and logic circuits are often supplied bythe design package. The connections between the components have been madautomatically. Animations are often used in CAD applications. Real-time animations using wire frame displays are useful for testing performance of a vehicle. Wire frame models allow the designer to see the interior parts of the vehicle during motion. When object designs are complete, realistic lighting models and surface rendering are applied. Manufacturing process of object can also be controlled through CAD. Page | 3
  5. 5. Interactive graphics methods are used to layout the buildings. Three-dimensional interior layouts and lighting also provided. With virtual-reality systems, the designers can go for a simulated walk inside the building.PRESENTATION GRAPHICS  It is used to produce illustrations for reports or to generate slide for with projections.  Examples of presentation graphics are bar charts, line graphs, surface graphs, pie charts and displays showing relationships between parameters.  3-D graphics can provide more attraction to the presentation.COMPUTER ART  Computer graphics methods are widely used in both fine are and commercial art applications.  The artist uses a combination of 3D modeling packages, texture mapping, drawing programs and CAD software.  Pen plotter with specially designed software can create “automatic art”.  “Mathematical Art” can be produced using mathematical functions, fractal procedures.  These methods are also applied in commercial art.  Photorealistic techniques are used to render images of a product.  Animations are also used frequently in advertising, and television commercials are produced frame by frame. Film animations require 24 frames for each second in the animation sequence.  A common graphics method employed in many commercials is morphing, where one object is transformed into another.ENTERTAINMENT  CG methods are now commonly used in making motion pictures, music videos and television shows.  Many TV series regularly employ computer graphics method.  Graphics objects can be combined with a live action.EDUCATION AND TRAINING  Computer-generated models of physical, financial and economic systems are often used as educational aids. Page | 4
  6. 6.  For some training applications, special systems are designed.  Eg. Training of ship captains, aircraft pilots etc.,  Some simulators have no video screens, but most simulators provide graphics screen for visual operation. Some of them provide only the control panel.VISUALIZATION  The numerical and scientific data are converted to a visual form for analysis and to study the behavior called visualization.  Producing graphical representation for scientific data sets are calls scientific visualization.  And business visualization is used to represent the data sets related to commerce and industry.  The visualization can be either 2D or 3D.IMAGE PROCESSING  Computer graphics is used to create a picture.  Image processing applies techniques to modify or interpret existing pictures.  To apply image processing methods, the image must be digitized first.  Medical applications also make extensive use of image processing techniques for picture enhancements, simulations of operations, etc.GRAPHICAL USER INTERFACE  Nowadays software packages provide graphics user interface (GUI) for the user to work easily.  A major component in GUI is a window.  Multiple windows can be opened at a time.  To activate any one of the window, the user needs just to check on that window.  Menus and icons are used for fast selection of processing operations.  Icons are used as shortcut to perform functions. The advantages of icons are which takes less screen space.  And some other interfaces like text box, buttons, and list are also used. Page | 5
  7. 7. 3. Inbuilt Functions:1. ArcArc draws a circular arc in the current drawing color.Declaration:Void arc(int x, int y , int Stangle, int endangle , int radius )ARGUMENT What it is Does(x,y) Center point of arcStangle Start angle in degreesEndangle End angle in degreesRadius Radius of arcThe arc travels from Stangle to EndangleIf Stangle =0 and Endangle = 360 , the call to arc draws a complete circle2. InitgraphInitializes the graphic systemDecalaration:Void far initgraph(int far * graphdriver);Int far * graphmode , char far *pathdriver);Remarks:To start the graphics system , you must first call the initgraphInitgraph initializes the graphics system by loading a graphics the driver from disk (orvalidating a registered driver ) then putting the system into graphics mode . Page | 6
  8. 8. Initgraph also resets all the graphics settings (color ,palette ,current position , viewport,etc) to their defaults , then resets graphresult to 0 .Argument What it is does*graphdriver Integer that specifies the graphic driver to be used . you can give graphdriver a value using a constant of the graphics_driversenumeration type .*graphmode Integer that specifies the initial graphics mode .(unless *graphdriver =DETECT).If *graphdriver =DETECT , initgraph sets *graphmode to the highest resolution availablefor the detected driver . You can give the *graphmode a value using a constant of thegraphics_modes enumeration type .*graphdriver and *graphmode must be set to the valid graphics _drivers andgraphics_mode values or you will get unpredictable results . ( the exception is graphdriver= DETECT).After a call to the initgraph , *graphdriver is set to the current graphics driver , and*graphmode is set to the current graphics mode .3. closegraphShuts down the graphics systemDeclaration :void far closegraph( void );Remarks :Closegraph deallocates all memory allocated by the graphics system . It then restores thescreen to the mode it was in before you called initgraph .( the graphics system deallocates memory such as the drivers , fonts , and an internalbuffer , through a call to the _graphfreemem) Page | 7
  9. 9. Return Value : None4.lineLine draws a line between the two specified pointsDeclaration :Void far line ( int x1, int y1 , int x2 , int y2 );Remarks :Line draws a line from the ( x1, y1 ) to ( x2,y2 ) using the current color , line style nad thickness . it does not the update the current position (cp).5. setcolorSetcolor sets the current drawing colorDeclaration:Void far setcolor( int color)Remarks :Setcolor sets the current drawing color to color , which can range from 0 to getmaxcolorTo select a drawing color is the value that pixels are set to the when the program drawslines etc.Return value :Setcolor does not return6.setbkcolor Page | 8
  10. 10. Setbkcolor sets the current background the color using the paletteDeclaration :Void far setbkcolor( int color );Remarks :Setbkcolor sets the background to the color specified by colorColor :color is either a number or symbolic name specifying the color to setFor example , if you want to set the background color to blue , you can callsetbkcolor (BLUE) /* or */ setbkcolor(1)On CGA and EGA systems , setbkcolor changes the background color by changing the firstentry in the palette.Return:Setbkcolor does not return7.EllipseEllipse draws an elliptical arcDeclaration :Void far ellipse ( int x, int y , int stangle , int endangle , int xradius , int yradius ) ;Remarks :Ellipse draws an elliptical arc in the current drawing color .Argument What it is( x,y) Center of ellipseXradius Horizontal axisYradius Vertical axis Page | 9
  11. 11. Stangle starting angleEndangle ending angleThe ellipse travels from stangle to endangle .If stangle = 0 and endangle = 360 , the call to the ellipse draws a complete ellipse .The linestyle parameter does not affect the arcs , circles , ellipse or pie slices . only thethickness parameter is used .Return value :None8.Flood fill :Flood fills a bounded regionDeclaration :void far floodfill( int x, int y , int border );Remarks :Floodfill fills an enclosed area on bitmap devices .The area bounded by the color border is flooded with the current fill pattern and fill color .(x,y) is the “seed point ”.  If the seed is within an enclosed area , the inside will be filled .  If the seed is outside the enclosed area, the exterior will be filled .Use fillpoly instead of floodfill whenever possible so you can maintain code compatibilitywith future versions .9.Outtextxy Page | 10
  12. 12. Outtextxy displays a string at the specified location ( graphics mode )Declaration :Void far outtextxy (int x , int y , char far * textstring );Remarks :Outtextxy displays a text string using the current justification settings and the current font, direction and size .Outtextxy displays the textstring in the viewport at the position ( x,y)To maintain the code compatibility when using the several fonts , use textwidth andtextheight to determine the dimensions of the string .If a string is printed with the default font using the outtext ot outtextxy ,any part of thestring that extends outside the current viewport is truncated .Outtextxy for use in graphics mode , they will not work in the text mode.Return value :None10.SettextjustifySets text justification for graphics mode .Decalration :void far settextjustify ( int horiz , int vert ) ;Remarks :Text output after a call to settextjustify is justified around the current position (CP)horizontally and vertically , as specified .The default justification settings are :  LEFT_TEXT ( for horizontal )  TOP_TEXT ( for vertical ) Page | 11
  13. 13. The enumeration text_justify in GRAPHICS. H provides names for the horiz and vertsettings passed to the settextjustify . Settextjusitfy affects the text written with outtext and cant be used with text – mode andstream functions .Return Values :If invalid input is passed to the settextjustify , graph result returns -11 and the current textjustification remains unchanged .11.CircleCircle draws a circleDecalration :Void far circle(int x , int y , int radius );ARGUMENT What it is Does(x,y) Center point of circleRadius Radius of circleRemarks :It draws a circle in the current drawing color.12.Rectangle:Draws a rectangle ( graphics mode )Declaration :Void far rectangle (int left , int top , int right , int bottom );Remarks :Rectangle draws a rectangle in the current line style , thickness and drawing color. Page | 12
  14. 14. (left , top ) is the upper left corner of the rectangle , and (right , bottom ) is the lower rightcorner .Return Values :None13.Setlinestyle:Set the current line style and width or patternDeclaration :void far setlinestyle ( int line style , unsigned upattern , int thickness);Remarks :Setlinestyle sets the style for all lines drawn by line , rectangle etc.Return Values :If invalid input is passed to setlinestyle , graphresult returns -11 , the current line styleremains unchanged .14.Getmaxx ()and Getmaxy()Return maximum x or y co-ordinates of screen.Declaration :Int far getmaxx (void );Int far getmaxy (void );Remarks :1 . getmaxx returns the maximum X value (screen relative ) for the current graphicsdriver and mode . Page | 13
  15. 15. 2. getmaxy returns the maximum Y value ( screen relative ) for the current graphicsdrivers and mode .For Example :On a CGA in 320 * 200 mode , getmaxx returns 319 and getmaxy returns the 199Return Values :1) Getmaxx : maximum x screen co-ordinates.2) Getmaxy : maximum y screen co-ordinates .15.Cleardevice()Clears the graphic screen .Declaration :Void far cleardevice (void);Remarks :Cleardevice erases the entire graphics screen and moves the CP (Current Position ) tohome (0,0).Return Values : None.16.Setfillstyle()Set the fill pattern and color .Declaration :void far setfillstyle (int pattern , int color);Remarks :Setfillstyle set the current fill pattern and fill colorTo set a user defined fill pattern , do not give a pattern of 12 (USER_FILL) to setfillstyle ,instead , call setfillpattern . Page | 14
  16. 16. Return Values : None.If invalid input is passed to the setfillstyle ,graphresult return -11 and the current fillpattern and fill color remain unchanged .17. delayDelay function is used to suspend execution of a program for a particular time.Declaration :- void delay(unsigned int);Here unsigned int is the number of milliseconds ( remember 1 second = 1000 milliseconds). To use delay function in your program you should include the dos.h header file. Page | 15
  17. 17. Programs of AlgorithmsProgram1: Direct Method Of Line DrawingCODING:#include<graphics.h>#include<iostream.h>#include<conio.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");int x1,x2,c,y1,y2,dy,dx,m;cout<<"tttt-----------------------------n";cout<<"tttt DIRECT LINE BY NEHA SHARMAn";cout<<"tttt-----------------------------n";cout<<"enter the coordinatesn";cin>>x1>>y1>>x2>>y2;dy=y2-y1;dx=x2-x1;m=dy/dx;c=y1-m*x1;while(x1<=x2) Page | 16
  18. 18. {if(m<=1){x1=x1+1;y1=m*x1+c;putpixel(x1,y1,WHITE);}else{y1=y1+1;x1=(y1-c)/m;putpixel(x1,y1,WHITE);}}getch();}OUTPUT: Page | 17
  19. 19. Program 2. Digital Differential Analyzer For LineDrawingCODING:#include<graphics.h>#include<iostream.h>#include<conio.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");int x1,x2,c,y1,y2,dy,dx,m;cout<<"ttt------------------n";cout<<"tttDDA By Neha Sharman";cout<<"ttt------------------n";cout<<"enter the coordinatesn";cin>>x1>>y1>>x2>>y2;dy=y2-y1;dx=x2-x1;m=dy/dx;//c=y1-m*x1;while(x1<=x2){if(m<=1){ Page | 18
  20. 20. x1=x1+1;y1=y1+m;putpixel(x1,y1,WHITE);}else{y1=y1+1;x1=x1+(1/m);putpixel(x1,y1,WHITE);}}getch();closegraph();}OUTPUT: Page | 19
  21. 21. Program3: Bresenham Line Drawing AlgorithmCODING:#include<graphics.h>#include<iostream.h>#include<conio.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");int f,fe,fne,x1,x2,y1,y2,dy,dx,m;cout<<"-----------------";cout<<"nBy Neha Sharman";cout<<"-----------------";cout<<"nenter the cordinatesn";cin>>x1>>y1>>x2>>y2;dy=y2-y1;dx=x2-x1;f=(2*dy)-dx;while(x1<=x2){if(f<=0){x1=x1+1;//y1=y1; Page | 20
  22. 22. f=2*dy;putpixel(x1,y1,WHITE);f=f+fe;}else{y1=y1+1;x1=x1+1;f=(2*dy)-(2*dx);putpixel(x1,y1,WHITE);f=f+fne;}}getch();closegraph();}OUTPUT: Page | 21
  23. 23. Program 4: Direct MethodPolynomialMethodCartesian Co-Ordinate Algortihm ForCircle DrawingCODING:#include<conio.h>#include<graphics.h>#include<math.h>#include<iostream.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");int x1,y1,xc,yc,r,x2;cout<<"tttt-----------------------------n";cout<<"tttt DIRECT CIRCLE BY NEHA SHARMAn";cout<<"tttt-----------------------------n";cout<<"enter the x center y center and radiusn";cin>>xc>>yc>>r;x1=0;x2=r/sqrt(2);while(x1<=x2){y1=sqrt(r*r-x1*x1);putpixel(y1+xc,x1+yc,WHITE); Page | 22
  24. 24. putpixel(x1+yc,y1+xc,WHITE);putpixel(x1+yc,-y1+xc,WHITE);putpixel(-y1+xc,x1+yc,WHITE);putpixel(-y1+xc,-x1+yc,WHITE);putpixel(-x1+yc,-y1+xc,WHITE);putpixel(-x1+yc,y1+xc,WHITE);putpixel(y1+xc,-x1+yc,WHITE);x1=x1+1;}getch();}OUTPUT: Page | 23
  25. 25. Program 5: Polar Circle Drawing AlgorithmCODING:#include<conio.h>#include<graphics.h>#include<math.h>#include<iostream.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");float x1,y1,xc,yc,r,t;cout<<"enter the x center y center and radius";cin>>xc>>yc>>r;x1=0;y1=r;t=1/r;while(x1<=y1){putpixel(y1+xc,x1+yc,WHITE);putpixel(x1+yc,y1+xc,WHITE);putpixel(x1+yc,-y1+xc,WHITE);putpixel(-y1+xc,x1+yc,WHITE);putpixel(-y1+xc,-x1+yc,WHITE);putpixel(-x1+yc,-y1+xc,WHITE); Page | 24
  26. 26. putpixel(-x1+yc,y1+xc,WHITE);putpixel(y1+xc,-x1+yc,WHITE);float temp=y1;y1=(y1*cos(t))+(x1*sin(t));x1=(x1*cos(t))-(temp*sin(t));}getch();}OUTPUT: Page | 25
  27. 27. Program 6: Bresenham Circle DrawingAlgorithmCODING:#include<conio.h>#include<graphics.h>#include<math.h>#include<iostream.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");float x1,y1,f,xc,yc,r,t;cout<<"enter the x center y center and radius";cin>>xc>>yc>>r;x1=0;y1=r;f=3-(2*r);while(x1<=y1){if(f<=0){f=f+6+4*x;x=x+1;} Page | 26
  28. 28. else{f=f+10+(4*x)-(4*y);x=x+1;y=y-1;}putpixel(y1+xc,x1+yc,WHITE);putpixel(x1+yc,y1+xc,WHITE);putpixel(x1+yc,-y1+xc,WHITE);putpixel(-y1+xc,x1+yc,WHITE);putpixel(-y1+xc,-x1+yc,WHITE);putpixel(-x1+yc,-y1+xc,WHITE);putpixel(-x1+yc,y1+xc,WHITE);putpixel(y1+xc,-x1+yc,WHITE);}getch();closegraph(); } Page | 27
  29. 29. Program 7: Mid Point Circle Drawing AlgorithmCODING:#include<conio.h>#include<graphics.h>#include<math.h>#include<iostream.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");float x1,y1,f,xc,yc,r;cout<<"MIDPOINT CIRCLEBY NEHA SHARMAn";cout<<"enter the x center y center and radiusn";cin>>xc>>yc>>r;x1=0;y1=r;f=1-r;while(x1<=y1){if(f<=0){f=f+3+2*x1;x1=x1+1;} Page | 28
  30. 30. else{f=f+5+(2*x1)-(2*y1);x1=x1+1;y1=y1-1;}putpixel(y1+xc,x1+yc,WHITE);putpixel(x1+xc,y1+yc,WHITE);putpixel(x1+xc,-y1+yc,WHITE);putpixel(-y1+xc,x1+yc,WHITE);putpixel(-y1+xc,-x1+yc,WHITE);putpixel(-x1+xc,-y1+yc,WHITE);putpixel(-x1+xc,y1+yc,WHITE);putpixel(y1+xc,-x1+yc,WHITE);}getch();closegraph();}OUTPUT: Page | 29
  31. 31. Program 8: DirectPolynomial Algorithm OfEllipse DrawingCODING:#include<conio.h>#include<iostream.h>#include<graphics.h>#include<math.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");float xc,yc,rx,ry,x1,y1,x2;cout<<"DIRECT ELLIPSE BY NEHA SHARMAn";cout<<"enter xc,yc,x radius,y radiusn" ;cin>>xc>>yc>>rx>>ry;x1=0;x2=rx;while(x1<=x2){y1=(ry/rx)*sqrt((rx*rx)-(x1*x1));putpixel(x1+xc,y1+yc,WHITE);putpixel(x1+xc,-y1+yc,WHITE);putpixel(-x1+xc,-y1+yc,WHITE); Page | 30
  32. 32. putpixel(-x1+xc,y1+yc,WHITE);x1=x1+1;}getch();closegraph();}OUTPUT: Page | 31
  33. 33. Program 9: Polar Ellipse Drawing AlgorithmCODING:#include<conio.h>#include<iostream.h>#include<graphics.h>#include<math.h>void main(){int gd=DETECT,gm;initgraph(&gd,&gm,"");float xc,yc,rx,ry,t1,t2,x1,y1,x2;cout<<"POLAR ELLIPSE BY NEHA SHARMAn";cout<<"enter xc,yc,x radius,y radiusn" ;cin>>xc>>yc>>rx>>ry;t1=0;t2=90;while(t1<=t2){y1=ry*sin(t1);x1=rx*cos(t1);putpixel(x1+xc,y1+yc,WHITE);putpixel(x1+xc,-y1+yc,WHITE);putpixel(-x1+xc,-y1+yc,WHITE); Page | 32
  34. 34. putpixel(-x1+xc,y1+yc,WHITE);t1=t1+1;}getch();closegraph();}OUTPUT: Page | 33
  35. 35. 10. Program To Draw Ellipse Using MidpointEllipse AlgorithmCoding:#include<iostream.h>#include<conio.h>#include<graphics.h>#include<math.h>void main(){int gd=DETECT, gm;initgraph(&gd,&gm,"");float xc,yc,x,y,xr,yr,f;cout<<”tttt------------------------------------”;cout<<"ntttMID POINT ELLIPSE BY MOHITn";cout<<”ntttt---------------------------------------”;cout<<"nEnter the coordinates xc,yc,xr ,yr";cin>>xc>>yc>>xr>>yr;x=0;y=yr;f=(yr*yr)+(xr*xr)/4-(xr*xr)*yr;while(2*(yr*yr)*x<=2*(xr*xr)*y){if(f<=0){f=f+(3+(2*x))*(yr*yr);x++;putpixel(x+xc,y+yc,WHITE);putpixel(-x+xc,y+yc,WHITE);putpixel(-x+xc,-y+yc,WHITE); Page | 34
  36. 36. putpixel(x+xc,-y+yc,WHITE);}else{f=f+3*(yr*yr)+2*x*(yr*yr)+2*(xr*xr)-2*y*(xr*xr);x++;y--;putpixel(x+xc,y+yc,WHITE);putpixel(-x+xc,y+yc,WHITE);putpixel(-x+xc,-y+yc,WHITE);putpixel(x+xc,-y+yc,WHITE);}}f=(yr*yr)/4-2*(xr*xr)*yr+(xr*xr);while(y>=0){if(f<=0){f=f+(3-(2*y))*(xr*xr);x++;y--;putpixel(x+xc,y+yc,WHITE);putpixel(-x+xc,y+yc,WHITE);putpixel(-x+xc,-y+yc,WHITE);putpixel(x+xc,-y+yc,WHITE);}else{f=f+2*(yr*yr)+2*x*(yr*yr)+3*(xr*xr)-2*y*(xr*xr);y--; Page | 35
  37. 37. putpixel(x+xc,y+yc,WHITE);putpixel(-x+xc,y+yc,WHITE);putpixel(-x+xc,-y+yc,WHITE);putpixel(x+xc,-y+yc,WHITE);}}getch();closegraph();}OUTPUT: Mid Point Ellipse Drawing Algorithm Page | 36