Saturday, 27 August 2011

computer graphics prg


COMPUTER GRAPHICS PROGRAMS:
circle drawing:
#include"stdio.h"
#include"conio.h"
#include"math.h"
#include"graphics.h"
main()
{
 int gd=DETECT,gm;
 int xcenter,ycenter,radius;
 int p,x,y;
 initgraph(&gd,&gm,"c,\\tc\\bgi");
 x=0;
 printf("Enter The Radices Value:\n");
 scanf("%d",&radius);
 y=radius;
 printf("Enter xcenter & Ycenter Value:\n");
 scanf("%d %d",&xcenter,&ycenter);
 plotpoint(xcenter,ycenter,x,y);
 p=1-radius;
 while(x<y)
 {
if(p<0)
x=x+1;
 else
      {
x=x-1;
y=y-1;
 }
 if(p<0)
 p=p-1/x+1;
 else
  p=p+2*(x-y)+1;
  plotpoint(xcenter,ycenter,x,y);
  }
 getch();
 return(0);
 }
 int plotpoint(int xcenter ,int ycenter,int x,int y)
 {
 putpixel(xcenter +x,ycenter +y,1);
 putpixel(xcenter -x,ycenter +y,1);
 putpixel(xcenter +x,ycenter -y,1);
 putpixel(xcenter -x,ycenter -y,1);
 putpixel(xcenter +y,ycenter +x,1);
 putpixel(xcenter -y,ycenter +x,1);
 putpixel(xcenter +y,ycenter -x,1);
 putpixel(xcenter -y,ycenter -x,1);
 }
line drawing:

#include"stdio.h"
#include"conio.h"
#include"math.h"
#include"graphics.h"
main()
{
 int gd=DETECT,gm;
 int p,x,y,xend,xa,xb,ya,yb,dx,dy;
 initgraph(&gd,&gm,"");
 printf("\nEnter the two left end points(xa,ya):\n");
 scanf("%d%d",&xa,&ya);
 printf("\nEnter the two right end points(xb,yb):\n");
 scanf("%d%d",&xb,&yb);
 dx=abs(xa-xb);
 dy=abs(ya-yb);
 p=2*dy-dx;
 if(xa>xb)
 {
  x=xb;
  y=yb;
  xend=xa;
 }
 else
 {
  x=xa;
  y=ya;
  xend=xb;
 }
 putpixel(x,y,6);
 while(x<xend)
 {
  x=x+1;
  if(p<0)
  {
   p=p+2*dy;
  }
  else
  {
   y=y+1;
   p=p+2*(dy-dx);
  }
  putpixel(x,y,6);
 }
 getch();
 return(0);
}
2 dimensional transformation:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void reflection();
int a[10][2],i,x,option;
int temp,angle,tx,ty,fx,fy;
int  sh,k,n,axis,y;
float sx,sy;
void menu()
{
printf("\nmenu\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit\nenter choice");
scanf("%d",&option);
switch(option)
{
case 1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;
case 4:
input();
shearing();
break;
case 5:
input();
reflection();
break;
case 6:
exit(0);
break;
}
}
void input()
{
printf("\n enter no.of vertics");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter coordinate:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
cleardevice();
for(i=0;i<n;i++)
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
void translation()
{
output();
printf("\n enter translation vertics");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
{
a[i][0]+=tx;
a[i][1]+=ty;
}
output();
delay(10);
menu();
}
void rotation()
{
output();
printf("\n enter rotating angle");
scanf("%d",&y);
printf("\n enter print point:");
scanf("%d%d",&fx,&fy);
k=(y*3.14)/100;
for(i=0;i<=n;i++)
{
a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
}
output();
delay(10);
menu();
}
void shearing()
{
output();
printf("\n enter shear value:");
scanf("%d",&sh);
printf("\n enter fixed point:");
scanf("%d%d",&fx,&fy);
printf("\nenter the axis for shearing if x-axis then 1 &y-axis is 0:");
scanf("%d",&axis);
for(i=0;i<=n;i++)
{
if(axis==1)
a[i][0]=a[i][0]+sh*(a[i][1]-fy);
else
a[i][1]=a[i][1]+sh*(a[i][0]-fx);
}
output();
delay(10);
menu();
}
void scaling()
{
output();
printf("\n enter scaling factor:");
scanf("%f%f",&sx,&sy);
printf("\n enter fixed point:");
scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
output();
delay(10);
menu();
}
void reflection()
{
output();
for(i=0;i<=n;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
output();
delay(10);
menu();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tcplus\\bgi");
menu();
getch();
}
line clipping:

#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
float cx1,cxr,cyt,cyb;
code(float,float);
void clip(float,float,float,float);
void rect(float,float,float,float);
void main()
{
float x1,y1,x2,y2;
int g=0,d;
initgraph(&g,&d,"c:\\tc\\bgi");
settextstyle(1,0,1);
outtextxy(40,15,"before cliping");
printf("\n\n\n please enter the left bottom,right,top of clip window");
scanf("%f%f%f%f",&cx1,&cyt,&cxr,&cyb);
rect(cx1,cyb,cxr,cyt);
getch();
printf("\nplease enter the line(x1,y1,x2,y2)");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
getch();
cleardevice();
settextstyle(1,0,1);
outtextxy(40,15,"after cliping");
clip(x1,y1,x2,y2);
getch();
closegraph();
}
void clip(float x1,float y1,float x2,float y2)
{
int c,c1,c2;
float x,y;
c1=code(x1,y1);
c2=code(x2,y2);
getch();
while((c1!=0)||(c2!=0))
{
if((c1&c2)!=0)
goto out;
c=c1;
if(c==0)
c=c2;
if((c&1)==1)
{
y=y1+(y2-y1)+(cx1-x1)/(x2-x1);
x=cx1;
}
else
if((c&2)==2)
{
y=y+(y2-y1)*(cx1-x1)/(x2-x1);
x=cxr;
}
else
if((c&8)==8)
{
x=x1+(x2-x1)*(cyb-y1)/(y2-y1);
x=cyb;
}
else
if((c&4)==4)
{
x=x1+(x2-x1)*(cyt-y1)/(y2-y1);
x=cyt;
}
if(c==c1)
{
x1=x;
y1=y;
c1=code(x,y);
}
else
{
x2=x;
y2=y;
c2=code(x,y);
}
}
out:
rect(cx1,cyb,cxr,cyt);
line(x1,y1,x2,y2);
}
code(float x,float y)
{
int c=0;
if(x<cx1)
c=1;
else
if(x>cxr)
c=2;
if(y<cyb)
c=c/8;
else
if(y>cyt)
c=c/4;
return c;
}
void rect(float x1,float yb,float xr,float yt)
{
line(x1,yb,xr,yb);
line(xr,yb,xr,yt);
line(xr,yt,x1,yt);
line(x1,yt,x1,yb);
}
view porting:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
{
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("enter x1,y1,x2,y2,x3,y3\n");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;w2=5;w3=635;w4=665;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
v1=425;v2=75;v3=550;v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+.5);
x2=v1+floor(((float)(x2-w1)*sx)+.5);
x3=v1+floor(((float)(x3-w1)*sx)+.5);
y1=v2+floor(((float)(y1-w2)*sy)+.5);
y2=v2+floor(((float)(y2-w2)*sy)+.5);
y3=v2+floor(((float)(y3-w2)*sy)+.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y2,x1,y1);
getch();
return(0);
}

0 comments:

Post a Comment

Template Design by SkinCorner