Assignment 3: 1) Write A Program To Implement DDA Algorithm
Assignment 3: 1) Write A Program To Implement DDA Algorithm
ASSIGNMENT 3
8
CG ASSIGNMENT
y=y1+0.5*sign(dy);
for(i=1;i<=l;i++)
{
/* u=(int) x;
v=x-u;
if(u>0)
{
if(v>=0.5 || v<=-0.5)
a=u+1;
else
a=u;
}
else
{
if(v>0.5 || v<-0.5)
a=u-1;
else
a=u;
}
u=(int) y;
v=y-u;
if(y>0)
{
if(abs(v)>=0.5)
b=u+1;
else
b=u;
}
else
{
if(v>=0.5 || v<=-0.5)
b=u-1;
else
b=u;
} */
a=round(x);
b=round(y);
9
CG ASSIGNMENT
}
line(x1,y1,x2,y2);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
int round(float f)
{
int a;
float c;
c=fabs(f-(int)f);
if((c <= 0.5 && f>0) || (c >=0.5 && f<0 ))
a=floor(f);
else
a=ceil(f);
return a;
}
10
CG ASSIGNMENT
void main()
{
int gdriver=DETECT,gmode;
int x1,y1,x2,y2,x,y,dx,dy,s1,s2,intch,e,temp,i;
initgraph(&gdriver,&gmode,"../bgi");
printf(" %d \t",i);
printf("Enter rhe co-ordinate of first point : ");
scanf("%d %d",&x1,&y1);
printf("Enter rhe co-ordinate of second point : ");
scanf("%d %d",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
s1=sign(x2-x1);
s2=sign(y2-y1);
x=x1;
y=y1;
if(dy>dx)
{
temp= dx;
11
CG ASSIGNMENT
dx=dy;
dy=temp;
intch=1;
}
else
intch=0;
e=2*dy-dx;
for(i=1;i<=dx;i++)
{
putpixel(x,y,1);
printf(" (%d,%d) ",x,y);
while(e>=0)
{
if(intch==1)
x=x+s1;
else
y=y+s2;
e=e-2*dx;
}
if(intch==1)
y=y+s2;
else
x=x+s1;
e=e+2*dy;
}
getch();
}
ASSIGNMENT 4
12
CG ASSIGNMENT
#include <stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
float wy1,wy2,wy;
void linear(int,int,int,int);
void main()
{
int graphdrive=DETECT,gmode,changx,wx1,wx2;
int x1=0,y1=0,x2=100,y2=100,w=5;
float length;
initgraph(&graphdrive,&gmode,"c:/tc/bgi");
changx=abs(x2-x1);
length=sqrt(pow((x2-x1),2)+pow((y2-y1),2));
wy=((w-1)/2.0)*((float)length/changx);
linear(x1,y1,x2,y2);
setcolor(RED);
line(x1,y1,x2,y2);
getch();
}
13
CG ASSIGNMENT
t=(x2*y1)-(x1*y2);
if (((changx>changy)&&(x1<=x2))||(y1>y2))
{
for(i=1;i<=changx;i++)
{
y1=(-t-(r*x1))/s;
line(x1,y1+wy,x1,y1-wy);
putpixel(x1,y1,RED);
x1++;
}
}
else
{
for(i=1;i<=changy;i++)
{
line(x1,y1+wy,x1,y1-wy);
x1=(-(s*y1)-t)/r;
putpixel(x1,y1,RED);
y1++;
}
}
}
14
CG ASSIGNMENT
ASSIGNMENT 5
15
CG ASSIGNMENT
{
if(a[i]>max)
max=a[i];
}
}
if(x1>max)
x1=max;
x2=min-1;
y2=y1;
line(x1,y1,x2,y2);
for(i=0;i<2*n;i+=2)
{
if(x1!=x2)
m1=(y2-y1)/(x2-x1);
else
m1=1000;
if(a[i]!=a[i+2])
m2=(a[i+3]-a[i+1])/(a[i+2]-a[i]);
else
m2=1000;
if(m1!=m2)
{
b1=y1-(m1*x1);
b2=a[i+1]-(m2*a[i]);
x=(b2-b1)/(m1-m2);
y=(b2*m1-b1*m2)/(m1-m2);
if(x>x2 && x<=x1)
{
if(x==x1 || x==a[i] || x==a[i+2])
{
if(x==a[i] && i==0)
{
if((a[i+3]>a[i+1] && a[2*n-1]>a[i+1]) || (a[i+3]<a[i+1]
&& a[2*n-1]<a[i+1]))
e+=0;
else
e+=1;
}
if( x==a[i+2] && i!=2*n-1 && ((a[i+5]>a[i+3] && a[i-
1]>a[i+3]) || (a[i+5]<a[i+3] && a[i+1]<a[i+3])))
16
CG ASSIGNMENT
e+=0;
if(x==a[i] && i!=0)
{
if ((a[i+3]>a[i+1] && a[i-1]>a[i+1]) || (a[i+3]<a[i+1] &&
a[i-1]<a[i+1]))
e+=0;
else
e+=1;
}
}
else
e+=1;
}
else
e+=0;
}
}
if(e%2==0)
printf("Point is outside polygone....%d",y);
else
printf("Point is inside the polygone........");
printf("\nDo you want to continue (y/n):");
scanf("%s",&ch);
}
getch();
}
17
CG ASSIGNMENT
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
float x1,x2,y1,y2;
float p[50];
float max_x,max_y,min_x,min_y;
int count,num,size,f1,f2;
void intersection(float,float,float,float);
void min_max();
void main()
{
int gm=0,gd=DETECT,i,j,k,cnt=0;
initgraph(&gd,&gm,"c:\\tc\\BGI");
cleardevice();
printf("\nENTER Sides of polygon: ");
scanf("%d",&num);
printf("\nENTER COORDINATES:\n");
for(i=0;i<num*2;i=i+2)
{
cnt++;
printf("X%d = ",cnt);
scanf("%f",&p[i]);
printf("Y%d = ",cnt);
scanf("%f",&p[i+1]);
}
p[i]=p[0];
p[i+1]=p[1];
size=i+1;
cleardevice();
setcolor(BLUE);
for(i=0;i<num*2;i=i+2)
{
line(p[i],p[i+1],p[i+2],p[i+3]);
}
setcolor(YELLOW);
min_max();
18
CG ASSIGNMENT
for(j=min_y+1;j<max_y;j++)
for(i=min_x+1;i<max_x;i++)
{
x1=min_x;y1=j;x2=i;y2=j;
count=0; f1=0; f2=0;
for(k=0;k<num*2;k=k+2)
{
intersection(p[k],p[k+1],p[k+2],p[k+3]);
}
if(count==1)
{
putpixel(x2,y2,YELLOW);
}
}
setcolor(BLUE);
for(i=0;i<num*2;i=i+2)
{
line(p[i],p[i+1],p[i+2],p[i+3]);
}
getch();
}
void intersection(float x3, float y3, float x4,float y4)
{
float m1,m2,b1,b2;
float x,y;
int i;
if((x1==x3&&y1==y3&&x2==x4&&y2==y4)||
(x1==x4&&y1==y4&&x2==x3&&y2==y3))
{
return;
}
if(x1!=x2 && x3!=x4)
{
m1=(y2-y1)/(x2-x1);
b1=y1-(m1*x1);
m2=(y4-y3)/(x4-x3);
b2=y3-(m2*x3);
if(m1==m2)
{
return;
19
CG ASSIGNMENT
}
x=(b2-b1)/(m1-m2);
y=(-(m2*b1)+(m1*b2))/(m1-m2);
}
else
{
if(x1==x2)
x=x1;
else if(x3==x4)
x=x3;
if(y1==y2)
y=y1;
else if(y3==y4)
y=y3;
}
if((x>=x1&&x<=x2&&x>=x3&&x<=x4)||
(x>=x1&&x<=x2&&x>=x4&&x<=x3))
{
if((y>=y1&&y<=y2&&y>=y3&&y<=y4)||
(y>=y1&&y<=y2&&y>=y4&&y<=y3))
{
count++;
for(i=0;i<num*2;i=i+2)
{
if(x==p[i]&&y==p[i+1])
{
if(((f1==0&&y!=min_y)&&(f1==0&&y!
=max_y))||((f1==0&&x!=min_x)&&(f1==0&&x!=max_x)))
{count+=1;f1=1;}
if(f2==0&&((p[i-1]<=y&&p[i+3]<=y)||(p[i-
1]>=y&&p[i+3]>=y)))
{count+=1;f2=1;}
}
}
if(count%2==0)
{
count=0;
}
}
20
CG ASSIGNMENT
}
return;
}
void min_max()
{
int i;
max_x=p[0];
min_x=p[0];
for(i=0;i<num*2;i=i+2)
{
if(p[i]>max_x)
max_x=p[i];
if(p[i]<min_x)
min_x=p[i];
}
max_y=p[1];
min_y=p[1];
for(i=1;i<num*2;i=i+2)
{
if(p[i]>max_y)
max_y=p[i];
if(p[i]<min_y)
min_y=p[i];
}
}
3) Write a program to display file.
/* Draw Polygon and store records in File */
#include<conio.h>
#include<malloc.h>
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
struct node{
int op;
float x;
float y;
struct node * next;
};
void main()
21
CG ASSIGNMENT
{
int gd=DETECT,gm=0;
int op1,n,i;
float x1,y1;
float x=0,y=0;
int ch;
char c;
FILE *fp1,*fp2;
initgraph(&gd,&gm,"C:\\TC\\BGI");
fp1=fopen("disp.txt","w");
fp2=fopen("temp.txt","w");
fclose(fp1);
fclose(fp2);
x=getmaxx()+1;
y=getmaxy()+1;
do
{
printf("\n 1 Input co-ordinate and store in File:");
printf("\n 2 Draw a polygon from record:");
printf("\n 3 Exit:");
printf("\n Enter Your Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter total no of the Normal Screen co-ordinate:");
scanf("%d",&n);
fp1=fopen("disp.txt","a");
for(i=1;i<=n;i++)
{
printf("\nEnter Opcode [ 1(Moveto) / 2(Lineto) ]:");
scanf("%d",&op1);
22
CG ASSIGNMENT
case 2:
cleardevice();
fp1=fopen("disp.txt","r");
// fseek(f,0L,0);
while(feof(fp1)==0)
{
fscanf(fp1,"%d %f %f",&op1,&x1,&y1);
p->op=op1;
p->x=x1*x;
p->y=y1*y;
if(p->op==1)
{
moveto(p->x,p->y);
}
else if(p->op==2)
{
lineto(p->x,p->y);
}
else
{
printf("\n %d Operation is not allowed:",p->op);
}
}
fclose(fp1);
getch();
break;
case 3:
exit(0);
break;
23
CG ASSIGNMENT
}
printf("\nDo you want to Continue (Y/N) ?");
flushall();
scanf("%c",&c);
}while(c=='y');
}
24
CG ASSIGNMENT
ASSIGNMENT 6
25
CG ASSIGNMENT
scanf("%d",&ch);
switch(ch)
{
case 1:
scalling(x,y);
break;
case 2:
rotation(x,y);
break;
case 3:
translation(x,y);
break;
case 4:
exit(0);
break;
default:
printf("\n wrong entry");
}
printf("\n do You want to continue:");
flushall();
scanf("%c",&c);
}while(c=='y'||c=='Y');
getch();
closegraph();
}
void draw_poly(float x[],float y[])
{
int i;
cleardevice();
line((getmaxx()/2),0,(getmaxx()/2),getmaxy());
line(0,(getmaxy()/2),getmaxx(),(getmaxy()/2));
moveto(getmaxx()/2+x[0],getmaxy()/2-y[0]);
for(i=1;i<n;i++)
{
lineto(getmaxx()/2+x[i],getmaxy()/2-y[i]);
}
lineto(getmaxx()/2+x[0],getmaxy()/2-y[0]);
}
26
CG ASSIGNMENT
27
CG ASSIGNMENT
rad=(deg*3.14159)/180;
// printf("\%f",rad);
printf("\nDo you want to rotate the fig :\n 1. clockwise \n 2.counterclockwise");
scanf("%d",&ch);
switch(ch)
{
case 1:
mat[0][0]=cos(rad);
mat[0][1]=-sin(rad);
mat[1][0]=sin(rad);
mat[1][1]=cos(rad);
i=0;
while(i<n)
{
x[i]=x[i]*(float)mat[0][0] + y[i]*(float)mat[1][0];
y[i]=x[i]*(float)mat[0][1] + y[i]*(float)mat[1][1];
i++;
}
draw_poly(x,y);
break;
case 2:
i=0;
mat[0][0]=cos(rad);
mat[0][1]=sin(rad);
mat[1][0]=-sin(rad);
mat[1][1]=cos(rad);
while(i<n)
{
tx=x[i]*mat[0][0]-y[i]*mat[1][0];
ty=x[i]*mat[0][1]+y[i]*mat[1][1];
x[i]=tx;
y[i]=ty;
i++;
}
draw_poly(x,y);
break;
}
28
CG ASSIGNMENT
}
void translation(float x[],float y[])
{
int mat[3][3];
int i=0;
float tx,ty;
float w=1;
printf("Translate Factor");
printf("\nEnter TX:");
scanf("%f",&tx);
printf("\nfEnter TY:");
scanf("%f",&ty);
mat[0][0]=1;
mat[0][1]=0;
mat[0][2]=0;
mat[1][0]=0;
mat[1][1]=1;
mat[1][2]=0;
mat[2][0]=tx;
mat[2][1]=ty;
mat[2][2]=1;
while(i<n)
{
x[i]=(x[i]*mat[0][0]+y[i]*mat[1][0]+w*mat[2][0])/w;
y[i]=(x[i]*mat[0][1]+y[i]*mat[1][1]+w*mat[2][1])/w;
i++;
}
/*while(i<n)
{
printf("\n%f %f",x[i],y[i]);
x[i]=x[i]+tx;
y[i]=y[i]+ty;
printf("\n%f %f",x[i],y[i]);
i++;
getch();
29
CG ASSIGNMENT
} */
draw_poly(x,y);
getch();
}
30
CG ASSIGNMENT
ASSIGNMENT 7
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);
void main()
{
int gd=DETECT,gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
drawwindow();
printf("\nEnter End-Point 1 (x,y): ");
scanf("%d %d",&p1.x,&p1.y);
printf("\nENTER End-Point 2 (x,y): ");
scanf("%d %d",&p2.x,&p2.y);
cleardevice();
drawline(p1,p2,15);
getch();
31
CG ASSIGNMENT
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0: cleardevice();
drawwindow();
drawline(p1,p2,15);
break;
case 1: cleardevice();
drawwindow();
break;
case 2: cleardevice();
p1=resetendpt(p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break;
}
getch();
closegraph();
}
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
32
CG ASSIGNMENT
PT setcode(PT p)
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
33
CG ASSIGNMENT
return(1);
return(2);
}
34
CG ASSIGNMENT
35
CG ASSIGNMENT
ASSIGNMENT 8
#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#define SIZE 10000
int stkx[SIZE],stky[SIZE];
int cntx=-1,cnty=-1;
void push(int x,int y)
{
if(cntx==SIZE-1)
{
getch();
exit(0);
}
stkx[++cntx]=x;
stky[++cnty]=y;
}
int popx()
{
return stkx[cntx--];
}
int popy()
{
return stky[cnty--];
}
void seedfill(int x,int y,int newcol,int boundcolor)
{
int color;
push(x,y);
while(cntx!=-1)
{
x=popx();
36
CG ASSIGNMENT
y=popy();
if(getpixel(x,y)!=newcol)
{
putpixel(x,y,newcol);
}
color=getpixel(x,y);
if((getpixel(x+1,y)!=newcol)&&(getpixel(x+1,y)==boundcolor))
{
push(x+1,y);
} if((getpixel(x,y+1)!
=newcol)&&(getpixel(x,y+1)==boundcolor))
{
push(x,y+1);
}
if((getpixel(x-1,y)!=newcol)&&(getpixel(x-1,y)==boundcolor))
{
push(x-1,y);
}
if((getpixel(x,y-1)!=newcol)&&(getpixel(x,y-1)==boundcolor))
{
push(x,y-1);
}
}
}
main()
{
int gdriver = DETECT, gmode, errorcode;
int seedx=40, seedy=30;
int poly[12];
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
poly[0] = 10;
poly[1] = 10;
poly[2] = 80;
poly[3] = 10;
poly[4] = 80;
poly[5] = 60;
poly[6] = 10;
poly[7] = 60;
poly[8]=10;
poly[9]=10;
37
CG ASSIGNMENT
setcolor(YELLOW);
drawpoly(5,poly);
setfillstyle(SOLID_FILL,RED);
floodfill(20,20,YELLOW);
getch();
seedfill(seedx,seedy,BLUE,RED);
putpixel(seedx,seedy,WHITE);
getch();
}
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#define MAX 10000
int st_x[MAX]={0};
int st_y[MAX]={0};
int top=-1;
int s_x,s_y;
int push(int x,int y)
{
if(top==MAX-1)
return 1;
top++;
st_x[top]=x;
st_y[top]=y;
return 0;
}
void pop(int *x ,int *y)
{
*x=st_x[top];
*y=st_y[top];
top--;
}
void bfconnect(int seedx, int seedy, int bcol, int fcol)
{
int x=seedx;
38
CG ASSIGNMENT
int y=seedy;
if(push(x,y))
return;
while(top>=0)
{
pop(&x,&y);
if(getpixel(x,y)!=fcol)
putpixel(x,y,fcol);
if(getpixel(x+1,y)!=fcol && getpixel(x+1,y)!=bcol)
if(push(x+1,y))break;
if(getpixel(x,y+1)!=fcol && getpixel(x,y+1)!=bcol)
if(push(x,y+1))break;
if(getpixel(x-1,y)!=fcol && getpixel(x-1,y)!=bcol)
if(push(x-1,y)) break;
if(getpixel(x,y-1)!=fcol && getpixel(x,y-1)!=bcol)
if(push(x,y-1))break;
}
}
void main()
{
int gd=DETECT,gm=0;
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(200,200,150,150);
getch();
bfconnect(155,165,WHITE,CYAN);
putpixel(155,165,RED);
getch();
}
#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#define SIZE 10000
int stkx[SIZE],stky[SIZE];
int cntx=-1,cnty=-1;
void push(int x,int y)
39
CG ASSIGNMENT
{
if(cntx==SIZE-1)
{
getch();
exit(0);
}
stkx[++cntx]=x;
stky[++cnty]=y;
}
int popx()
{
return stkx[cntx--];
}
int popy()
{
return stky[cnty--];
}
void seedfill(int x,int y,int newcol,int boundcolor)
{
int color;
push(x,y);
while(cntx!=-1)
{
x=popx();
y=popy();
if(getpixel(x,y)!=newcol)
{
putpixel(x,y,newcol);
}
color=getpixel(x,y);
if((getpixel(x+1,y)!=newcol)&&(getpixel(x+1,y)==boundcolor))
{
push(x+1,y);
}
if((getpixel(x,y+1)!=newcol)&&(getpixel(x,y+1)==boundcolor))
{
push(x,y+1);
}
if((getpixel(x-1,y)!=newcol)&&(getpixel(x-1,y)==boundcolor))
{
40
CG ASSIGNMENT
push(x-1,y);
}
if((getpixel(x,y-1)!=newcol)&&(getpixel(x,y-1)==boundcolor))
{
push(x,y-1);
}
if((getpixel(x+1,y+1)!
=newcol)&&(getpixel(x+1,y+1)== boundcolor))
{
push(x+1,y+1);
}
if((getpixel(x-1,y+1)!=newcol)&&(getpixel(x-1,y+1)==boundcolor))
{
push(x-1,y+1);
}
if((getpixel(x-1,y-1)!=newcol)&&(getpixel(x-1,y-1)==boundcolor))
{
push(x-1,y-1);
}
if((getpixel(x+1,y-1)!=newcol)&&(getpixel(x+1,y-1)==boundcolor))
{
push(x+1,y-1);
}
}
}
main()
{
int gdriver = DETECT, gmode, errorcode;
int seedx=40, seedy=30;
int poly[12];
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
poly[0] = 10;
poly[1] = 10;
poly[2] = 80;
poly[3] = 10;
poly[4] = 80;
poly[5] = 60;
poly[6] = 10;
poly[7] = 60;
poly[8]=10;
41
CG ASSIGNMENT
poly[9]=10;
setcolor(YELLOW);
drawpoly(6,poly);
setfillstyle(SOLID_FILL,RED);
floodfill(20,20,YELLOW);
getch();
seedfill(seedx,seedy,BLUE,RED);
putpixel(seedx,seedy,WHITE);
getch();
}
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#define MAX 10000
int st_x[MAX]={0};
int st_y[MAX]={0};
int top=-1;
int s_x,s_y;
int push(int x,int y)
{
if(top==MAX-1)
return 1;
top++;
st_x[top]=x;
st_y[top]=y;
return 0;
}
void pop(int *x ,int *y)
{
*x=st_x[top];
*y=st_y[top];
top--;
}
void bfconnect(int seedx, int seedy, int bcol, int fcol)
{
42
CG ASSIGNMENT
int x=seedx;
int y=seedy;
if(push(x,y)) return;
while(top>=0)
{
pop(&x,&y);
if(getpixel(x,y)!=fcol)
putpixel(x,y,fcol);
if(getpixel(x+1,y)!=fcol && getpixel(x+1,y)!=bcol)
if(push(x+1,y)) break;
if(getpixel(x,y+1)!=fcol && getpixel(x,y+1)!=bcol)
if(push(x,y+1))break;
if(getpixel(x-1,y)!=fcol && getpixel(x-1,y)!=bcol)
if(push(x-1,y)) break;
if(getpixel(x,y-1)!=fcol && getpixel(x,y-1)!=bcol)
if(push(x,y-1))break;
if(getpixel(x-1,y+1)!=fcol && getpixel(x-1,y+1)!=bcol)
if(push(x-1,y+1)) break;
if(getpixel(x-1,y-1)!=fcol && getpixel(x-1,y-1)!=bcol)
if(push(x-1,y-1)) break;
if(getpixel(x+1,y-1)!=fcol && getpixel(x+1,y-1)!=bcol)
if(push(x+1,y-1)) break;
if(getpixel(x+1,y+1)!=fcol && getpixel(x+1,y+1)!=bcol)
if(push(x+1,y+1)) break;
getch();
}
}
void main()
{
int gd=DETECT,gm=0;
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(200,200,150,150);
getch();
bfconnect(155,165,WHITE,RED);
putpixel(155,165,YELLOW);
getch();
}
43
CG ASSIGNMENT
ASSIGNMENT - 9
1) Write a Program of Bezier Curve Algorithm.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
struct point
{
int x,y;
};
typedef struct point point;
int round(double d)
{
int ans;
d+=(d>=0)?0.5:-0.5;
ans=d;
return ans;
}
void myline(point a,point b)
{
line(a.x,a.y,b.x,b.y);
}
point midpoint(point a,point b)
{
point ans;
ans.x=round((a.x+b.x)/2.0);
ans.y=round((a.y+b.y)/2.0);
return ans;
}
44
CG ASSIGNMENT
point ab,bc,cd;
point abc,bcd;
point abcd;
ab=midpoint(a,b);
bc=midpoint(b,c);
cd=midpoint(c,d);
abc=midpoint(ab,bc);
bcd=midpoint(bc,cd);
abcd=midpoint(abc,bcd);
bezier(a,ab,abc,abcd,n-1);
bezier(d,cd,bcd,abcd,n-1);
}
}
void inputpoint(point *p)
{
printf("\n\n\t X:");
scanf("%d",&(p->x));
printf("\n\n\t Y:");
scanf("%d",&(p->y));
}
void inputbezier(point p[],int *n)
{
int i;
for(i=0;i<=3;i++)
{
printf("\n\n Enter Point %d",i+1);
inputpoint(&(p[i]));
}
printf("\n\n Enter Number of Subdivision:");
scanf("%d",n);
}
void main()
{
int n;
point p[4];
45
CG ASSIGNMENT
int gd=DETECT,gm;
clrscr();
inputbezier(p,&n);
initgraph(&gd,&gm,"c:\\tc\\bgi"); bezier(p[0],p[1],p[2],p[3],n);
getch();
cleardevice();
closegraph();
}
2) Write a Program of RunLength Algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int re[500][50];
int index=-1,scind=-1;
void line1(int x1,int y1,int x2,int y2,int c)
{
setcolor(c);
line(x1,y1,x2,y2);
setcolor(WHITE);
}
void runlen(int xmin,int ymin,int xmax,int ymax,int drawcol,int backcol)
{
int bc,pc;
int px,py;
int flag=1;
int cnt=0;
int cur;
46
CG ASSIGNMENT
for(py=ymin;py<ymax;py++)
{
cur=getpixel(xmin,py);
++scind;
index=-1;
cnt=0;
for(px=xmin;px<=xmax;px++)
{
if(cur==getpixel(px,py))
{
cnt++;
}
else
{
re[scind][++index]=cnt;
re[scind][++index]=cur;
cur=getpixel(px,py);
cnt=1;
}
}
re[scind][++index]=cnt; //no of black
re[scind][++index]=cur; //color 0
}
}
void main(void)
{
int gd=DETECT,gm,ge;
initgraph(&gd,&gm,"C:\\tc\\bgi");
rectangle(55,55,100,100);
floodfill(57,57,15);
runlen(50,50,150,150,WHITE,0); //
for(int i=0;i<=scind;i++)
{
for(int j=0;j<=9;j++)
{
printf(" %d ",re[i][j]); //count color
}
printf("\n");
getch();
47
CG ASSIGNMENT
}
getch();
}
3) Write a Program of Triadic Koch Curve Algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
typedef struct
{
int x;
int y;
}Point;
void main()
{
int gd=DETECT;
int gm=0;
Point sp,ep;
initgraph(&gd,&gm,"C:\\tc\\bgi");
48
CG ASSIGNMENT
{
double midlength;
Point fmidPoint,smidPoint,tmidPoint;
if(abs(sp.x - ep.x) <= 1 && abs(sp.y - ep.y) <= 1)
return;
fmidPoint.x = round((ep.x+2*sp.x)/(double)3);
fmidPoint.y = round((ep.y+2*sp.y)/(double)3);
smidPoint.x = round((2*ep.x+sp.x)/(double)3);
smidPoint.y = round((2*ep.y+sp.y)/(double)3);
midlength = (abs(fmidPoint.x - smidPoint.x) >= abs(fmidPoint.y -
smidPoint.y)) ? abs(fmidPoint.x - smidPoint.x):abs(fmidPoint.y - smidPoint.y);
tmidPoint.x = round(midlength/(double)2);
tmidPoint.y = round((double)0.866025404 * midlength);
setcolor(BLACK);
line(fmidPoint.x,fmidPoint.y,smidPoint.x,smidPoint.y);
setcolor(WHITE);
line(fmidPoint.x,fmidPoint.y,tmidPoint.x,tmidPoint.y);
line(tmidPoint.x,tmidPoint.y,smidPoint.x,smidPoint.y);
getch();
triadicKoch(sp,fmidPoint);
triadicKoch(fmidPoint,tmidPoint);
triadicKoch(tmidPoint,smidPoint);
triadicKoch(smidPoint,ep);
}
Point inputPoint()
{
Point p;
printf("\n Enter the X co-ordinate : ");
scanf("%d",&p.x);
printf("\n Enter the Y co-ordinate : ");
scanf("%d",&p.y);
return p;
}
int round(double val)
{
int value;
if(abs(val-(int)val) <= 0.5)
value = floor(val);
else
value = ceil(val);
49
CG ASSIGNMENT
return value;
}
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
50
CG ASSIGNMENT
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{1,0,0,0,1,0,0,0},
{1,1,0,1,1,0,0,0},
{1,0,1,0,1,0,0,0},
{1,0,0,0,1,0,0,0}};
void main()
{
int gd=DETECT;
int gm=0;
int x,y,i,j;
int ds[16][16],dm[16][16];
initgraph(&gd,&gm,"C:\\tc\\bgi");
clrscr();
cleardevice();
doubleSize(ds,s);
print(30,30,5,8,s);
print(100,100,10,16,ds);
print(40,40,RED,8,ss);
print(200,200,5,8,m);
print(210,210,RED,8,sm);
doubleSize(dm,m);
print(250,250,10,16,dm);
getch();
closegraph();
}
void print(int x,int y,int color,int size,int cdouble[16][16])
{
int temp = x;
int i,j;
for(i=0;i<size;i++)
{
x=temp;//10;
for(j=0;j<size;j++)
{
if(cdouble[i][j] == 1)
putpixel(x,y,color);
x++;
}
51
CG ASSIGNMENT
y++;
}
}
void doubleSize(int dc[16][16],int o[16][16])
{
int i,j,k;
for(i=0;i<16;i+=2)
for(j=0;j<16;j+=2)
{
dc[i][j] = o[i/2][j/2];
dc[i+1][j] = dc[i+1][j+1] = dc[i][j+1] = dc[i][j];
}
}
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
typedef struct
{
int x;
int y;
}Point;
void hilbert(Point ,Point);
Point inputPoint();
int round(double );
void main()
{
int gd=DETECT;
int gm=0;
Point sp,ep;
initgraph(&gd,&gm,"C:\\tc\\bgi");
printf("\n Enter point for rectangle : ");
printf("\n Enter start Point : ");
sp = inputPoint();
52
CG ASSIGNMENT
ep = inputPoint();
clrscr();
cleardevice();
hilbert(sp,ep);
getch();
closegraph();
}
void hilbert(Point sp,Point ep)
{
Point sp1,ep1,sp2,ep2,sp3,ep3,sp4,ep4;
if(abs(sp.x - ep.x) <= 1 && abs(sp.y - ep.y) <= 1)
return;
sp1 = sp;
ep1.x = round((sp.x + ep.x)/2);
ep1.y = round((sp.y + ep.y)/2);
sp2.x = ep1.x; //round((sp.x + ep.x)/2);
sp2.y = sp.y;
ep2.x = ep.x;
ep2.y = ep1.y; //round((sp.y + ep.y)/2);
sp3 = ep1;
ep3 = ep;
sp4.x = sp.x;
sp4.y = ep1.y;
ep4.x = ep1.x;
ep4.y = ep.y;
putpixel(ep1.x,ep1.y,GREEN);
hilbert(sp1,ep1);
hilbert(sp2,ep2);
hilbert(sp3,ep3);
hilbert(sp4,ep4);
}
Point inputPoint()
{
Point p;
printf("\n Enter the X co-ordinate : ");
scanf("%d",&p.x);
printf("\n Enter the Y co-ordinate : ");
scanf("%d",&p.y);
return p;
}
53
CG ASSIGNMENT
54