CGAAAAAAAAAAAAND

1a]Study and enlist the basic functions used for graphics in 

C/C++/Python language. Give an example for each of them.

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

void main()

{

int gd=DETECT,gm;

initgraph(&gd,&gm, "C:\\TurboC3\\bgi");

line (100,100,200,200);

getch();

closegraph();

}  


1b]Draw a co-ordinate axis at the center of the screen.

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void main() {

 int gd = DETECT, gm;

 int midx, middy;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 cleardevice();

 midx = getmaxx() / 2;

 midy = getmaxy() / 2;

 line(1, midy, getmaxx(), midy);

 line(midx, 1, midx, getmaxy());

 getch();

 closegraph(); }

3]Draw the following basic shapes in the center of the screen:

i) Circle ii) Rectangle iii) Square iv) Concentric Circles v)Ellipsevi) Line

#include <graphics.h>

#include <conio.h>

void main() {

int gd = DETECT,

gm,left=100,top=100,right=200,bottom=200,x=300,y=150,radius=50;

 initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 rectangle(120, 150, 230, 200);

 circle(x,y,radius);

 bar(left +300 top , right +300, bottom);

 line(left + 10, top + 150, left + 410, top + 150);

 ellipse(x ,y+ 200, 0, 360, 80, 50);

 for (radius = 25; radius <= 100; radius += 20) {

 circle(500, 350, radius);

 }getch(); closegraph();}

4a]Develop the program for DDA Line drawing algorithm.

#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include<dos.h>

#include<conio.h>

void main() {

 float x,y,x1, y1, x2, y2, dx, dy, pixel;

 int i,gd,gm;

 printf("Enter the value of x1: ");

 scanf("%f", &x1);

 printf("Enter the value of y1: ");

 scanf("%f", &y1);

 printf("Enter the value of x2: ");

 scanf("%f", &x2);

 printf("Enter the value of y2: ");

 scanf("%f", &y2);

 detectgraph(&gd,&gm);

 initgraph(&gd,&gm,"c:\\turboc3\\bgi");

 dx = abs(x2 - x1);

 dy = abs(y2 - y1);

 if (dx >= dy)

pixel = dx

else

 pixel = dy;

 dx=dx/pixel;

 dy=dy/pixel;

 x = x1; y = y1; i = 1;

 while (i <= pixel) {

putpixel(x,y,1);

x = x + dx;y = y + dy;i = i + 1;

delay(100);

}getch();

 closegraph();

}

4B]Develop the program for Bresenham's Line drawing algorithm.

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void main() {

 int gd = DETECT, gm;

 int dx, dy, x, y, p, x1, y1, x2, y2; clrscr();

 printf("\n\n\tEnter the co-ordinates of first point: ");

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

 printf("\n\tEnter the co-ordinates of second point: ");

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

 detectgraph(&gd, &gm);

 initgraph(&gd, &gm, "C:\\Turboc3\\BGI");

 dx = x2 - x1;

 dy = y2 - y1;

 p = 2 * dy - dx;

 x = x1;

 y = y1;

 while (x <= x2) {

 putpixel(x, y, WHITE);

 x++;

 if (p < 0) {

 p = p + 2 * dy;

 } else {

 y++;p = p + 2 * (dy - dx); } }

 getch();

 closegraph();

}

5A]Develop the program for the mid-point circle drawing algorithm.

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

void pixel(int xc, int yc, int x, int y);

void main() {

 int gd = DETECT, gm,xc, yc, r, x, y, Pk;

 clrscr();

 initgraph(&gd, &gm, "C:\\turboc3\\bgi");

 printf("* Bresenham's Midpoint algorithm of circle *\n");

 printf("Enter the value of Xc\t");

 scanf("%d", &xc);

 printf("Enter the value of Yc\t");

 scanf("%d", &yc);

 printf("Enter the Radius of circle\t");

 scanf("%d", &r);

 x = 0;

 y = r;

 Pk = 1 - r;

 pixel(xc, yc, x, y);

 while (x < y) {

 if (Pk < 0) {x = x + 1;

 Pk = Pk + (2 * x) + 1;

 } else {

 x = x + 1; y = y - 1;}pixel(xc, yc, x, y);

 } getch();

 closegraph();}

void pixel(int xc, int yc, int x, int y) {

 putpixel(xc + x, yc + y, 7);

 putpixel(xc + y, yc + x, 7);

 putpixel(xc - y, yc + x, 7);

 putpixel(xc - x, yc + y, 7);

 putpixel(xc - x, yc - y, 7);

 putpixel(xc - y, yc - x, 7);

 putpixel(xc + y, yc - x, 7); putpixel(xc + x, yc - y, 7);}

5B]Develop the program for the mid-point ellipse drawing

algorithm.

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <math.h>

void disp();

float x, y;int xc, yc;

void main() {

 int gd = DETECT, gm;int a, b; float p1, p2;

 clrscr();

 initgraph(&gd, &gm, "C:\\turboc3\\bgi");

 printf("Enter xc:\t");scanf("%d", &xc);

 printf("Enter yc:\t "); scanf("%d", &yc);

 printf("Enter a:\t ");scanf("%d", &a);

 printf("Enter b:\t ");scanf("%d", &b);

 x = 0;y = b; disp();

 p1 = (b * b) - (a * a * b) + (a * a)/4;

 while ((2.0 * b * b * x) <= (2.0 * a * a * y)) { x++;

 if (p1 < 0)

 p1 = p1 + (2.0 * b * b * x) + (b * b);

 else {

 y--;

 p1 = p1 + (2.0 * b * b * x) +(b*b)- (2.0 * a * a * y) ;

 }

 disp();x=-x;disp();x=-x;}x=a;y=0;disp()

 p2 = (a * a) +2.0*(b * b *a)+( b * b)/4;

 while ((2,0*b*b*x)>(2.0*a*a*y)) {

 y++;

 if (p2 > 0)

 p2 = p2+ (a * a)-(2.0 * a * a * y) ;

 else {

 x--;

 p2 = p2 + (2.0 * b * b * x) - (2.0 * a * a * y) + (a * a);

 }

 disp();y=-y; disp(); y=-y;

 }getch();

 closegraph();

}void disp() {

 putpixel(xc + x, yc + y, 10);

 putpixel(xc - x, yc + y, 10);

 putpixel(xc + x, yc - y, 10);

 putpixel(xc + x, yc - y, 10);}

6A]Write a program to implement 2D scaling.

#include <graphics.h>

#include <conio.h>

#include <iostream.h>

void main() {

 int x1, y1, x2, y2, x3, y3, x4, y4, tx, ty;

 int gd = DETECT, gm; initgraph(&gd, &gm,"c:\\turboC3\\bgi");

 cout << "Enter x1 & y1:";

 cin >> x1 >> y1;

 cout << "Enter x2 & y2:";

 cin >> x2 >> y2;

 setcolor(10);

 line(x1, y1, x2, y2);

 cout << "Enter the transation in x & y direction:"; cin >> tx >> ty;

 x3 = x1 + tx; y3 = y1 + ty;

 x4 = x2 + tx; y4 = y2 + ty;

 line(x3, y3, x4, y4);

 getch(); closegraph() }

6B] Write a program to Perform 2D translation.

#include <graphics.h>

#include <iostream.h>

#include<conio.h>

int main() {

 int x1, y1, x2, y2, tx, ty;

 int gd = DETECT, gm; initgraph(&gd, &gm, "C:\\TurboC3\\BGI");

 cout << "Enter the 2 line end points (x1, y1, x2, y2): ";

 cin >> x1 >> y1 >> x2 >> y2;

 line(x1, y1, x2, y2);

 cout << "Enter translation coordinates (tx, ty): ";

 cin >> tx >> ty;

 x1 += tx;

 y1 += ty;

 x2 += tx;

 y2 += ty;

 cout << "Line after translation" << endl;

 line(x1, y1, x2, y2);

 getch(); closegraph(); return 0;}

7A]Perform 2D Rotation on a given object.

#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <conio.h>

#define PI 3.14

void main()

{

 int gd = DETECT, gm;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 int x1, y1, x2, y2;

 float x1new, y1new, x2new, y2new;

 float r = 80;

 float theta;

 printf("Enter the co-ordinate of line: ");

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

 printf("Enter the radius: ");

 scanf("%d", &r);

 theta = r*PI / 180;

 x1new = x1;

 y1new = y1;

 x1new = abs(x1 * cos(theta) - sin(theta));

 y1new = abs(x1 * sin(theta) - cos(theta));

 x2new = abs(x2 * cos(theta) - y2 * sin(theta));

 y2new = abs(x2 * sin(theta) - y2 * cos(theta));

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

 printf("x1new=%d, y1new=%d, x2new=%d, y2new=%d",

 x1new, y1new, x2new,y2new);

 line(x1, y1, x2, y2);

 line(x1new, y1new, x2new, y2new);

 getch();

}

7B]Program to create a house like figure and perform the following

operations.

i)Scaling about the origin followed by translation.

#include<graphics.h>

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

void main()

{

 int gd=DETECT, gm, x1, y1, x2, y2, sx, sy, tx, ty;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 printf("Enter the coordinates of rectangle: ");

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

 printf("Before Scaling:\n");

 rectangle(x1, y1, x2, y2);

 printf("\n\nEnter scaling factor: ");

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

 x1 = sx * x1;

 x2 = sx * x2;

 y1 = sy * y1;

 y2 = sy * y2;

 printf("\nAfter Scaling:\n");

 rectangle(x1, y1, x2, y2);

 printf("\n\n\nEnter Translating factor: ");

 scanf("%d%d", &tx, &ty);x1 = tx + x1;

 x2 = tx + x2;

 y1 = ty + y1;

 y2 = ty + y2;

 printf("After Translation:\n");

 rectangle(x1, y1, x2, y2);

 getch();

}

ii. Scaling with reference to an arbitrary point.

#include<stdio.h>

#include<graphics.h>

#include<stdlib.h>

#include<conio.h>

void main()

{

 int gd=DETECT, gm;

 int x1=100, x2=150, y1=200, y2=230, sx=2, sy=2;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 printf("Rectangle Before Scaling:");

 rectangle(x1, y1, x2, y2);

 x1 = sx * x1;

 x2 = sx * x2;

 y1 = sy * y1;

 y2 = sy * y2;

 printf("\n\n\n\nRectangle After Scaling:"); rectangle(x1, y1, x2, y2); getch();}

iii. Reflect about the line y = mx + c.

#include<graphics.h>

#include<math.h>

#include<stdio.h>

#include<conio.h>

#define PI 3.14

void reset(int h[][2])

{

int val[9][2] = {{50,50}, {75,50}, {75,75}, {100,75}, {100,50},

 {125,50}, {125,100}, {87,125}, {50,100}};

 int i;

 for(i = 0; i < 9; i++) {

 h[i][0] = val[i][0] - 50;

 h[i][1] = val[i][1] - 50;

 }}

void draw(int h[][2]){

 int i;

 setlinestyle(DOTTED_LINE, 0, 1);

 line(320, 0, 320, 480);

 line(0, 240, 640, 240);

 setlinestyle(SOLID_LINE, 0, 1);

 for(i = 0; i < 8; i++)

 line(320 + h[i][0], 240 - h[i][1], 320 + h[i+1][0], 240 - h[i+1][1]);

 line(320 + h[0][0], 240 - h[0][1], 320 + h[8][0], 240 - h[8][1]);}

void rotate(int h[][2], float angle){

int i;

 for(i = 0; i < 9; i++) {

 int xnew, ynew;

 xnew = h[i][0] * cos(angle) - h[i][1] * sin(angle);

 ynew = h[i][0] * sin(angle) + h[i][1] * cos(angle);

 h[i][0] = xnew;

 h[i][1] = ynew; }}

void reflect(int h[][2], int m, int c){

 int i; float angle;

 for(i = 0; i < 9; i++)

 h[i][1] = -h[i][1] - c;

 angle = -PI / 2 - atan(m);

 rotate(h, angle);

 for(i = 0; i < 9; i++)

 h[i][0] = -h[i][0];

 angle = -angle;

 rotate(h, angle);

 for(i = 0; i < 9; i++)

 h[i][1] += c;}

void main()

{

 int h[9][2], m, c;

 int gd = DETECT, gm;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 printf("Enter the values of m and c: ");

 scanf("%d%d", &m, &c);

reset(h);

 draw(h);

 getch();

 reflect(h, m, c);

cleardevice();

draw(h);

getch();

closegraph();}

8A] Write a program to implement Cohen-Sutherland clipping.

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

void main() {

 int gd=DETECT,gm;

 float i,xmax,ymax,xmin,ymin,x1,x2,y1,y2,m,start[4],end[4],code[4];

 clrscr(); initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

 printf("\n Enter bottom left coordinate of viewport: ");

scanf("%f%f",&xmin,&ymin);

 printf("\n Enter top right coordinate of viewport: ");

scanf("%f%f",&xmax,&ymax);

 printf("\nEnter start point coordinates: "); scanf("%f%f",&x1,&y1);

 printf("\nEnter end point coordinates: "); scanf("%f%f",&x2,&y2);


 for(i=0;i<4;i++) {

start[i]=0;end[i]=0;}


 m=(y2-y1)/(x2-x1);

 if(x1<xmin) start[0]=1; if(x1>xmax) start[1]=1;

 if(y1>ymax) start[2]=1; if(y1<ymin) start[3]=1;

 if(x2<xmin) start[0]=1; if(x2>xmax) start[1]=1;

if(y2>ymax) start[2]=1; if(y2<ymin) start[3]=1;

 for(i=0;i<4;i++){

code[i]=start[i]&&end[i];

 if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0)) {

if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&

&(end[1]==0)&&(end[2]==0)&&end[3]==0) {

 cleardevice(); printf("\nLine is totally visible, not a clipping candidate");

 rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch();

 } else {

 cleardevice(); printf("\nLine is partially visible");

 rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2);

 if((start[2]==0)&&(start[3]==1)) { x1=x1+ (ymin-y1)/m; y2=ymin; }

 if((end[2]==0)&&end[3]==1)) {x2= x2+(ymin-y2)/m; y2=ymin; }

 if((start[2]==1)&&(start[3]==0)) {x1= x1+(ymax-y2)/m; y2=ymax; }

 if((end[2]==1)&&(end[3]==0) { x2=x2+(ymax-y2)/m; y2=ymax; }

 if((start[1]==0)&&(start[0]==1)) { y1=y1+ m*(xmin-x1); x1=xmin; }

 if((end[1]==0)&&(end[0]==1)) {y2= y2+ m*(xmin-x2); x2=xmin; }

 if((start[1]==0)&&(start[0]==0) { y1=y1+m*(xmax-x1); x1=xmax; }

 if((end[1]==1)&&(end[0]==0)) { y2=y2+ m*(xmax-x2); x2=xmax; }

 clrscr(); cleardevice(); printf("\nAfter clipping:");

 rectangle(xmin,ymin,xmax,ymax); line(x1,y1,x2,y2); getch();

 }

 } else {

 clrscr(); cleardevice(); printf("\nLine is invisible");

rectangle(xmin,ymin,xmax,ymax);

 }

 getch(); closegraph();

}

8B] Write a program to implement Liang - Barsky Line Clipping

Algorithm.

#include <graphics.h>

#include <conio.h>

#include <stdio.h>

void liangBarskyClip(float x1, float y1, float x2, float y2, float xmin, float ymin,

float xmax, float ymax) {

 float dx = x2 - x1, dy = y2 - y1, p[4], q[4], t1 = 0, t2 = 1;

 p[0] = -dx; q[0] = x1 - xmin; p[1] = dx; q[1] = xmax - x1;

 p[2] = -dy; q[2] = y1 - ymin; p[3] = dy; q[3] = ymax - y1;

 for (int i = 0; i < 4; i++) {

 if (p[i] == 0 && q[i] < 0) return;

 float t = q[i] / p[i];

 if (p[i] < 0) { if (t > t1) t1 = t; }

 else if (p[i] > 0) { if (t < t2) t2 = t; }

 }

 if (t1 > t2) return;

 float xx1 = x1 + t1 * dx, yy1 = y1 + t1 * dy, xx2 = x1 + t2 * dx, yy2 = y1 + t2 *

dy;

 setcolor(RED); line(xx1, yy1, xx2, yy2);

}

int main() {

 int gd = DETECT, gm;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 float xmin = 100, ymin = 100, xmax = 250, ymax = 250;

 setcolor(WHITE); rectangle(xmin, ymin, xmax, ymax);float x1 = 50, y1 = 50, x2 = 300, y2 = 300;

 setcolor(BLUE); line(x1, y1, x2, y2);

 liangBarskyClip(x1, y1, x2, y2, xmin, ymin, xmax, ymax);

 getch();

 closegraph();

 return 0;

}

9A] Write a program to fill a circle using Flood Fill Algorithm.

i)4-Connectors:

#include<graphics.h>#include<conio.h>

#include<stdlib.h>#include<stdio.h>

#include<dos.h>

void flood(int, int,int,int);void main(){

int gd=DETECT,gm;

clrscr();

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("circle filled by flood fill algorithm 4-connector:");

circle(100,200,50);

flood(100,200,8,0);

getch();}

void flood(int x,int y,int fill_col,int old_col){

if((getpixel(x,y)==old_col)){delay(50);putpixel(x,y,fill_col);

flood(x+1,y,fill_col,old_col);flood(x-1,y,fill_col,old_col);

flood(x,y+1,fill_col,old_col);flood(x,y-1,fill_col,old_col);}

ii)8-Connectors:

#include<graphics.h>

#include<conio.h>

#include<stdlib.h>

#include<stdio.h>

#include<dos.h>

void flood(int,int,int,int);

void main(){

int gd=DETECT,gm;clrscr();

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("circle filled by flood fill algorithm 8-connector:");

rectangle(50,100,150,200);flood(54,104,8,0);getch();}

void flood(int x,int y,int fill_col,int old_col){

if((getpixel(x,y)==old_col)){delay(50);

putpixel(x,y,fill_col);

flood(x+1,y,fill_col,old_col);flood(x-1,y,fill_col,old_col);

flood(x,y+1,fill_col,old_col);flood(x,y-1,fill_col,old_col);

flood(x+1,y-1,fill_col,old_col);flood(x-1,y+1,fill_col,old_col);

flood(x-1,y-1,fill_col,old_col);flood(x-1,y+1,fill_col,old_col);}}

9B] Write a program to fill a circle using Boundary Fill Algorithm.

i]4-Connectors:

#include<graphics.h>

#include<conio.h>

#include<stdlib.h>

#include<stdio.h>

#include<dos.h>

void boundaryfill(int x,int y,int fcolor,int bcolor)

{ if(getpixel(x,y)!=bcolor&&getpixel(x,y)!=fcolor)

{ putpixel(x,y,fcolor);

boundaryfill(x+1,y,fcolor,bcolor); boundaryfill(x-1,y,fcolor,bcolor);

boundaryfill(x,y+1,fcolor,bcolor); boundaryfill(x,y-1,fcolor,bcolor);}

}void main(){

int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\turboc3\\bgi");

setcolor(10); rectangle(250,200,310,260);

boundaryfill(260,250,12,10); getch();}

ii]82-Connectors:

#include<graphics.h>

#include<conio.h>

#include<stdlib.h>

#include<stdio.h>

#include<dos.h>

void boundaryfill(int x,int y,int fcolor,int bcolor){

if(getpixel(x,y)!=bcolor&&getpixel(x,y)!=fcolor){

putpixel(x,y,fcolor);

boundaryfill(x+1,y,fcolor,bcolor); boundaryfill(x-1,y,fcolor,bcolor);

boundaryfill(x,y+1,fcolor,bcolor); boundaryfill(x,y-1,fcolor,bcolor);

boundaryfill(x+1,y+1,fcolor,bcolor); boundaryfill(x+1,y-1,fcolor,bcolor);

boundaryfill(x-1,y+1,fcolor,bcolor); boundaryfill(x-1,y-1,fcolor,bcolor);}}

void main(){ int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi"); setcolor(10);

rectangle(250,200,310,260); boundaryfill(280,250,12,10);getch();}

10A]Develop a simple text screen saver using graphics functions.

#include <graphics.h>

#include <stdlib.h>

#include <time.h>

#include <conio.h>

int main() {

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); srand(time(0));

while (!kbhit()) { cleardevice();

setcolor(rand() % 15 + 1);

outtextxy(rand() % getmaxx(), rand() % getmaxy(), "Screen

Server”);

delay(500); }

closegraph();

return 0;}

10B] Perform smiling face animation using graphic functions.

#include <graphics.h>

#include <conio.h>

#include <dos.h>

void drawSmiley(int x, int y) {

 setcolor(YELLOW);

 setfillstyle(SOLID_FILL, YELLOW);

 fillellipse(x, y, 50, 50);

 setcolor(BLACK);

 setfillstyle(SOLID_FILL, BLACK);

 fillellipse(x - 20, y - 20, 5, 5);

 fillellipse(x + 20, y - 20, 5, 5);

 arc(x, y, 200, 340, 30);

}

int main() {

 int gd = DETECT, gm;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 for (int i = 50; i < getmaxx() - 50; i += 10) {

 cleardevice();

 drawSmiley(i, getmaxy() / 2);

 delay(100);

 }

 getch();

 closegraph();

 return 0;}

10C]Draw the moving car on the screen.

#include <graphics.h>

#include <conio.h>

#include <dos.h>

void drawCar(int x, int y)

{

 setcolor(WHITE);

 rectangle(x, y, x + 100, y + 40);

 rectangle(x + 20, y - 20, x + 80, y);

 setfillstyle(SOLID_FILL, BLUE);

 floodfill(x + 1, y + 1, WHITE);

 setcolor(WHITE);

 setfillstyle(SOLID_FILL, BLACK);

 fillellipse(x + 20, y + 40, 10, 10);

 fillellipse(x + 80, y + 40, 10, 10);

}

int main()

{

 int gd = DETECT, gm;

 initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

 int x = 0, y = getmaxy() - 50;

 while (x < getmaxx() - 100)

{

 cleardevice();

drawCar(x, y);

 delay(50);

 x += 10;

 }

 getch();

 closegraph();

 return 0;

}


Comments