Top Banner
Program #1 Implementation of line generation using DDA algorithms #include<stdio.h> #include<graphics.h> #include<math.h> 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,""); dx=abs(x2-x1); dy=abs(y2-y1);
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DDA line

Program #1Implementation of line generation using DDA

algorithms

#include<stdio.h>#include<graphics.h>#include<math.h>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,"");

dx=abs(x2-x1);dy=abs(y2-y1);

if (dx >= dy){length = dx;}else{

Page 2: DDA line

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 */

i = 1; /* Initialise loop counter */while(i <= length) { putpixel(x,y,15); x = x + dx; y = y + dy; i = i + 1; delay(100); /* Delay is purposely inserted to see observe the line drawing process */ }getch();closegraph();

}

Page 3: DDA line

Program #2Implementation of line generation using

Bresenham’s algorithms

#include<stdio.h>#include<graphics.h>#include<math.h>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,"");

dx=abs(x2-x1);dy=abs(y2-y1);

/* Initialise starting point-----------------------------*/x = x1;y = y1;

/* Initialise decision variable-------------------------------- */

e = 2 * dy-dx;

Page 4: DDA line

i = 1; /* Initialise loop counter */

do{putpixel(x,y,15);while(e >= 0) { y = y + 1; e = e - 2 * dx; }x = x + 1;e = e + 2 * dy;i = i + 1;}while( i <= dx);getch();closegraph();

}

Page 5: DDA line

Program #3Implementation of circle generation using Mid-

point method.

#include<stdio.h>#include<graphics.h>#include<math.h>void main(){float p;int i,gd,gm,x,y;int r;

/* initialise graphics------------------------ */detectgraph(&gd,&gm);initgraph(&gd,&gm,"");

/* Read the radius----------------------- */printf("Enter the radius of the circle :");scanf("%d",&r);

x=0;y=r;p = 1.25 - r;do{

putpixel(200+x,200+y,15);putpixel(200+y,200+x,15);putpixel(200+x,200-y,15);putpixel(200+y,200-x,15);putpixel(200-x,200-y,15);putpixel(200-x,200+y,15);putpixel(200-y,200+x,15);putpixel(200-y,200-x,15);

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

Page 6: DDA line

y = y;p = p + 2*x + 2;}else{x= x+1;y= y-1;p = p + 2*(x-y) + 1;}delay(10000);}while(x < y);

getch();closegraph();

}

Page 7: DDA line

Program #4Implementation of circle generation using

Bresenham’s algorithm

#include<stdio.h>#include<graphics.h>#include<math.h>void main(){float d;int gd,gm,x,y;int r;clrscr();

/* Read the radius of the circle---------------------------------- */printf("Enter the radius of a circle :");scanf("%d",&r);

/* Initialise graphics mode------------------------------*/detectgraph(&gd,&gm);initgraph(&gd,&gm,"");

/* Initialise starting points -------------------------------*/

x = 0;y = r;

/* initialise the decision variable ---------------------------------------*/ d = 3 - 2 * r;

do{ putpixel(200+x,200+y,15);

putpixel(200+y,200+x,15);putpixel(200+y,200-x,15);putpixel(200+x,200-y,15);putpixel(200-x,200-y,15);

Page 8: DDA line

putpixel(200-y,200-x,15);putpixel(200-y,200+x,15);putpixel(200-x,200+y,15);if (d <= 0){d = d + 4*x + 6;}else{d = d + 4*(x-y) + 10;y = y - 1;}x = x + 1;delay(1000); /* Delay is purposely inserted to see

observe the line drawing process */}while(x < y);

getch();closegraph();

}

Page 9: DDA line

Program #5Implementation of 2D transformation: Translation, Scaling, Rotation, Mirror Reflection and Shearing

(write a menu driven program)

TRANSLATION#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>void main(){int gd = DETECT, gm ;float wy, wx, x1, y1, x2, y2;int i, thickness;initgraph(&gd, &gm, " ");

/* Read two end points of the line------------------------------------ */printf("Enter the co-ordinates for the line:\n");printf("X1: ");scanf("%f", &x1);printf("Y1: ");scanf("%f", &y1);printf("X2: ");scanf("%f", &x2);printf("Y2: ");scanf("%f", &y2);

/* Enter the thickness of the line------------------------------------*/printf("Enter the required thickness: ");scanf("%d", &thickness);cleardevice();line (x1, y1, x2, y2);if ((y2 - y1) / (x2 - x1) < 1){

Page 10: DDA line

wy = (thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(x2-x1));

for(i = 0; i < wy; i++){line(x1, y1 - i, x2, y2 - i);line(x1, y1 + i, x2, y2 + i);}

}else{

wx = (thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-y1));

for(i = 0; i < wx; i++){line(x1 - i, y1, x2 - i, y2);line(x1 + i, y1, x2 + i, y2);}

}printf("This is the line of thickness %d units.\n", thickness);getch();

}

Page 11: DDA line

SCALING#include <stdio.h>#include <graphics.h># include <conio.h>main(){int gdriver;int gmode,maxx,maxy,i;

int tri [58] [2];int tri1 [58] [2] ={0,8,7,8,0,8,8,6,7,8,8,12,7,8,10,5,8,12,21,12,21,12,31,27,31,27,53,36,53,36,50,30,50,30,54,32,54,32,53,29,53,29,55,30,55,30,53,26,27,13,38,25,

Page 12: DDA line

38,25,53,26,53,26,56,26,56,26,70,35,70,35,66,28,66,28,72,30,72,30,67,26,67,26,74,29,74,29,52,10,52,10,44,7,44,7,52,10,52,10,80,10,80,10,42,0,42,0,36,0,36,0,23,4,23,4,10,5,11,7,11,9};int scale [2][2] ={1,0,0,1};clrscr();

Page 13: DDA line

printf("Enter the x scale factor");scanf("%d",&scale[0][0]);printf("Enter the y scale factor");scanf("%d",&scale[1][1]);

detectgraph(&gdriver,&gmode);if(gdriver < 0){puts("Cannot detect a graphics card");exit(1);}initgraph(&gdriver, &gmode, "..");

cleardevice();

setbkcolor(0);setcolor(15);maxx= getmaxx();maxy=getmaxy();for(i=0;i<66;i++){

tri[i][0] = tri1[i][0]*scale[0][0]+tri1[i][1]*scale[1][0];tri[i][1] = tri1[i][0]*scale[0][1]+tri1[i][1]*scale[1][1];

}for (i=0;i<57;){

line(maxx/2+tri[i][0],maxy/2-tri[i][1],maxx/2+tri[i+1][0],maxy/2-tri[i+1][1]);

i=i+2;}

getch();closegraph();

}

Page 14: DDA line

ROTATION#include<graphics.h>#include<stdio.h>#include<math.h>#include<dos.h> int gdriver,gmode,j,k,sum,mult;

int pi[8]; double b[3][3]={1,0,0,

0,1,0, 0,0,1};

int c[1][1]; float a[1][1]; int x=0,y=0; void matmul(float[8]); main() { int angle; float p1[10]= {100,200,

130,200,130,270,100,270,100,200};

float p2[10]= {100,200,120,180,150,180,130,200,100,200};

float p3[10]= {130,200,150,180,150,250,130,270,130,200};

detectgraph(&gdriver,&gmode); initgraph(&gdriver,&gmode," "); setcolor(4); matmul(p1); setfillstyle(1,4);

Page 15: DDA line

fillpoly(4,pi); matmul(p2); setfillstyle(1,1); fillpoly(4,pi); matmul(p3); setfillstyle(1,15); fillpoly(4,pi); getch(); for(angle=0;angle<360;angle++) { setcolor(0); matmul(p1); setfillstyle(1,0); fillpoly(4,pi); matmul(p2); setfillstyle(1,0); fillpoly(4,pi); matmul(p3); setfillstyle(1,0); fillpoly(4,pi); b[0][0] =cos(angle*3.142/180); b[0][1] =sin(angle*3.142/180); b[1][0] =-sin(angle*3.142/180); b[1][1] =cos(angle*3.142/180); b[1][2] = 0; b[2][0] =-x*cos(angle*3.142/180)+y*sin(angle*3.142/180)+x; b[2][1] =-x*sin(angle*3.142/180)+y*cos(angle*3.142/180)+y; b[2][2] = 0; matmul(p1); setfillstyle(1,4); fillpoly(4,pi); matmul(p2); setfillstyle(1,1); fillpoly(4,pi); matmul(p3); setfillstyle(1,15);

Page 16: DDA line

fillpoly(4,pi);

delay(100); }

for(angle=360;angle>=0;angle--) { setcolor(0); matmul(p1); setfillstyle(1,0); fillpoly(4,pi); matmul(p2); setfillstyle(1,0); fillpoly(4,pi); matmul(p3); setfillstyle(1,0); fillpoly(4,pi); 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] =-x*cos(angle*3.142/180)+y*sin(angle*3.142/180)+x; b[2][1] =-x*sin(angle*3.142/180)+y*cos(angle*3.142/180)+y; b[2][2] = 0; matmul(p1); setfillstyle(1,4); fillpoly(4,pi); matmul(p2); setfillstyle(1,1); fillpoly(4,pi); matmul(p3); setfillstyle(1,15); fillpoly(4,pi); delay(100); }

Page 17: DDA line

getch(); closegraph(); }

void matmul(float p[10]) { int i; for(i=0;i<9;i=i+2) { a[0][0]=p[i];

a[0][1]=p[i+1];c[0][0] = a[0][0]*b[0][0]+a[0][1]*b[1][0]+b[2]

[0]+320;c[0][1] = a[0][0]*b[0][1]+a[0][1]*b[1][1]+b[2]

[1]+240;pi[i]=c[0][0];pi[i+1]=c[0][1];

} }

Page 18: DDA line

Program #6Implementation of Line Clipping using Cohen-

Sutherland algorithm

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#include<math.h>#include<graphics.h>/* Defining structure for end point of line */typedef struct coordinate{int x,y;char code[4];}PT;void drawwindow();void drawline (PT p1,PT p2,int cl);PT setcode(PT p);int visibility (PT p1,PT p2);PT resetendpt (PT p1,PT p2);main(){

int gd=DETECT, gm,v;PT p1,p2,ptemp;initgraph(&gd,&gm," ");cleardevice();printf("\n\n\t\tENTER END-POINT 1 (x,y): ");scanf("%d,%d",&p1.x,&p1.y);printf("\n\n\t\tENTER END-POINT 2 (x,y): ");scanf("%d,%d",&p2.x,&p2.y);cleardevice();drawwindow();getch();drawline(p1,p2,15);getch();p1=setcode(p1);p2=setcode(p2);

Page 19: DDA line

v=visibility(p1,p2);switch(v)

{case 0: cleardevice(); /* Line conpletely

visible */drawwindow();drawline(p1,p2,15);break;

case 1: cleardevice(); /* Line completely invisible */

drawwindow();break;

case 2: cleardevice(); /* line partly visible */p1=resetendpt (p1,p2);p2=resetendpt(p2,p1);drawwindow();drawline(p1,p2,15);break;

}getch();closegraph();return(0);}/* Function to draw window */void drawwindow(){setcolor(RED);line(150,100,450,100);line(450,100,450,350);line(450,350,150,350);line(150,350,150,100);}/* Function to draw line between two points---------------------------------------------*/void drawline (PT p1,PT p2,int cl){setcolor(cl);line(p1.x,p1.y,p2.x,p2.y);

Page 20: DDA line

}/* Function to set code of the coordinates--------------------------------------------*/PT setcode(PT p){PT ptemp;if(p.y<100)ptemp.code[0]='1'; /* TOP */elseptemp.code[0]='0';if(p.y>350)ptemp.code[1]='1'; /* BOTTOM */elseptemp.code[1]='0';if (p.x>450)ptemp.code[2]='1'; /* RIGHT */elseptemp.code[2]='0';if (p.x<150) /* LEFT */ptemp.code[3]='1';elseptemp.code[3]='0';ptemp.x=p.x;ptemp.y=p.y;return(ptemp);}/* Function to determine visibility of line--------------------------------------------*/int visibility (PT p1,PT p2){int i,flag=0;for(i=0;i<4;i++){if((p1.code[i]!='0')||(p2.code[i]!='0'))flag=1;}if(flag==0)return(0);

Page 21: DDA line

for(i=0;i<4;i++){if((p1.code[i]==p2.code[i]) &&(p1.code[i]=='1'))flag=0;}if(flag==0)return(1);return(2);}

/* Function to find new end points--------------------------------------*/PT resetendpt (PT p1,PT p2){PT temp;int x,y,i;float m,k;if( p1.code[3]=='1') /* Cutting LEFT Edge */x=150;if(p1.code[2]=='1') /* Cutting RIGHT Edge */x=450;if((p1.code[3]=='1')||(p1.code[2]=='1')){m=(float) (p2.y-p1.y)/(p2.x-p1.x);k=(p1.y+(m*(x-p1.x)));temp.y=k;temp.x=x;for(i=0;i<4;i++)temp.code[i]=p1.code[i];if(temp.y<=350&&temp.y>=100)return(temp);}if(p1.code[0]=='1') /* Cutting TOP Edge */y=100;if(p1.code [1]=='1') /* Cutting BOTTOM Edge */y=350;if((p1.code[0]=='1')||(p1.code[1]=='1')){

Page 22: DDA line

m=(float)(p2.y-p1.y)/(p2.x-p1.x);k=(float)p1.x+(float)(y-p1.y)/m;temp.x=k;temp.y=y;for(i=0;i<4;i++)temp.code[i]=p1.code[i];return(temp);}elsereturn(p1);}

Page 23: DDA line

Program #7Implementation of Curve generation using Bezier curves

#include <stdio.h>

#include <graphics.h>

#include <conio.h>

int gd, gm, maxx, maxy;

float xxx[1][2]={0.0,0.0};

void line1(float x2,float y2)

{

int d1,d2,d3,d4;

d1=maxx*xxx[0][0];

d2=maxy*xxx[0][1];

d3=maxx*x2;

d4=maxy*y2;

line(d1,d2,d3,d4);

xxx[0][0]=x2;

xxx[0][1]=y2;

}

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);

Page 24: DDA line

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);

}

}

void igraph()

{

detectgraph(&gd,&gm);

if(gd<0)

{

Page 25: DDA line

puts("CANNOT DETECT A GRAPHICS CARD");

exit(1);

}

initgraph(&gd,&gm,"f:\\tc");

}

main()

{

igraph();

maxx = getmaxx();

maxy = getmaxy();

xxx[0][0] = 0.5;

xxx[0][1] = 0.5;

setcolor(12);

bezier(0.15,0.5,0.25,0.2,0.5,0.5,8);

setfillstyle(1,12);

floodfill(0.45*maxx,0.45*maxy,12);

xxx[0][0] = 0.5;

xxx[0][1] = 0.5;

setcolor(12);

bezier(0.35,0.3,0.6,0,0.5,0.5,4);

setfillstyle(1,12);

floodfill(0.5*maxx,0.45*maxy,12);

xxx[0][0] = 0.5;

xxx[0][1] = 0.5;

setcolor(12);

bezier(0.90,0.45,0.64,0.2,0.5,0.5,4);

Page 26: DDA line

setfillstyle(1,12);

floodfill(0.55*maxx,0.45*maxy,12);

xxx[0][0] = 0.5;

xxx[0][1] = 0.5;

setcolor(12);

bezier(0.15,0.6,0.25,0.87,0.5,0.5,4);

setfillstyle(1,12);

floodfill(0.45*maxx,0.55*maxy,12);

xxx[0][0] = 0.5;

xxx[0][1] = 0.5;

setcolor(12);

bezier(0.85,0.75,0.57,0.8,0.5,0.5,4);

setfillstyle(1,12);

floodfill(0.55*maxx,0.55*maxy,12);

xxx[0][0] = 0.5;

xxx[0][1] = 0.5;

setcolor(2);

bezier(0.5,0.75,0.57,0.8,0.5,1,4);

setcolor(14);

circle(0.5*maxx,0.5*maxy,10);

setfillstyle(11,14);

floodfill(0.5*maxx,0.5*maxy,14);

xxx[0][0] = 0.534;

xxx[0][1] = 0.9;

setcolor(2);

bezier(0.85,0.75,0.57,0.78,0.534,0.9,4);

Page 27: DDA line

setfillstyle(2,2);

floodfill(0.60*maxx,0.85*maxy,2);

getch();

closegraph();

}