55
CE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 1 of 55 CE-102: Computer Programming & Problem Solving CPPS Chapter No 10 G RAPHICS Computer Engineering Department Sir Syed University of Engineering & Technology Tel: 111-994-994, 34988000-2

CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 1 of 55

C E - 1 0 2 : C o m p u t e r P r o g r a m m i n g & P r o b l e m S o l v i n g

CPPS

Chapter No 10

GRAPHICS

Computer Engineering Department

Sir Syed University of Engineering & Technology

Tel: 111-994-994, 34988000-2

Page 2: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 2 of 55

COURSE INSTRUCTORS

Muzammil Ahmad Khan Assistant Professor Computer Engineering Department Sir Syed University of Engineering & Technology University Road, Karachi. Tel: 111994994, 4988000-2 Ext. 326 Room No: BS- 04 Email Address: [email protected]

Mohammed Kashif Shaikh Assistant Professor Computer Engineering Department Sir Syed University of Engineering & Technology University Road, Karachi. Tel: 111994994, 4988000-2 Ext. 326 Room No: BS- 04 Email Address: [email protected]

http://www.ssuet.edu.pk/courses/ce102/c

Page 3: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 3 of 55

Graphics Program No 1 #include<dos.h> #include<stdio.h> #include<conio.h> #include<graphics.h> int rect (int x1, int y1, int x2, int y2); void main (void) { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice(); rect(100,100,300,200); getch(); sound(1000); delay(20); nosound(); getch(); closegraph(); } int rect (int x1, int y1, int x2, int y2) { setfillstyle(SOLID_FILL,GREEN); bar(x1,y1,x2,y2); setcolor(RED); rectangle(x1,y1,x2,y2); setcolor(GREEN); line(x1,y1,x2,y1); line(x1,y1,x1,y2); }

Page 4: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 4 of 55

Graphics Program No 2 #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> # include<dos.h> void main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "C:\\TC\\BGI"); /* 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 */ } cleardevice(); settextstyle (10,0,5); int loop; for (loop=0; loop<3000; loop++) { putpixel (random (640),random (480),loop); setcolor (loop); outtextxy (60,400,"Hello World"); } getch(); cleardevice(); setcolor(RED); setbkcolor(0); /* line(int x1, int y1, int x2, int y2); linerel(int dx, int dy); */ line(10,10,10,100); linerel(120,20);

Page 5: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 5 of 55

/* rectangle(int left, int top, int right, int bottom); */ rectangle(75,25,175,50); rectangle(25,25,40,40); /* Square */ /* circle(midx, midy, radius); */ circle(200,100,50); /* arc(int x, int y, int stangle, int endangle, int radius);*/ arc(75,125,10,110,50); /* setfillstyle(int pattern, int color); */ /* ellipse(int x, int y, int stangle, int endangle,int xradius, int yradius); */ ellipse(50,140,25,180,25,50); /* fillellipse(int x, int y,int xradius, int yradius); */ fillellipse(100,160,20,40); /* sector(int x, int y, int stangle, int endangle,int xradius, int yradius); */ /* sector draws and fills an elliptical pie slice in the current drawing color, then fills it using the pattern and color defined by setfillstyle */ setfillstyle(7,11); sector(200,220,45,135,100,50); setfillstyle(4,10); /* bar(int left, int top, int right, int bottom); */ bar(500,100,300,50); /* bar3d(int left, int top, int right, int bottom,int depth,

int topflag); */

/* setlinestyle(int linestyle, unsigned upattern, int thickness); */ setlinestyle(2,2,1); bar3d(500,120,300,150,10,1); bar3d(500,180,350,160,1,1); /* only bar */ getch();

Page 6: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 6 of 55

/* pieslice(midx, midy, stangle, endangle, radius); */ setfillstyle(1,10); pieslice(200,275,0,360,50); setfillstyle(2,11); pieslice(280,275,0,90,50); setfillstyle(3,12); pieslice(400,275,0,180,50); /* clean up */ getch(); closegraph(); return 0; }

Page 7: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 7 of 55

Graphics Program No 3 #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> void main(void) { /* request auto detection */ int gdriver = DETECT, gmode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); moveto(100,100); /* settextstyle(int font, int direction, int charsize); */ settextstyle(4,0,3); outtext("Testing"); settextstyle(10,1,3); outtextxy(10,10,"Graphics"); settextstyle(9,0,3); outtextxy(200,100,"TC"); getch(); cleardevice(); /* cleardevice erases the entire graphics screen and moves the CP (current position) to home (0,0). */ settextstyle (10,0,3); int loop; for (loop=0; loop<3000; loop++) { putpixel (random (640),random (480),loop); setcolor (loop); outtextxy (100,200,"Thank U 4 using TC Graphics"); } getch(); /* clean up */ getch(); closegraph(); return 0; }

Page 8: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 8 of 55

Graphics Program No 4 #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> void main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); /* 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 */ } int i,j; settextstyle(TRIPLEX_FONT,HORIZ_DIR,2); setcolor(10); for(i=440;i>=200;i--) { if(kbhit()) break; cleardevice(); moveto(40,i); outtext("WELCOME TO THE PROGRAMMING WORLD"); delay(10); } getch(); /* clean up */ getch(); closegraph(); return 0; }

Page 9: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 9 of 55

Graphics Program No 5 #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include<dos.h> #define LEFT 0 #define TOP 0 #define RIGHT 639 #define BOTTOM 390 #define LINES 200 #define MAXCOLOR 15 void title(void); int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "C:\\TC\\BGI"); /* 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 */ } cleardevice(); settextstyle (10,0,3); int loop; for (loop=0; loop<3000; loop++) { putpixel (random (640),random (480),loop); setcolor (loop); outtextxy (50,250,"Turbo C Programming World"); }

Page 10: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 10 of 55

getch(); cleardevice(); title(); /* clean up */ getch(); closegraph(); return 0; } void title(void) { int driver,mode; int x1,y1,x2,y2,dx1,dy1,dx2,dy2,count=0,color=0; driver=VGA; mode=VGAHI; initgraph(&driver,&mode,"c:\\tc\\bgi"); x1=y1=x2=y2=10; dx1=dy1=2; dx2=dy2=3; while(!kbhit() ) { line(x1,y1,x2,y2); x1+=dx1;y1+=dy1; x2+=dx2;y2+=dy2; if(x1<=LEFT || x1>=RIGHT ) dx1=-dx1; if(y1<=TOP || y1>=BOTTOM ) dy1=-dy1; if(x2<=LEFT || x2>=RIGHT) dx2=-dx2; if(y2<=TOP || y2>=BOTTOM) dy2=-dy2; if (++count>LINES) { setcolor(color); color=(color>=MAXCOLOR) ? 0:++color; count=0; settextstyle(4,0,4); outtextxy(150,440,"Muzammil Ahmad Khan"); } } closegraph(); }

Page 11: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 11 of 55

Graphics Program No 6 #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> void main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); /* 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 */ } setcolor(RED); circle(300,175,150); setfillstyle(SOLID_FILL, BLUE); rectangle(30,30,120,120); getch(); /* wait for a key */ floodfill(300,175,RED); /* fill in bounded region */ getch(); closegraph(); return 0; }

Page 12: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 12 of 55

Graphics Program No 7 /* floodfill(); */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int maxx, maxy; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, "\\tc\\bgi"); /* 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 */ } maxx = getmaxx(); maxy = getmaxy(); setcolor(getmaxcolor()); /* select drawing color */ setfillstyle(SOLID_FILL, getmaxcolor()); /* select fill color */ rectangle(0, 0, maxx, maxy); /* draw a border around the screen */ circle(maxx / 3, maxy /2, 50); /* draw some circles */ circle(maxx / 2, 20, 100); circle(maxx-20, maxy-50, 75); circle(20, maxy-20, 25); getch(); /* wait for a key */ floodfill(2, 2, getmaxcolor()); /* fill in bounded region */ getch(); closegraph(); return 0; }

Page 13: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 13 of 55

/* floodfill ( ); Flood-fills a bounded region Declaration: 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 a "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. */

Page 14: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 14 of 55

Graphics Program No 8 /* drawpoly ( ); */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { int gdriver = DETECT, gmode,errorcode; /* request auto detection */ int maxx, maxy; int poly[10]; /* our polygon array */ initgraph(&gdriver, &gmode, "\\tc\\bgi"); errorcode = graphresult(); /* read result of initialization */ 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 */ } maxx = getmaxx(); maxy = getmaxy(); poly[0] = 20; /* 1st vertext */ poly[1] = maxy / 2; poly[2] = maxx - 20; /* 2nd */ poly[3] = 20; poly[4] = maxx - 50; /* 3rd */ poly[5] = maxy - 20; poly[6] = maxx / 2; /* 4th */ poly[7] = maxy / 2; /* drawpoly doesn't automatically close the polygon, so we close it. */ poly[8] = poly[0]; poly[9] = poly[1]; drawpoly(5, poly); /* draw the polygon */ getch(); closegraph(); return 0; }

Page 15: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 15 of 55

/* drawpoly ( ); fillpoly ( ); drawpoly draws the outline of a polygon fillpoly draws and fills a polygon Declaration: void far drawpoly(int numpoints, int far *polypoints); void far fillpoly(int numpoints, int far *polypoints); Remarks: drawpoly draws a polygon using the current line style and color. fillpoly draws the outline of a polygon using the current line style and color, then fills the polygon using the current fill pattern and fill color. Argument What It Does numpoints Specifies number of points *polypoints Points to a sequence of (numpoints x 2) integers Each pair of integers gives the x and y coordinates of a point on the polygon. To draw a closed figure with N vertices, you must pass N+1 coordinates to drawpoly, where the Nth coordinate == the 0th coordinate. */

Page 16: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 16 of 55

Graphics Program No 9 /* fillpoly ( ); */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { int gdriver = DETECT, gmode, errorcode; int i, maxx, maxy; int poly[8]; /* our polygon array */ initgraph(&gdriver, &gmode, "\\tc\\bgi"); errorcode = graphresult(); /* read result of initialization */ 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 */ } maxx = getmaxx(); maxy = getmaxy(); poly[0] = 20; /* 1st vertex */ poly[1] = maxy / 2; poly[2] = maxx - 20; /* 2nd */ poly[3] = 20; poly[4] = maxx - 50; /* 3rd */ poly[5] = maxy - 20; /* 4th vertex. fillpoly automatically closes the polygon. */ poly[6] = maxx / 2; poly[7] = maxy / 2; for (i=EMPTY_FILL; i<USER_FILL; i++) /* loop through the fill patterns */ { setfillstyle(i, getmaxcolor()); /* set fill pattern */ fillpoly(4, poly); /* draw a filled polygon */ getch(); } closegraph(); return 0; }

Page 17: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 17 of 55

Graphics Program No 10

/* lineto ( ); */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { int gdriver = DETECT, gmode, errorcode; /* request auto detection */ char msg[80]; initgraph(&gdriver, &gmode, "\\tc\\bgi"); /* initialize graphics */ errorcode = graphresult(); /* read result of initialization */ if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } moveto(20, 30); /* move the C.P. to location (20, 30) */ /* create and output a message at (20, 30) */ sprintf(msg, " (%d, %d)", getx(), gety()); outtextxy(20, 30, msg); lineto(100, 100); /* draw a line to (100, 100) */ /* create and output a message at C.P. */ sprintf(msg, " (%d, %d)", getx(), gety()); outtext(msg); getch(); closegraph(); return 0; }

Page 18: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 18 of 55

/* lineto ( ); line draws a line between two specified points linerel draws a line a relative distance from the current position (CP) lineto draws a line from the current position (CP) to (x,y) Declaration: void far line(int x1, int y1, int x2, int y2); void far linerel(int dx, int dy); void far lineto(int x, int y); Remarks: line draws a line from (x1, y1) to (x2, y2) using the current color, line style, and thickness. It does not update the current position (CP). linerel draws a line from the CP to a point that is a relative distance (dx,dy) from the CP, then advances the CP by (dx, dy). lineto draws a line from the CP to (x, y), then moves the CP to (x, y). */

Page 19: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 19 of 55

Graphics Program No 11 /* setlinestyle ( ); */ #include <graphics.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <conio.h> int main(void) { int gdriver = DETECT, gmode, errorcode; /* request auto detection */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); /* initialize graphics */ errorcode = graphresult(); /* read result of initialization */ 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 */ } setlinestyle(DOTTED_LINE,2,1); /* select the line style */ line(10,10,100,100); /* draw a line */ rectangle(100,100,200,200); /* draw a rectangle */ outtextxy(110,80,"Dotted_Line"); setlinestyle(DASHED_LINE,1,3);

line(400,400,300,300); rectangle(300,300,100,250); outtextxy(110,310,"Dashed_Line"); closegraph(); /* clean up */ return 0; }

Page 20: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 20 of 55

/* setlinestyle ( ); Sets the current line style and width or pattern Declaration: void far setlinestyle(int linestyle, unsigned upattern, int thickness); Remarks: setlinestyle sets the style for all lines drawn by line, lineto, rectangle, drawpoly, etc. Return Value: If invalid input is passed to setlinestyle, graphresult returns -11, and the current line style remains unchanged.

Name Value Meaning

SOLID_LINE 0 Solid line DOTTED_LINE 1 Dotted line CENTER_LINE 2 Centered line DASHED_LINE 3 Dashed line USERBIT_LINE 4 User-defined line style */

Page 21: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 21 of 55

Graphics Program No 12 /* settextjustify ( ) ; */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> void xat(int x, int y); /* function prototype */ /* horizontal text justification settings */ char *hjust[] = { "LEFT_TEXT",

"CENTER_TEXT", "RIGHT_TEXT" };

/* vertical text justification settings */

char *vjust[] = { "LEFT_TEXT", "CENTER_TEXT", "RIGHT_TEXT" }; int main(void) { int gdriver = DETECT, gmode, errorcode; int midx, midy, hj, vj; char msg[80]; initgraph(&gdriver, &gmode, "\\tc\\bgi"); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); } midx = getmaxx() / 2; midy = getmaxy() / 2; /* loop through text justifications */ for (hj=LEFT_TEXT; hj<=RIGHT_TEXT; hj++) for (vj=LEFT_TEXT; vj<=RIGHT_TEXT; vj++) { cleardevice(); settextjustify(hj, vj); /* set the text justification */

Page 22: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 22 of 55

/* create a message string */ sprintf(msg, "%s %s", hjust[hj], vjust[vj]); /* create cross hairs on the screen */ xat(midx, midy);

outtextxy(midx, midy, msg); /* output the message */

getch(); } closegraph(); return 0; } void xat(int x, int y) /* draw an "x" at (x, y) */ { line(x-4, y, x+4, y); line(x, y-4, x, y+4); }

Page 23: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 23 of 55

/* settextjustify(); Sets text justification for graphics mode Declaration: 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) and TOP_TEXT (for vertical) The enumeration text_just in GRAPHICS.H provides names for the horiz and vert settings passed to settextjustify. settextjustify affects text written with outtext and can't be used with text- mode and stream functions. Return Value: If invalid input is passed to settextjustify, graphresult returns -11, and the current text justification remains unchanged.

Argument Constant Value Meaning Horiz LEFT_TEXT 0 Left-justify text

CENTER_TEXT 1 Center text RIGHT_TEXT 2 Right-justify text

Vert BOTTOM_TEXT 0 Justify from bottom CENTER_TEXT 1 Center text TOP_TEXT 2 Justify from top In calls to settextjustify, if horiz = LEFT_TEXT and direction = HORIZ_DIR The CP's x component is advanced after a call to outtext(string) by textwidth(string). */

Page 24: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 24 of 55

Graphics Program No 13 /* setusercharsize ( ); */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode, "\\tc\\bgi"); 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 */ } settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); /* select a text style */ moveto(0, getmaxy() / 2); /* move to the text starting position */ outtext("Norm "); /* output some normal text */ setusercharsize(1, 3, 1, 1); /* make the text 1/3 the normal width */ outtext("Short "); setusercharsize(3, 1, 1, 1); /* make the text 3 times normal width */ outtext("Wide"); getch(); closegraph(); return 0; }

Page 25: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 25 of 55

/* setusercharsize(); User-defined character magnification factor for stroked fonts Declaration: void far setusercharsize(int multx, int divx, int multy, int divy); Remarks: setusercharsize gives you finer control over the size of text from stroked fonts used with graphics functions. The values set by setusercharsize are active only if charsize = 0, as set by a previous call to settextstyle. With setusercharsize, you specify factors by which the width and height are scaled. the default width is scaled by multx : divx the default height is scaled by multy : divy. For example, to make text twice as wide and 50% taller than the default, set multx = 2; divx = 1; /* 2:1 multy = 3; divy = 2; /* 3:2 Names for BGI fonts

Name Value Meaning DEFAULT_FONT 0 8x8 bit-mapped font

TRIPLEX_FONT 1 Stroked triplex font SMALL_FONT 2 Stroked small font SANS_SERIF_FONT 3 Stroked sans-serif font GOTHIC_FONT 4 Stroked gothic font */

Page 26: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 26 of 55

Graphics Program No 14 /* putimage ( ); getimage ( ); imagesize( ); */ #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<dos.h> # define X 300 # define Y 250 # define R 50 # define N 8 void main (void) { void *buff[N]; int b, x, j, mode, gdriver = DETECT, gmode; initgraph( &gdriver, &gmode, "\\tc\\bgi" ); b = imagesize( X-R, Y-R, X+R, Y+R ); setcolor(2); for( j = 0 ; j < N ; j++ ) { x = j * R / ( N – 1 ); if( j == 0 ) x = 1; cleardevice( ); ellipse ( X, Y, 0, 360, x, R ); buff[j] = (void*) malloc(b); getimage (X-R, Y-R, X+R, Y+R, buff[j] ); } while ( !kbhit( ) ) { for( j = 0 ; j < N ; j++ ) { putimage (X-R, Y-R, buff[j], COPY_PUT ); delay(20); putimage (X-R, Y-R, buff[j], XOR_PUT ); } for( j = N-1; j > 0 ; j-- ) { putimage ( X-R, Y-R, buff[j], COPY_PUT ); delay(20); putimage ( X-R, Y-R, buff[j], XOR_PUT ); } } closegraph(); }

Page 27: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 27 of 55

Graphics Program No 15 /* putimage ( ); getimage ( ); imagesize( ); */ #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> #define ARROW_SIZE 10 void draw_arrow(int x, int y); int main(void) { void *arrow; int gdriver = DETECT, gmode, errorcode; int x, y, maxx; unsigned int size; initgraph(&gdriver, &gmode, "\\tc\\bgi"); 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 */ } maxx = getmaxx(); x = 0; y = getmaxy() / 2; draw_arrow ( x , y ); /* draw the image to be grabbed */ /* calculate the size of the image */ size = imagesize (x , y-ARROW_SIZE , x+(4*ARROW_SIZE) , y+ARROW_SIZE ); arrow = malloc ( size ); /* allocate memory to hold the image */ /* grab the image */ getimage (x , y-ARROW_SIZE , x+(4*ARROW_SIZE) , y+ARROW_SIZE , arrow ); while (!kbhit ( ) ) /* repeat until a key is pressed */ {

putimage (x , y-ARROW_SIZE , arrow , XOR_PUT ); /* erase old image */

Page 28: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 28 of 55

x += ARROW_SIZE; if ( x >= maxx ) x = 0; /* plot new image */ putimage ( x , y-ARROW_SIZE , arrow , XOR_PUT ); } free ( arrow ); /* clean up */ closegraph(); return 0; } void draw_arrow ( int x , int y ) { moveto (x, y); /* draw an arrow on the screen */ linerel(4*ARROW_SIZE, 0); linerel(-2*ARROW_SIZE, -1*ARROW_SIZE); linerel(0, 2*ARROW_SIZE); linerel(2*ARROW_SIZE, -1*ARROW_SIZE); } /* imagesize ( ); Returns the number of bytes required to store a bit image Declaration: unsigned far imagesize(int left, int top, int right, int bottom); Remarks: imagesize determines the size of the memory area required to store a bit image. Return Value: On success, returns the size of the required memory area in bytes. On error (if the size required for the selected image is >= (64K - 1) bytes), returns 0xFFFF (-1)

Page 29: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 29 of 55

/* getimage ( ); putimge ( ); getimage saves a bit image of the specified region into memory putimage outputs a bit image onto the screen Declaration: void far getimage(int left, int top, int right, int bottom, void far *bitmap); void far putimage(int left, int top, void far *bitmap, int op); Remarks: getimage copies an image from the screen to memory. putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left,top).

Argument What It Is/Does Bitmap Points to the area in memory where the bit image

is stored. The first two words of this area are used for the width and height of the rectangle. The remainder holds the image itself.

Bottom left, top) and (right, bottom) define the Left rectangular screen area from which getimage Right copies the bit image. (left, top) is where Top putimage places the upper left corner of the

stored image. Op Specifies a combination operator that controls

how the color for each destination pixel onscreen is computed, based on the pixel already onscreen and the corresponding source pixel in memory.

The enumeration putimage_ops, defined in GRAPHICS.H, gives names to the putimage combination operators. Operators for putimage

Constant Value Meaning COPY_PUT 0 Copies source bitmap onto screen

XOR_PUT 1 Exclusive ORs source image with that already onscreen OR_PUT 2 Inclusive ORs image with that already onscreen AND_PUT 3 ANDs image with that already onscreen NOT_PUT 4 Copy the inverse of the source */

Page 30: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 30 of 55

Implementation of a Graphical Button Type 1 #include<graphics.h> int x1=100, y1=200, x2=300, y2=100; void button (void); void buttonpress (void); void main (void) { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice(); button(); buttonpress(); delay(500); setcolor(10); settextstyle(1,0,5); outtextxy(100,300,"Have a nice day."); getch(); closegraph(); } void button (void) { setlinestyle(SOLID_LINE,1,1); setfillstyle(SOLID_FILL,3); bar(x1,y1,x2,y2); setcolor(0); rectangle(x1,y1,x2,y2); setcolor(15); line(x1,y1,x2,y1); line(x1,y1,x1,y2); setcolor(10); settextstyle(1,0,2); outtextxy(110,125,"Press the Button"); getch(); } void buttonpress(void) { setcolor(4); setlinestyle(0,1,1); setfillstyle(4,8); /* setlinestyle(SOLID_LINE,1,1); */ bar(x1,y1,x2,y2); setcolor(15); rectangle(x1,y1,x2,y2); setcolor(0); line(x1,y1,x2,y1); line(x1,y1,x1,y2); setcolor(CYAN); settextstyle(1,0,5); outtextxy(125,125,"CPPS-1"); getch(); }

Page 31: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 31 of 55

Implementation of a Graphical Button Type 2 #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void mbody ( void ) ; void makebuttons ( void ) ; void buttonchar ( void ) ; void displayscreen ( void ) ; void press ( int [ ] [ 4 ] ) ; void main() { clrscr ( ); int driver = DETECT, mode ; initgraph ( &driver, &mode, "c:\\tc\\bgi" ); setbkcolor(0); char ch = 'w'; mbody ( ); displayscreen ( ); makebuttons ( ); buttonchar ( ); while ( ch != 27 ) {

int q = 0, num1 = 0 ; int chknum = 1 ; /* chknum used to decide if # is 1st/2nd */ double realno1 ,realno2 ,fact; char scno1[32] , scno2[32], op = 'w'; do { ch = getch ( ); if ( ch == 27 ) op = ch; gotoxy ( 25+q ,8 ); if ( ch == 27 || q > 22 ) break; switch(ch) { case 55 : /* Number 7 */ { sound(350); delay(200); nosound(); printf("%c",ch); int coord[6][4] = { { 170,273,210,303 }, { 170,303,210,303 }, { 210,273,210,303 } }; if ( chknum == 1 )

Page 32: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 32 of 55

scno1[num1]= ch; else scno2[num1]= ch; press( coord ); break; } case 56 : /* Number 8 */ { sound(350); delay(200); nosound(); printf("%c",ch); int coord[6][4] = { { 220,273,260,303 }, { 220,303,260,303 }, { 260,273,260,303 } }; if ( chknum == 1 ) scno1[num1]= ch; else scno2[num1]= ch; press( coord ); break; } case 57 : /* Number 9 */ { sound(350); delay(200); nosound(); printf("%c",ch); int coord[6][4] = { { 270,273,310,303 }, { 270,303,310,303 }, { 310,273,310,303 } }; if ( chknum == 1 ) scno1[num1]= ch; else scno2[num1]= ch; press( coord ); break; } } /* End of Case */ if ( ( ch >= 48 && ch <= 57 ) || ( ch == '.' ) || ( ch == 'm' ) ) { /* displays one digit after another */ q++ ;

Page 33: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 33 of 55

num1++ ; /* increase array element */ } } /* End of Do While */ while(ch != '\r'); if ( op == 27 ) break; ch = getch() ; } /* End of While */ } /* End of Main */ void makebuttons(void) { int x, y ; setcolor(15); x = 1; y = 1; setlinestyle(0,1,1); rectangle(x+170, 273+y, x+210, 303+y ); setfillstyle(1,7); floodfill(175+x, 280+y, 15 ); x = 50; y = 1; setlinestyle(0,1,1); rectangle(x+170, 273+y, x+210, 303+y ); setfillstyle(1,7); floodfill(175+x, 280+y, 15 ); x = 100; y = 1; setlinestyle(0,1,1); rectangle(x+170, 273+y, x+210, 303+y ); setfillstyle(1,7); floodfill(175+x, 280+y, 15 );

Page 34: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 34 of 55

/* Make Button for(y=0; y<25; y+=37) for(x=0; x<150; x+=50) { setlinestyle(0,1,1); rectangle(x+170, 273+y, x+210, 303+y ); setfillstyle(1,7); floodfill(175+x, 280+y, 15 ); } 3D Button Effect setcolor(0); for(y=0; y<25; y+=37) for(x=0; x<150; x+=50) { setlinestyle(0,1,1); line(170+x,303+y, 210+x,303+y); } for(y=0; y<25; y+=37) for(x=0; x<150; x+=50) { setlinestyle(0,1,1); line(210+x,273+y, 210+x,303+y); } */ } void press( int coord[6][4] ) { setcolor(0); setlinestyle(0,1,1); rectangle( coord[0][0],coord[0][1],coord[0][2],coord[0][3] ); setcolor(15); setlinestyle(0,1,1); line( coord[1][0],coord[1][1],coord[1][2],coord[1][3] ); line( coord[2][0],coord[2][1],coord[2][2],coord[2][3] ); delay(200); setcolor(15); setlinestyle(0,1,1); rectangle( coord[0][0],coord[0][1],coord[0][2],coord[0][3] ); setcolor(0); setlinestyle(0,1,1); line( coord[1][0],coord[1][1],coord[1][2],coord[1][3] ); line( coord[2][0],coord[2][1],coord[2][2],coord[2][3] ); }

Page 35: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 35 of 55

void displayscreen(void) { setlinestyle(0,0,1); setcolor(15); rectangle(150,85,330,130); setfillstyle(1,0); floodfill(250, 190, 15); setcolor(10); line(150,85, 330,85); line(150,85, 150,129); } void mbody(void) { setcolor(15); setlinestyle(0,0,3); rectangle(150,330,330,250); } void buttonchar(void) { setcolor(15); settextstyle(8,0,4); outtextxy(183 , 263, "7"); outtextxy(233 , 263, "8"); outtextxy(283 , 263, "9"); }

Page 36: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 36 of 55

Text Mode Graphics Program No 1 #include <stdio.h> #include <conio.h> # include<dos.h> #define LEFT 10 #define TOP 8 #define RIGHT 52 #define BOTTOM 21 #define HEIGHT (BOTTOM – TOP + 1) void main (void) { int loop; clrscr(); window(LEFT,TOP,RIGHT,BOTTOM); textcolor(RED); textbackground(3); for (loop=0; loop<100; loop++) { cputs("Greetings"); delay(200); } clrscr(); gotoxy(20,15); printf("Thank U 4 using it."); getch(); }

Page 37: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 37 of 55

Text Mode Graphics Program No 2 #include <stdio.h> #include <conio.h> #include<dos.h> #define LEFT 40 #define TOP 7 #define RIGHT 75 #define BOTTOM 20 #define DESLEFT 1 #define DESRIGHT 1 #define COLORS 16 void main (void) { int loop; clrscr(); window(LEFT,TOP,RIGHT,BOTTOM); textbackground(10); for (loop=0; loop<99; loop++) { textcolor(loop % COLORS ); cputs("Greetings"); } delay(2000); movetext(LEFT,TOP,RIGHT,BOTTOM,DESLEFT,DESRIGHT); getch(); }

Page 38: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 38 of 55

Text Mode Graphics Program No 3 #include <conio.h> void main(void) { char buffer[512]; /* put some text to the console */ clrscr(); textcolor(4); gotoxy(20, 12); cprintf("This is a test. Press any key to continue ..."); getch(); /* grab screen contents */ gettext(20, 12, 36, 21,buffer); clrscr(); /* put selected characters back to the screen */ gotoxy(20, 12); puttext(20, 12, 36, 21, buffer); getch(); return 0; }

Page 39: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 39 of 55

/* gettext ( ); puttext ( ); gettext copies text from text-mode screen to memory puttext copies text from memory to text-mode screen Declaration: int gettext(int left, int top, int right, int bottom, void*destin); int puttext(int left, int top, int right, int bottom, void*source); Remarks:

gettext stores the contents of an onscreen text rectangle defined by (left, top) and (right, bottom) into the area of memory *destin.

puttext writes the contents of the memory area *source out to the onscreen rectangle defined by (left, top) and (right, bottom). puttext is a text-mode function performing direct video output.

gettext reads the rectangle's contents into memory (and puttext puts the stored contents into the rectangle) SEQUENTIALLY from left to right and top to bottom.

All coordinates are absolute screen coordinates, not window-relative. The upper left corner is (1,1).

Each position onscreen takes 2 bytes of memory.

The first byte is the character in the cell.

The second byte is the cell's video attribute.

The space required for a rectangle w columns wide by h rows high is bytes = (h rows) x (w columns) x 2

Return Value: On success, gettext returns 1 puttext returns a non-zero value

On error, both functions return 0 (for example, if you gave coordinates outside the range of the current screen mode)

*/

Page 40: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 40 of 55

TURBO C GRAPHICS FUNCTIONS Some of the important commands used in the text mode are:

gotoxy (1,1) textcolor (10); textbackground (1); window ( ); delay (1000); sound (100); nosound ( ); movetext ( ); gettext ( ); puttext ( );

Some of the important commands used in the graphical mode are:

initgraph ( ); closegraph ( ); moveto ( ); arc ( ); line ( ) ; linerel ( ); circle ( ); rectangle ( ) ; setlinestyle ( ) ; setcolor (1); setbkcolor (2); ellispe ( ); drawpoly ( ); fillpoly ( ); bar ( ); bar3d ( ); pieslice ( ); setfillstyle ( ); floodfill ( ); putpixel ( ); outtext ( ); outextxy ( ); settextstyle ( ); settextjustify ( ); set usercharsize ( ); getmaxx ( ); getmaxy ( ); imagesize ( ); getimage ( ); putimage ( );

Page 41: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 41 of 55

Implementation of Putpixel #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> # include<dos.h> void main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "C:\\TC\\BGI"); cleardevice(); settextstyle (10,0,5); int loop; for (loop=0; loop<3000; loop++) { putpixel (random (100),random (100),loop); setcolor (loop); outtextxy (60,400,"Hello World"); } putpixel (220,200,10); getch(); closegraph(); }

Page 42: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 42 of 55

Implementation of Floodfill #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <dos.h> void main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics, local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

setcolor(RED); circle(300,175,150); rectangle(30,30,120,120); getch(); /* wait for a key */ setfillstyle(BKSLASH_FILL, BLUE); floodfill(300,175,RED); /* fill in bounded region */ setfillstyle(8,YELLOW); floodfill(75,75,RED); /* fill rectangle with mid points*/ getch(); closegraph(); } /* floodfill ( ); Flood-fills a bounded region Declaration: 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 a "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. */

Page 43: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 43 of 55

Implementation of Putimage and Getimage #include <graphics.h> #include <conio.h> #include <stdlib.h> void main(void) { int driver=VGA, mode=VGAHI; initgraph(&driver,&mode,"\\tc\\bgi"); rectangle(120,120,140,140); floodfill(125,125,15); unsigned int size; char *c; size = imagesize(120,120,140,140); c = (char *) malloc(size); getimage(120,120,140,140,c); getch(); putimage(120,120,c,XOR_PUT); putimage(160,160,c,XOR_PUT); getch(); putimage(160,160,c,1); putimage(240,160,c,1); getch(); } /* Operators for putimage

Constant Value Meaning COPY_PUT 0 Copies source bitmap onto screen

XOR_PUT 1 Exclusive ORs source image with that already onscreen OR_PUT 2 Inclusive ORs image with that already onscreen AND_PUT 3 ANDs image with that already onscreen NOT_PUT 4 Copy the inverse of the source */

Page 44: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 44 of 55

/* imagesize ( ); Returns the number of bytes required to store a bit image Declaration: unsigned far imagesize(int left, int top, int right, int bottom); Remarks: imagesize determines the size of the memory area required to store a bit image. Return Value:

♦ On success, returns the size of the required memory area in bytes.

♦ On error (if the size required for the selected image is >= (64K - 1) bytes), returns 0xFFFF (-1)

/* getimage ( ); putimge ( ); getimage saves a bit image of the specified region into memory putimage outputs a bit image onto the screen Declaration: void far getimage(int left, int top, int right, int bottom, void far *bitmap); void far putimage(int left, int top, void far *bitmap, int op); Remarks: getimage copies an image from the screen to memory. putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at left,top).

Argument What It Is/Does Bitmap Points to the area in memory where the bit image

is stored. The first two words of this area are used for the width and height of the rectangle. The remainder holds the image itself.

Bottom left, top) and (right, bottom) define the Left rectangular screen area from which getimage Right copies the bit image. (left, top) is where Top putimage places the upper left corner of the

stored image. Op Specifies a combination operator that controls

how the color for each destination pixel onscreen is computed, based on the pixel already onscreen and the corresponding source pixel in memory.

Page 45: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 45 of 55

Implementation of Putimage and Getimage ( 2nd Program )

/* putimage ( ); getimage ( ); imagesize( ); */ #include<graphics.h> #include<dos.h> # define X 300 # define Y 250 # define R 50 # define N 8 void main (void) { void *buff[N]; int b, x, j, mode, gdriver = DETECT, gmode; initgraph( &gdriver, &gmode, "\\tc\\bgi" ); b = imagesize( X-R, Y-R, X+R, Y+R ); setcolor(2); for( j = 0 ; j < N ; j++ ) { x = j * R / ( N^1 ); if( j == 0 ) x = 1; cleardevice( ); ellipse ( X, Y, 0, 360, x, R );

buff[j] = (void*) malloc(b); getimage (X-R, Y-R, X+R, Y+R, buff[j] ); } while ( !kbhit( ) ) { for( j = 0 ; j < N ; j++ ) { putimage (X-R, Y-R, buff[j], COPY_PUT ); delay(20); putimage (X-R, Y-R, buff[j], XOR_PUT ); } for( j = N-1; j > 0 ; j-- ) { putimage ( X-R, Y-R, buff[j], COPY_PUT ); delay(20); putimage ( X-R, Y-R, buff[j], XOR_PUT ); } } closegraph(); }

Page 46: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 46 of 55

Implementation of a Revolving Circle #include<math.h> #include<stdlib.h> #include<string.h> void p1 (void); void main(void) { int graphdriver = DETECT, graphmode; initgraph(&graphdriver, &graphmode, "c:\\tc\\bgi"); textcolor(0); clrscr(); settextstyle(7,0,4); settextjustify(1,1); outtextxy(330,100,"Solar System"); setcolor(14); setfillstyle(2,14); pieslice(325,225,0,360,18); p1(); getch(); } void p1 (void) { float x,y,t,r; float i,mx=0,my=0; i = -1.6; while( I <= 25.7 ) { x =(30*cos(i))+325 + mx; y =(30*sin(i))+225 + my; setcolor(4); //diplay p1 setfillstyle(1,4); pieslice(x,y,0,360,6); delay(10); setcolor(0); //remove p1 setfillstyle(1,0); pieslice(x,y,0,360,6); i+=.05; // increament of speed } }

Page 47: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 47 of 55

Implementation of a Revolving Circle ( 2nd Program ) #include <dos.h> #include <math.h> #include <graphics.h> #define MI_PI 360 * 3.1472/180 #define RAD(x) x * 3.1472/180 int gx,gy; void Plot( float x, float y) { if( x >= 0 ) x = 320 + x; if( x < 0 ) x = 320 - abs(x); if( y >= 0 ) y = 240 - y; if( y < 0 ) y = 240 + abs(y); x = ceil(x); y = ceil(y); //putpixel((int)x,(int)y,15); setfillstyle(1,0); setcolor(0); pieslice(gx,gy,0,360,10);

//circle(gx,gy,10); gx = x; gy = y; setfillstyle(1,15); setcolor(15); pieslice(x,y,0,360,10);//circle(x,y,10);

//lineto((int)x,(int)y); } main()

{ int driver = DETECT, mode; initgraph(&driver,&mode,"D:/tc/bgi"); float xa = 100; float ya = 100; float xb= 150; float yb = 150; float x1, y1; float x2, y2; double ang = 0.00f;

Page 48: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 48 of 55

while( !kbhit() ) { setcolor(15); circle(320,240,140); delay(50); x1 = xa*cos( RAD(ang) ) - ya*sin( RAD(ang) ); y1 = xa*sin( RAD(ang) ) + ya*cos( RAD(ang) ); xa = x1; ya = y1; Plot( xa, ya ); /*

x2 = xb*cos( RAD(ang) ) - yb*sin( RAD(ang) ); y2 = xb*sin( RAD(ang) ) + yb*cos( RAD(ang) ); xb = x2; yb = y2; Plot( xb, yb );

*/ if( (ang += 1) > 1 ) ang = 0; } }

Page 49: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 49 of 55

Printing a variable with outtextxy #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "\\tc\\bgi"); /* 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 */ } // itoa(cnt,num,10); // int number = 12345; int aa; char s[25]; scanf("%d",&aa); // aa = 99; itoa(aa,s,10); outtextxy(250,250, s ); getch(); closegraph(); return 0; }

Page 50: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 50 of 55

Input Function in Graphical Mode #include<conio.h> #include<graphics.h> #include<string.h> #include<dos.h> const int TAB_WIDTH = 4; const int C_DELAY = 100; char* intextxy (int x, int y, char* str); void rect (int left, int top, int right, int bottom, int color,

int pattern = SOLID_FILL); int main() { int gd = DETECT, mode; char str[80]; initgraph (&gd, &mode, "c:\\tc\\bgi"); cleardevice(); outtextxy (1, 1, "Name: "); intextxy (1, 10+textheight ("A"), str); outtextxy (1, 100, str); getch(); closegraph(); return 0; } char* intextxy (int x, int y, char* str) { char ch; //Store original x and y values int bX = x; int bY = y; int fX; //Clear str strcpy (str, "");

Page 51: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 51 of 55

//Flash the cursor

while (!kbhit()) { //Display the cursor outtextxy (x, y, "_");

//wait delay (C_DELAY);

//make copy of color int color=getcolor(); setcolor (getbkcolor());

//erase cursor rect (x, y, x+textwidth("_"), y+textheight("_"), getbkcolor());

//wait again

delay (C_DELAY);

//restore color setcolor (color); } while ((ch=getch()) != '\r') //Loop until enter pressed { //Make a string out of ch char st[2] = {ch, '\0'}; if (ch == 0) //If Extended Code { //Empty buffer getch(); //Flash the cursor while (!kbhit()) { outtextxy (x, y, "_"); delay (C_DELAY); int color=getcolor(); setcolor (getbkcolor());

rect (x, y, x+textwidth("_"), y+textheight("_"), getbkcolor());

delay (C_DELAY); setcolor (color); } continue; } else if (ch == '\t') //If Tab { //Give tab spacing x += TAB_WIDTH * textwidth ("A"); //Concatenate st into str strcat (str, st);

Page 52: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 52 of 55

//Flash the cursor while (!kbhit()) { outtextxy (x, y, "_"); delay (C_DELAY); int color=getcolor(); setcolor (getbkcolor()); rect (x, y, x+textwidth("_"), y+textheight("_"), getbkcolor()); delay (C_DELAY); setcolor (color); } continue; } else if (ch == '\b') //If backspace encountered { //Store previous color and set color to bkcolor int color = getcolor(); setcolor (getbkcolor()); if (x <= 1) //if x is crossing the initial screen limit { if (y < bY) { //Flash the cursor while (!kbhit()) { outtextxy (x, y, "_"); delay (C_DELAY); int color=getcolor(); setcolor (getbkcolor()); rect (x, y, x+textwidth("_"), y+textheight("_"), getbkcolor()); delay (C_DELAY); setcolor (color); } continue; //if start of string } else { /*move back one line on the x position previously saved when moving to next line*/ y -= textheight (st); x = fX; } } //draw a rectangle (empty filled) on the character to be erased rect (x-textwidth (st), y, x, y+textheight (st), getbkcolor());

//move back one character x -= textwidth (st);

Page 53: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 53 of 55

//remove character from the string str[strlen (str)-1] = '\0';

//restore original color setting setcolor (color); //Flash the cursor while (!kbhit()) { outtextxy (x, y, "_"); delay (C_DELAY); int color=getcolor(); setcolor (getbkcolor()); rect (x, y, x+textwidth("_"), y+textheight("_"), getbkcolor()); delay (C_DELAY); setcolor (color); } continue; } else { //concatenate to string strcat (str, st); //display newly aquired character outtextxy (x, y, st); } //increase x to equal the new position x += textwidth (st); if (x >= getmaxx() - textwidth (st)) //If x reaches the screen limit { //store final x value (screen limit) fX = x; //change x to 1 (initial value) x = 1; //move to next line y += textheight (st); } //Flash the cursor while (!kbhit()) { outtextxy (x, y, "_"); delay (C_DELAY); int color=getcolor(); setcolor (getbkcolor()); rect (x, y, x+textwidth("_"), y+textheight("_"), getbkcolor());

Page 54: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 54 of 55

delay (C_DELAY); setcolor (color); } } return str; } //Function to draw a filled rectangle void rect (int left, int top, int right, int bottom, int color, int pattern) { int poly[8]={left, top, right, top, right, bottom, left, bottom}; setfillstyle (pattern, color); fillpoly (4, poly); }

Page 55: CE-102: Computer Programming & Problem Solving …1stsemesternotes.yolasite.com/resources/Graphics.pdfCE-102: CPPS ( Chapter: Graphics ) Complied By: Muzammil Ahmad Khan and Muhammad

CE-102: CPPS ( Chapter: Graphics )

Complied By: Muzammil Ahmad Khan and Muhammad Kashif Shaikh Page 55 of 55

TURBO C GRAPHICS FUNCTIONS Some of the important commands used in the text mode are:

gotoxy (1,1) textcolor (10); textbackground (1); window ( ); delay (1000); sound (100); nosound ( ); movetext ( ); gettext ( ); puttext ( );

Some of the important commands used in the graphical mode are:

initgraph ( ); closegraph ( ); moveto ( ); arc ( ); line ( ) ; linerel ( ); circle ( ); rectangle ( ) ; setlinestyle ( ) ; setcolor (1); setbkcolor (2); ellispe ( ); drawpoly ( ); fillpoly ( ); bar ( ); bar3d ( ); pieslice ( ); setfillstyle ( ); floodfill ( ); putpixel ( ); outtext ( ); outextxy ( ); settextstyle ( ); settextjustify ( ); set usercharsize ( ); getmaxx ( ); getmaxy ( ); imagesize ( ); getimage ( ); putimage ( );