Thursday, May 5, 2016
BRESENHAM LINE DRAWING PROGRAM IN C
/*BRESENHAM LINE DRAWING PROGRAM IN C */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2;
int dx,dy,i;
float m,e;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\nEnter the co-ordinates of (x1,y1) :");
scanf("%d%d",&x1,&y1);
printf("\nEnter the co-ordinates of (x2,y2) :");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=(float)dy/dx;
if(m<1)
{
e=(2*dy)-dx;
for(i=0;i<=dx;i++){
if(e<0)
{
if(x1<x2)
x1++;
else
x1--;
e=e+(2*dy);
}
else
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
e=e+2*(dy-dx);
}
putpixel(x1,y1,RED);
}}
else
{
e=(2*dx)-dy;
for(i=0;i<=dy;i++){
if(e<0)
{
if(y1<y2)
y1++;
else
y1--;
e=e+(2*dx);
}
else
{
if(x1<x2)
x1++;
else
x1--;
if(y1<y2)
y1++;
else
y1--;
e=e+2*(dx-dy);
}
putpixel(x1,y1,GREEN);
}}
getch();
}
MID POINT CIRCLE DRAWING PROGRAM IN C
/*MID POINT CIRCLE DRAWING*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver = DETECT,gmode,x,y,r,x1,y1;
float e;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI");
printf("center co-ordinate of x and y position of circle:\n");
scanf("%d %d",&x,&y);
printf("input the radius of circle:\n");
scanf("%d",&r);
x1=0;
y1=r;
putpixel(x+0,y+r,RED);
putpixel(x+r,y+0,RED);
putpixel(x-0,y-r,RED);
putpixel(x-r,y-0,RED);
e=1.25-r;
while(x1<y1)
{
if(e<0)
{
x1=x1+1;
e=e+2*x1+3;
}
else
{
x1=x1+1;
y1=y1-1;
e=e+4*(x1-y1)+5;
}
putpixel(x+x1,y+y1,1);
putpixel(x+y1,y+x1,2);
putpixel(x-x1,y+y1,3);
putpixel(x-y1,y+x1,4);
putpixel(x-x1,y-y1,5);
putpixel(x-y1,y-x1,6);
putpixel(x+x1,y-y1,7);
putpixel(x+y1,y-x1,8);
delay(100);
}
getch();
}
/*NOTE:-You have to change the graphics driver location according to your compiler graphics file address which has been written in initgraph in quotation. */
DDA LINE DRAWING PROGRAM IN C
/* DDA LINE DRAWING */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define round(a) ((int)(a+0.5))
void main()
{
int gd=DETECT,gm,x1,y1,x2,y2,dx,dy,length,i;
float xinc,yinc,x,y;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("enter the end point co ordinates of lines");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
x=x1;
y=y1;
dx=x2-x2;
dy=y2-y1;
if(abs(dx)>abs(dy))
{
length=abs(dx);
}
else
{
length=abs(dy);
}
xinc=dx/(float)length;
yinc=dy/(float)length;
putpixel(round(x),round(y),RED);
for(i=0;i<length;i++)
{
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),RED);
}
getch();
}
POLYGON FORMATION IN C
/* POLYGON FORMATION IN C */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int n,x[15],y[15],i;
printf("ENTER THE NUMBER OF SIDE OF POLYGON:\n");
scanf("%d",&n);
if(n<3)
{
printf("POLYGON CAN'T BE FORM\n");
getch();
exit(0);
}
else
{
for(i=1;i<=n;i++)
{
printf("ENTER THE CORDINATES OF THE POLYGON %d SIDE\n",i);
scanf("%d %d",&x[i],&y[i]);
}
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\BGI");
for(i=1;i<n;i++)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
line(x[1],y[1],x[n],y[n]);
}
getch();
}
TRANSFORMATION OF SHAPES IN C
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
void main()
{
again:
int gd=DETECT,gm,n;
double a[10][10],b[10][10],c[10][10];
int i,j,k,m,x1[10],y1[10],x[10],y[10],sx,sy;
char ch;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the no of sides : ");
scanf("%d",&n);
if(n<3)
{
printf("\nPolygon can not form\n");
goto exit;
getch();
}
else
{
for(i=1;i<=n;i++)
{
printf("\nEnter the co-ordinates (x%d,y%d) : ",i,i);
scanf("%d%d",&x[i],&y[i]);
}
cleardevice();
for(i=1;i<n;i++)
{
setcolor(RED);
line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]);
}
setcolor(RED);
line(320+x[1],240-y[1],320+x[n],240-y[n]);
}
getch();
//again:
printf("\nWhat you want to do : \n");
printf("\n1.Scaling");
printf("\n2.Translating");
printf("\n3.Reflection");
printf("\n4.Rotation");
printf("\n5.Shearing");
printf("\n6.Exit");
printf("\nPlease enter your choice : ");
scanf("%d",&m);
switch(m)
{
case 1:
{
printf("\nEnter the scaling points : ");
scanf("%d%d",&sx,&sy);
a[1][1]=sx;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=sy;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
int tx,ty;
printf("\nEnter the translation points : ");
scanf("%d%d",&tx,&ty);
a[1][1]=1;
a[1][2]=0;
a[1][3]=tx;
a[2][1]=0;
a[2][2]=1;
a[2][3]=ty;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
int r;
printf("\nWhich direction");
printf("\n1.Reflection in x direction ");
printf("\n2.Reflection in y direction ");
printf("\n3.Reflection about origin");
printf("\nEnter choice : ");
scanf("%d",&r);
switch(r)
{
case 1:
{
a[1][1]=1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 4:
{
int q;
double s,o;
printf("\nEnter the angle : ");
scanf("%lf",&o);
s=(o*(3.14/180));
printf("\nWhich direction :");
printf("\n1.Clockwise direction ");
printf("\n2.Anticlockwise direction ");
printf("\nEnter choice : ");
scanf("%d",&q);
switch(q)
{
case 1:
{
a[1][1]=cos(s);
a[1][2]=sin(s);
a[1][3]=0;
a[2][1]=-sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=cos(s);
a[1][2]=-sin(s);
a[1][3]=0;
a[2][1]=sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 5:
{
int shx,shy;
printf("\nEnter the shearing points : ");
scanf("%d%d",&shx,­);
a[1][1]=1;
a[1][2]=shx;
a[1][3]=0;
a[2][1]=shy;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
exit: case 6:
{
getch();
exit (0);
}
default :
{
printf("\nINVALID INPUT");
break;
}
}
for(i=1;i<=n;i++)
{
b[1][i]=x[i];
b[2][i]=y[i];
b[3][i]=1;
}
for(i=1;i<=3;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=0;
for(k=1;k<=3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=1;i<=n;i++)
{
x1[i]=c[1][i];
y1[i]=c[2][i];
}
for(i=1;i<n;i++)
{
setcolor(GREEN);
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}
setcolor(GREEN);
line(320+x1[1],240-y1[1],320+x1[n],240-y1[n]);
getch();
printf("\nAre you want to continue :y/n ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
{
cleardevice();
goto again;
}
else
{
cleardevice();
goto exit;
}
getch();
}
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
void main()
{
again:
int gd=DETECT,gm,n;
double a[10][10],b[10][10],c[10][10];
int i,j,k,m,x1[10],y1[10],x[10],y[10],sx,sy;
char ch;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the no of sides : ");
scanf("%d",&n);
if(n<3)
{
printf("\nPolygon can not form\n");
goto exit;
getch();
}
else
{
for(i=1;i<=n;i++)
{
printf("\nEnter the co-ordinates (x%d,y%d) : ",i,i);
scanf("%d%d",&x[i],&y[i]);
}
cleardevice();
for(i=1;i<n;i++)
{
setcolor(RED);
line(320+x[i],240-y[i],320+x[i+1],240-y[i+1]);
}
setcolor(RED);
line(320+x[1],240-y[1],320+x[n],240-y[n]);
}
getch();
//again:
printf("\nWhat you want to do : \n");
printf("\n1.Scaling");
printf("\n2.Translating");
printf("\n3.Reflection");
printf("\n4.Rotation");
printf("\n5.Shearing");
printf("\n6.Exit");
printf("\nPlease enter your choice : ");
scanf("%d",&m);
switch(m)
{
case 1:
{
printf("\nEnter the scaling points : ");
scanf("%d%d",&sx,&sy);
a[1][1]=sx;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=sy;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
int tx,ty;
printf("\nEnter the translation points : ");
scanf("%d%d",&tx,&ty);
a[1][1]=1;
a[1][2]=0;
a[1][3]=tx;
a[2][1]=0;
a[2][2]=1;
a[2][3]=ty;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
int r;
printf("\nWhich direction");
printf("\n1.Reflection in x direction ");
printf("\n2.Reflection in y direction ");
printf("\n3.Reflection about origin");
printf("\nEnter choice : ");
scanf("%d",&r);
switch(r)
{
case 1:
{
a[1][1]=1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 3:
{
a[1][1]=-1;
a[1][2]=0;
a[1][3]=0;
a[2][1]=0;
a[2][2]=-1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 4:
{
int q;
double s,o;
printf("\nEnter the angle : ");
scanf("%lf",&o);
s=(o*(3.14/180));
printf("\nWhich direction :");
printf("\n1.Clockwise direction ");
printf("\n2.Anticlockwise direction ");
printf("\nEnter choice : ");
scanf("%d",&q);
switch(q)
{
case 1:
{
a[1][1]=cos(s);
a[1][2]=sin(s);
a[1][3]=0;
a[2][1]=-sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
case 2:
{
a[1][1]=cos(s);
a[1][2]=-sin(s);
a[1][3]=0;
a[2][1]=sin(s);
a[2][2]=cos(s);
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
}
break;
}
case 5:
{
int shx,shy;
printf("\nEnter the shearing points : ");
scanf("%d%d",&shx,­);
a[1][1]=1;
a[1][2]=shx;
a[1][3]=0;
a[2][1]=shy;
a[2][2]=1;
a[2][3]=0;
a[3][1]=0;
a[3][2]=0;
a[3][3]=1;
break;
}
exit: case 6:
{
getch();
exit (0);
}
default :
{
printf("\nINVALID INPUT");
break;
}
}
for(i=1;i<=n;i++)
{
b[1][i]=x[i];
b[2][i]=y[i];
b[3][i]=1;
}
for(i=1;i<=3;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=0;
for(k=1;k<=3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=1;i<=n;i++)
{
x1[i]=c[1][i];
y1[i]=c[2][i];
}
for(i=1;i<n;i++)
{
setcolor(GREEN);
line(320+x1[i],240-y1[i],320+x1[i+1],240-y1[i+1]);
}
setcolor(GREEN);
line(320+x1[1],240-y1[1],320+x1[n],240-y1[n]);
getch();
printf("\nAre you want to continue :y/n ");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y'||ch=='y')
{
cleardevice();
goto again;
}
else
{
cleardevice();
goto exit;
}
getch();
}
MID POINT SUB DIVISION PROGRAM IN C
MID POINT SUB DIVISION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
int x_min,y_min,x_max,y_max;
void mpsd(int,int,int,int);
void main()
{
int x1,x2,y1,y2;
int gdriver =DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI");
printf("Enter the value of window coordinate:");
scanf("%d %d %d %d",&x_min,&y_min,&x_max,&y_max);
printf("Enter the coordinates of the view point:");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
cleardevice();
rectangle(x_min,y_min,x_max,y_max);
mpsd(x1,y1,x2,y2);
getch();
}
void mpsd(int xa,int ya,int xb,int yb)
{
if(((xa>=x_min)&&(ya>=y_min)&&(xa<=x_max)&&(ya<=y_max))&&((xb>=x_min)&&(yb>=y_min)&&(xb<=x_max)&&(yb<=y_max)))
{
setcolor(RED);
line(xa,ya,xb,yb);
}
else
{ if(((xa<=x_min)&&(xb<=x_min))||((ya<=y_min)&&(yb<=y_min))||((xa>=x_max)&&(xb>=x_max))||((ya>=y_max)&&(yb>=y_max)))
{ setcolor(BLUE);
line(xa,ya,xb,yb);
}
else
{
mpsd(xa,ya,((xa+xb)/2),((ya+yb)/2));
mpsd(((xa+xb)/2),((ya+yb)/2),xb,yb);
}
}}
Subscribe to:
Posts (Atom)