Theory : https://pastebin.pl/view/d9b1d994 // EXP 1 To implement DDA Algorithm for drawing a line segment between two given end #include #include #include void main() { float x,y,x1,y1,x2,y2,dx,dy,length; int i,gd,gm; clrscr(); /* Read two end points of line -- -------------------------------- */ printf("Enter the value of x1 :\t"); scanf("%f",&x1); printf("Enter the value of y1 :\t"); scanf("%f",&y1); printf("Enter the value of x2 :\t"); scanf("%f",&x2); printf("Enter the value of y2 :\t"); scanf("%f",&y2); /* Initialise graphics mode ---------------------------------- */ detectgraph(&gd,&gm); initgraph(&gd,&gm,"\\tc\\BGI"); dx=abs(x2-x1); dy=abs(y2-y1); if (dx >= dy) { length = dx; } else { length = dy; } dx = (x2-x1)/length; dy = (y2-y1)/length; x = x1 + 0.5; /* Factor 0.5 is added to round the values */ y = y1 + 0.5; /* Factor 0.5 is added to round the values */ putpixel (x, y, 15); i = 1; /* Initialise loop counter */ while(i <= length) { x = x + dx; y = y + dy;putpixel (x, y, 15); i = i + 1; delay(100); /*Delay is purposely inserted to see observe the line drawing process*/ } getch(); closegraph(); } // To implement Bresenham’s line drawing algorithm for drawing a line segment between two given endpoints A (x1, y2) and B(x2, y2). #include #include #include #include void main() { float x,y,x1,y1,x2,y2,dx,dy,e; int i,gd,gm; clrscr(); /* Read two end points of line -- -------------------------------- */ printf("Enter the value of x1 :\t"); scanf("%f",&x1); printf("Enter the value of y1 :\t"); scanf("%f",&y1); printf("Enter the value of x2 :\t"); scanf("%f",&x2); printf("Enter the value of y2 :\t"); scanf("%f",&y2); /* Initialise graphics mode ---------------------------------- */ detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\BGI"); dx=abs(x2-x1); dy=abs(y2-y1); /* Initialise starting point -----------------------------*/ x = x1; y = y1; putpixel (x, y, 15) ; /* Initialise decision variable -------------------------------- */ e = 2 * dy-dx; i = 1; /* Initialise loop counter */ do { while(e >= 0) { y = y + 1; e = e - 2 * dx; } x = x + 1; e = e + 2 * dy; i = i + 1; putpixel (x, y, 15); }while( i <= dx); getch(); closegraph(); } // To implement area filling algorithm. #include #include #include void flood(int, int, int, int); void main() { int gd,gm; /* Initialise graphics mode---------------------------------- */ detectgraph(&gd,&gm); initgraph(&gd,&gm,"\\tc\\bgi"); rectangle(50,50,100,100) ; flood(55,55,4,15); getch(); closegraph(); } void flood(int seed_x,int seed_y,int foreground_col,int background_col) { if(getpixel(seed_x,seed_y)!= background_col && getpixel(seed_x,seed_y)!= foreground_col) { putpixel(seed_x,seed_y,foreground_col); flood(seed_x+1,seed_y,foreground_col,background_col); flood(seed_x-1,seed_y,foreground_col,background_col); flood(seed_x,seed_y+1,foreground_col,background_col); flood(seed_x,seed_y-1,foreground_col,background_col); flood(seed_x+1,seed_y+1,foreground_col,background_col); flood(seed_x-1,seed_y-1,foreground_col,background_col); flood(seed_x+1,seed_y-1,foreground_col,background_col); flood(seed_x-1,seed_y+1,foreground_col,background_col); } } //To implement Bit Map method for given character generation #include #include #include void main() { int gd=DETECT,gm,i,j; int a[20][20]= {{0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0}, {0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0}, {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0}, {0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0}, {0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0}}; initgraph(&gd,&gm,"c:\\tc\\bgi"); for(i=0;i<19;i++) { for(j=0;j<19;j++) { if(a[i][j]==1) putpixel(100+j,200+i,WHITE); } } getch(); } //: To apply the basic 2D transformations such as translation, Scaling, Rotation for a given 2D object include #include #include #include int main() { int i, x, y, tx, ty, sx, sy, angle=10, xmax, ymax, xmid, ymid, op; int gd,gm; float p1[10]={50,50, 100,50, 100,100, 50,100, 50,50,}; int pi[10]; float b[3][3]={1,0,0, 0,1,0, 0,0,1}; int c[1][1]; float a[1][1]; printf("\nSelect the transformation : "); printf("\n1 : Traslation"); printf("\n2 : Rotation"); printf("\n3 : Scaling"); printf("\n4 : Rotation about arbitrary point"); printf( "\nEnter the option : "); scanf("%d",&op); switch(op) { case 1: printf("\nEnter x traslation : "); scanf("%d",&tx); printf("\nEnter y traslation : "); scanf("%d",&ty); b[0][0] = 1; b[0][1] = 0; b[0][2] = 0; b[1][0] = 0; b[1][1] = 1; b[1][2] = 0; b[2][0] = tx; b[2][1] = ty; b[2][2] = 1; break; case 2: printf("\nEnter Rotation angle : "); scanf("%d",&angle); b[0][0] =cos(angle*3.142/180); b[0][1] =sin(angle*3.142/180); b[0][2] = 0; b[1][0] =-sin(angle*3.142/180); b[1][1] = cos(angle*3.142/180); b[1][2] = 0; b[2][0] = 0; b[2][1] = 0; b[2][2] = 1; break; case 3: printf("\nEnter x scaling : "); scanf("%d",&sx); printf("\nEnter y scaling : "); scanf("%d",&sy); b[0][0] = sx; b[0][1] = 0; b[0][2] = 0; b[1][0] = 0; b[1][1] = sy; b[1][2] = 0; b[2][0] = 0; b[2][1] = 0; b[2][2] = 1; break; case 4: printf("\nEnter x coordinate of arbitrary point : "); scanf("%d",&x); printf("\nEnter y coordinate of arbitrary point : "); scanf("%d",&y); printf("\nEnter Rotation angle : "); scanf("%d",&angle); tx = x; ty = y; b[0][0] =cos(an gle*3.1 42/180) ; b[0][1] =sin(an gle*3.1 42/180) ; b[0][2] = 0; b[1][0] =-sin(angle*3.142/180); b[1][1] = cos(angle*3.142/180); b[1][2] = 0; b[2][0] = -tx* cos(angle*3.142/180) + ty*sin(angle*3.142/180)+tx; b[2][1] = -tx* sin(angle*3.142/180) - ty*cos(angle*3.142/180)+ty; b[2][2] = 1; } detectgraph(&gd,&gm); initgraph(&gd,&gm,"\\tc\\bgi"); // Initialize graphics xmax = getmaxx(); // Get maximum x coordinate ymax = getmaxy(); // Get maximum y coordinate xmid = xmax/2; // Get the center x coordinate ymid = ymax/2; setcolor(1); // Get the center y coordinate line(xmid,0,xmid,ymax); // Draw y coordinate line(0, ymid, xmax, ymid); // Draw x coordinate setcolor(4); for (i=0; i<8;i=i+2) { line(p1[i]+xmid,ymid-p1[i+1],xmid+p1[i+2],ymid-p1[i+3]); } for(i=0;i<9;i=i+2) { a[0][0]=p1[i]; a[0][1]=p1[i+1]; c[0][0] = a[0][0]*b[0][0]+a[0][1]*b[1][0]+b[2][0]; c[0][1] = a[0][0]*b[0][1]+a[0][1]*b[1][1]+b[2][1]; pi[i]=c[0][0]; pi[i+1]=c[0][1]; } setcolor(15); for (i=0; i<8;i=i+2) { line(xmid+pi[i],ymid-pi[i+1],xmid+pi[i+2],ymid-pi[i+3]); } getch(); closegraph(); return 0; } // To implement Bezier curve. #include #include #include #include #include int gd,gm,maxx,maxy; float xxx[4][2]; /* Function to draw line from relative position specified in array xxx-----------------------*/ void line1(float x2,float y2) { line(xxx[0][0],xxx[0][1],x2,y2); xxx[0][0]=x2; xxx[0][1]=y2; } /* Bezier function-------------------- */ void bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n) { float xab,yab,xbc,ybc,xcd,ycd; float xabc,yabc,xbcd,ybcd; float xabcd,yabcd; if (n==0) { line1(xb,yb); line1(xc,yc); line1(xd,yd); } else { xab = (xxx[0][0]+xb)/2; yab = (xxx[0][1]+yb)/2; xbc = (xb+xc)/2; ybc = (yb+yc)/2; xcd = (xc+xd)/2; ycd = (yc+yd)/2; xabc = (xab+xbc)/2; yabc = (yab+ybc)/2; xbcd = (xbc+xcd)/2; ybcd = (ybc+ycd)/2; xabcd = (xabc+xbcd)/2; yabcd = (yabc+ybcd)/2; n=n-1; bezier(xab,yab,xabc,yabc,xabcd,yabcd,n); bezier(xbcd,ybcd,xcd,ycd,xd,yd,n); } } /* Function to initialise graphics----------------------------------- */ void igraph() { detectgraph(&gd,&gm); if(gd<0) { puts("CANNOT DETECT A GRAPHICS CARD"); exit(1); } initgraph(&gd,&gm,"\\tc\\bgi"); } void main() { int i; float temp1,temp2; igraph(); /*Read two end points and two control points of the curve*/ for(i=0;i<4;i++) { printf("Enter (x,y) coordinates of point%d : ",i+1); scanf("%f,%f",&temp1,&temp2); xxx[i][0] = temp1; xxx[i][1] = temp2; } bezier(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1],8); getch(); closegraph(); }