CG Cse 4th File
CG Cse 4th File
CG Cse 4th File
To start the graphic system& yo' m'st !irst call initgraph. Initgraph initializes the graphic system (y loa ing a graphics ri$er !rom is% "or $ali ating a registere ri$er) then p'tting the system into graphics mo e. Initgraph also resets all graphics settings "color& palette& c'rrent position& $ie)port& etc) to their e!a'lts then resets graph. *) G+TPI,+-& P.TPI,+ Getpi/el gets the color o! a speci!ie pi/el. P'tpi/el places a pi/el at a speci!ie point. Decleration .nsigne !ar getpi/el"int /& int y) Voi !ar p'tpi/el"int /& int y& int color) Remar%s Getpi/el gets the color o! the pi/el locate at "/&y)0 P'tpi/el plots a point in the color e!ine at "/& y). Ret'rn $al'e Getpi/el ret'rns the color o! the gi$en pi/el. P'tpi/el oes not ret'rn. 1) 2-34+ GRAPH 4h'ts o)n the graphic system. Decleration Voi !ar closegraph"$oi )0 Remar%s 2lose graph eallocates all memory allocate (y the graphic system. It then restores the screen to the mo e it )as in (e!ore yo' calle initgraph. Ret'rn $al'e None. 5) AR2& 2IR2-+& PI+4-I2+ arc ra)s a circ'lar arc. 2ircle ra)s a circle Pieslice ra)s an !ills a circ'lar pieslice Decleration
Voi !ar arc"int /& int y& int stangle& int en angle& int ra i's)0 Voi !ar circle"int /& int y& int ra i's)0 Voi !ar pieslice"int /& int y& int stangle& int en angle& int ra i's)0 Remar%s Arc ra)s a circ'lar arc in the c'rrent ra)ing color 2ircle ra)s a circle in the c'rrent ra)ing color Pieslice ra)s a pieslice in the c'rrent ra)ing color& then !ills it 'sing the c'rrent !ill pattern an !ill color. 6) +--IP4+& 7I--+-IP4+& 4+2T3R +llipse ra)s an elliptical arc. 7illellipse ra)s an !ills ellipse. 4ector ra)s an !ills an elliptical pie slice. Decleration Voi !ar ellipse"int /& int y& int stangle& int en angle& int /ra i's& int yra i's) Voi !ar !illellipse"int /& int y& int /ra i's& int yra i's) Voi !arsectoe"int /& int y& int stangle& int en angle& int /ra i's& int yra i's) Remar%s +llipse ra)s an elliptical arc in the c'rrent ra)ing color. 7illellipse ra)s an elliptical arc in the c'rrent ra)ing color an than !ills it )ith !ill color an !ill pattern. 4ector ra)s an elliptical pie slice in the c'rrent ra)ing color an than !ills it 'sing the pattern an color e!ine (y set!illstyle or set!illpattern. 8) 7-33D7I- 7loo 9!ills a (o'n e region. Decleration Voi !ar !loo !ill"int /& int y& int (or er) Remar%s 7loo !ills an enclose area on (itmap e$ice. The area (o'n e (y the color (or er is !loo e )ith the c'rrent !ill pattern an !ill color.
"/&y) is a :see point; I! the see is )ithin an enclose area& the insi e )ill (e !ille . I! the see is o'tsi e the enclose area& the e/terior )ill (e !ille . .se !illpoly instea compati(ility )ith !'t're $ersions. 7loo !ill oesnot )or% )ith the I<=9>615 ri$er. Ret'rn $al'e I! an error occ'rs )hile !loo ing a region& graph res'lt ret'rns ?1@. A) G+T23-3R& 4+T23-3R Getcolor ret'rns the c'rrent ra)ing color. 4etcolor ret'rns the c'rrent ra)ing color. Decleration Int !ar getcolor"$oi )0 Voi !ar setcolor"int color) Remar%s Getcolor ret'rns the c'rrent ra)ing color. 4etcolor sets the c'rrent getma/color. eC'i$alent color name. >) -IN+&-IN+R+-&-IN+T3 -ine ra)s a line (et)een t)o speci!ie pints. 3nerel ra)s a line relati$e istance !rom c'rrent position"2P). -inrto ra)s a line !rom the c'rrent position "2P) to"/&y). Decleration Voi !ar lineto"int /& int y) Remar%s -ine ra)s a line !rom "/1& y1) to "/*& y*) 'sing the c'rrent color& line style an thic%ness. It oes not 'p ate the c'rrent position "2P). ra)ing color to color& )hich can range !rom B to o! !loo !ill )here$er possi(le so yo' can maintain co e
To set a ra)ing color )ith setcolor & yo' can pass either the color n'm(er or the
-inerel ra)s a line !rom the 2P to a point that is relati$e istance " /& y) !rom the 2P& then a $ances the 2P (y " /& y). -ineto ra)s a line !rom the 2P to "/& y)& then mo$es the 2P to "/&y). Ret'rn $al'e None D) R+2TANG-+ Dra)s a rectangle in graphics mo e. Decleration Voi !ar rectangle"int le!t& int top& int right& int (ottom) Remar%s It ra)s a rectangle in the c'rrent line style& thic%ness an ra)ing color. "le!t& top) is the 'pper le!t corner o! the rectangle& an "right& (ottom) is its lo)er right corner. Ret'rn $al'e None.
PRACTICAL NO.:-%
AIM: $ami&iari'e (ourse&) *ith creatin+ and storin+ di+ita& ima+es usin+ scanner and di+ita& camera ,compute the si'e o) ima+e *hen stored in di))erent )ormatsand con.ert thestored ima+es )rom one )ormat to another ,/MP0 GI$0 1P!G0 TI$$0 PNG0 etc. - and ana&('e them. The five most common image formats for the web and computer graphics are* +,-., .I/, 0$,, TI// and ,!.. 1. 1P!G +,-. is short for +oint ,hotographic -xperts .roup, and is the most popular among the image formats used on the web. +,-. files are very 1lousy", meaning so much information is lost from the original image when you save it in +,-. file. This is because +,-. discards most of the information to keep the image file size small2 which means some degree of %uality is also lost. If you take a closer look, the +,-. image is not as sharp as the original image. The colors are paler and the lines are less defined and the picture is noisier. If you zoom in there are +,-. artifacts like any other +,-. files. #lmost every digital camera can shoot and save in the +,-. format. +,-. is very web friendly because the file is smaller, which means it takes up less room, and re%uires less time to transfer to a sites. $oreover it is less grainy then .I/, the old king of the internet roost. 3ince )44&, +,-. has been considered the standard. Pros o) 1P!G* (&5bit color, with up to )6 million colors 7ich colors, great for photographs that needs fine attention to color detail $ost used and most widely accepted image format Compatible in most 83 9$ac, ,C, :inux; Cons o) 1P!G* They tend to discard a lot of data #fter compression, +,-. tends to create artifacts Cannot be animated 'oes not support transparency
%. GI$ .I/, short for .raphics Interchange /ormat, is limited to the < bit palette with only (=6 colors. .I/ is still a popular image format on the internet because image size is relatively small compared to other image compression types..I/ compresses images in two ways* first, by reducing the number of colors in rich color images, thus reducing the number of bits per pixel. 3econd, .I/ replaces multiple occurring patterns 9large patterns; into one. 3o instead of storing five kinds of blue, it stores only one blue..I/ is most suitable for graphics, diagrams, cartoons and logos with relatively few colors. .I/ is still the chosen format for animation effects.Compared to +,-., it is lossless and thus more effective with compressing images with a single color, but pales in detailed or dithered pictures. In other words, .I/ is lossless for images with (=6 colors and below. 3o for a full color image, it may lose up to 44.44<> of its colors.8ne edge of the .I/ image format is the interlacing feature, giving the illusion of fast loading graphics. ?hen it loads in a browser, the .I/ first appears to be blurry and fuzzy, but as soon as more data is downloaded, the image becomes more defined until all the date has been downloaded. Pros o) GI$ Can support transparency Can do small animation effects 1:ossless" %uality@they contain the same amount of %uality as the original, except of course it now only has (=6 colors .reat for images with limited colors, or with flat regions of color Cons o) GI$* 8nly supports (=6 colors
It"s the oldest format in the web, having existed since )4<4. It hasn"t been updated since, and sometimes, the file size is larger than ,!..
2. /MP The ?indows 0itmap or 0$, files are image files within the $icrosoft ?indows operating system. In fact, it was at one point one of the few image formats. These files are large and uncompressed, but the images are rich in color, high in %uality, simple and compatible in all ?indows 83 and programs. 0$, files are also called raster or paint images.0$, files are made of millions and millions of dots called 1pixels", with different colors and arrangements to come up with an image or pattern. It might an <5bit, )65bit or (&5bit image. Thus when you make a 0$, image larger or smaller, you are making the individual pixels larger, and thus making the shapes look fuzzy and agged.0$, files are not great and not very popular. 0eing oversized, bitmap files are not what you call 1web friendly", nor are they compatible in all platforms and they do not scale well. Pros o) /MP: ?orks well with most ?indows programs and 83, you can use it as a ?indows wallpaper Cons o) /MP: 'oes not scale or compress well #gain, very huge image files making it not web friendly !o real advantage over other image formats SAMPLE IMAGE OF BMP*
3. PNG ,!. or 9,ortable !etwork .raphics; is a recently introduced format, so not everyone familiar with it. 0ut ,!. has been approved as a standard since )446. It is an image format specifically designed for the web. ,!. is, in all aspects, the superior version of the .I/. +ust like the .I/ format, the ,!. is saved with (=6 colors maximum but it saves the color information more efficiently. It also supports an < bit transparency.,!. was actually created for the intent to replace the .I/ as an image format that doesn"t re%uire a patent license. ,!. can support (& bit 7.0 colorimages, grayscale images, both with and without alpha channels. Pros o) PNG: :ossless, so it does not lose %uality and detail after image compression In a lot ways better then .I/. To start, ,!. often creates smaller file sizes than .I/ 3upports transparency better than .I/ Cons o) PNG: !ot good for large images because they tend to generate a very large file, sometimes creating larger files than +,-.. Anlike .I/ however, it cannot be animated. !ot all web browsers can support ,!.
SAMPLE IMAGE OF PNG COMPARISON OF DIFFERENT FILE FORMATS SHOWING IMAGES BELOW:
PRACTICAL:-2
AIM: - 4rite a pro+ram to Imp&ement 5resenham6s &ine a&+orithm. A&so pro.ide Pro.ision to chan+e attri5utes o) +raph primiti.es such as stipp&in+ ,Dotted and Dashed pattern-0 co&ors BincludeCstdio.hD BincludeCmath.hD BincludeCconio.hD BincludeCgraphics.hD void main9; E int x),x(,y),y(2 int gdF'-T-CT,gm2 void linebres9int,int,int,int;2 printf9Genter the two end points*G;2 scanf9G>d>d>d>dG,Hx),Hx(,Hy),Hy(;2 initgraph9Hgd,Hgm,Gc*IIturbocJIIbgiG;2 cleardevice9;2 linebres9x),y),x(,y(;2 getch9;2 line9x),y),x(,y(;2 getch9;2 closegraph9;2 K void linebres9int x),int y),int x(,int y(; E int dxFabs9x)5x(;,dyFabs9y)5y(;2 int p,x,y,i,xend,yend2 if9dxLFM; E pF(Ndy5dx2 if9x)Dx(; E xFx(2 yFy(2 xendFx)2 K else E xFx)2 yFy)2 xendFx(2 K putpixel9x,y,(;2 for9iFx2iCxend2iOO;
ExOF)2 if9pCM; pOF(Ndy2 else pOF(N9dy5dx;2K putpixel9x,y,(;2K else E pF(Ndx5dy2 if9y)Dy(; E xFx(2 yFy(2 yendFy(2 K putpixel9x,y,(;2 for9iFy2iCyend2iOO; E yOF)2 if9pCM; pOF(Ndx2 else EpOF)2 pOF(N9dx5dy;2 K putpixel9x,y,(;2 K K K
O TP T:-
PRACTICAL:-3
AIM:- 4rite a pro+ram to Imp&ement 5resenhams circ&e a&+orithm. A&so pro.ide to chan+e attri5utes o) +raph primiti.es such as stipp&in+ ,Dotted and Dashed patternand co&ors. B includeCstdio.hD B includeCconio.hD B includeCgraphics.hD B includeCmath.hD void main9; Eint gdF'-T-CT,gm2 int r,x,y,p,xcFJ(M,ycF(&M2 initgraph9Hgd,Hgm,GC*IITA708CJII0.IG;2 cleardevice9;2 printf9G-nter the radius G;2 scanf9G>dG,Hr;2 xFM2 yFr2 putpixel9xcOx,yc5y,);2 pFJ59(Nr;2 for9xFM2xCFy2xOO; E if 9pCM; E yFy2 pF9pO9&Nx;O6;2 K else EyFy5)2 pFpO99&N9x5y;O)M;;2 K putpixel9xcOx,yc5y,);2 putpixel9xc5x,yc5y,(;2 putpixel9xcOx,ycOy,J;2 putpixel9xc5x,ycOy,&;2 putpixel9xcOy,yc5x,=;2 putpixel9xc5y,yc5x,6;2 putpixel9xcOy,ycOx,P;2 putpixel9xc5y,ycOx,<;2 K getch9;2 closegraph9;2 K
O TP T:-
PRACTICAL:-7
AIM:- 4rite a pro+ram to Imp&ement %-D trans)ormation *ith trans&ation0 sca&in+0 and rotation BincludeCstdio.hD BincludeCconio.hD BincludeCgraphics.hD BincludeCmath.hD void translate9;2 void scale9;2 void rotate9;2 void main9; E intch2 intgdF'-T-CT,gm2 initgraph9Hgd,Hgm,Gc*IItcIIbgiG;2 setcolor96;2 outtextxy 9)MM,<<,G8b ect.G;2 rectangle9)MM,)=M,)=M,)MM;2 printf9G555$-!A555G;2 printf9GIn );TranslateIn (;3caleIn J;7otateG;2 printf9GIn-nter your choice* G;2 scanf9G>dG,Hch;2 cleardevice9;2 switch9ch; E case )* translate9;2 break2 case (* scale9;2 break2 case J* rotate9;2 break2 default* printf9Gyou have enterd wrong choiceG;2 break2 K getch9;2 closegraph9;2 K void translate9; E inttx,ty2 setcolor9(;2
outtextxy9(&M,)M,GT7#!3:#TI8!G;2 outtextxy9(J<,(M,G555555555555G;2 printf9GIn-ntertx* G;2 scanf9G>dG,Htx;2 printf9GIn-nterty* G;2 scanf9G>dG,Hty;2 cleardevice9;2 rectangle9)MM,)=M,)=M,)MM;2 printf9GIn#fter TranslationG;2 rectangle9)MMOtx,)=MOty,)=MOtx,)MMOty;2 K void scale9; E intsx,sy2 setcolor9(;2 outtextxy9(&M,)M,G3C#:I!.G;2 outtextxy9(J<,(M,G55555555G;2 printf9GIn-ntersx* G;2 scanf9G>dG,Hsx;2 printf9GIn-ntersy* G;2 scanf9G>dG,Hsy;2 cleardevice9;2 rectangle9)MM,)=M,)=M,)MM;2 printf9GIn#fter 3calingG;2 rectangle9)MMNsx,)=MNsy,)=MNsx,)MMNsy;2 K void rotate9; E float theta2 int x),x(,xJ,x&2 int y),y(,yJ,y&2 int ax),ax(,axJ,ax&,ay),ay(,ayJ,ay&2 intrefx,refy2 printf9GIn-nter the angle for rotation* G;2 scanf9G>fG,Htheta;2 thetaFthetaN9J.)&Q)<M;2 cleardevice9;2 setcolor9(;2 outtextxy9(&M,)M,G78T#T-G;2 outtextxy9(J<,(M,G5555555G;2 refxF)MM2 refyF)MM2 x)F)MM2 y)F)MM2
x(F)=M2 y(F)MM2 xJF)=M2 yJF)=M2 x&F)MM2 y&F)=M2 ax)FrefyO9x)5refx;Ncos9theta;59y)5refy;Nsin9theta;2 ay)FrefyO9x)5refx;Nsin9theta;O9y)5refy;Ncos9theta;2 ax(FrefyO9x(5refx;Ncos9theta;59y(5refy;Nsin9theta;2 ay(FrefyO9x(5refx;Nsin9theta;O9y(5refy;Ncos9theta;2 axJFrefyO9xJ5refx;Ncos9theta;59yJ5refy;Nsin9theta;2 ayJFrefyO9xJ5refx;Nsin9theta;O9yJ5refy;Ncos9theta;2 ax&FrefyO9x&5refx;Ncos9theta;59y&5refy;Nsin9theta;2 ay&FrefyO9x&5refx;Nsin9theta;O9y&5refy;Ncos9theta;2 rectangle9)MM,)=M,)=M,)MM;2 line9ax),ay),ax(,ay(;2 line9ax(,ay(,axJ,ayJ;2 line9axJ,ayJ,ax&,ay&;2 line9ax&,ay&,ax),ay);2 K
O TP T:-
PRACTICAL:-8
AIM:- 4rite a pro+ram to dra* di))erent shapes and )i&& them *ith .arious pattern. BincludeCgraphics.hD BincludeCconio.hD void main9; E intgdF'-T-CT, gm2 int polyR)(SFEJ=M,&=M, J=M,&)M, &JM,&MM, J=M,J=M, JMM,&JM, J=M,&=M K2 initgraph9Hgd, Hgm, Gc*IIturboc*IIbgiG;2 circle9)MM,)MM,=M;2 outtextxy9P=,)PM, GCircleG;2 rectangle9(MM,=M,J=M,)=M;2 outtextxy9(&M, )PM, G7ectangleG;2 ellipse9=MM, )MM,M,J6M, )MM,=M;2 outtextxy9&<M, )PM, G-llipseG;2 line9)MM,(=M,=&M,(=M;2 outtextxy9JMM,(6M,G:ineG;2 sector9)=M, &MM, JM, JMM, )MM,=M;2 outtextxy9)(M, &6M, G3ectorG;2 drawpoly96, poly;2 outtextxy9J&M, &6M, G,olygonG;2 getch9;2 closegraph9;2 K
O TP T:-