Showing posts with label graphics programs. Show all posts
Showing posts with label graphics programs. Show all posts

Sunday, February 14, 2010

color model

#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"stdlib.h"
void main()
{
int gd=DETECT,gm,n;
float r=0,g=0,b=0;
float y=0,i=0,q=0;
float c=0,m=0,y1=0;
initgraph(&gd,&gm,"");
printf("\t(r,g,b)keys for incrementing R,G,B values respectively\n");
printf("\t(shift+(r,g,b))keys for decrementing R,G,B values respectively\n");
printf("\tpress esc to exit\n");
setcolor(15);
while(1)
{
gotoxy(18,10);
printf("R G B");
c=1.0-r;
m=1.0-g;
y1=1.0-y;
gotoxy(47,10);
printf("C M Y");
y=0.299*r+0.587*g+0.144*b;
i=0.596*r-0.275*g-0.3218*b;
q=0.212*r-0.528*g+0.311*b;
gotoxy(18,23);
printf("Y I Q");
switch(getch())
{
case 'r':
r++;
break;
case 'g':
g++;
break;
case 'b':
b++;
break;
case 'R':
r--;
break;
case 'G':
g--;
break;
case 'B':
b--;
break;
case 27:
closegraph();
exit(0);
}
if(r>255)
r=0;
if(g>255)
g=0;
if(b > 255)
b=0;
setrgbpalette(1,r,g,b);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(2,c,m,y1);
setfillstyle(1,2);
bar(275,50,495,250);
rectangle(275,50,495,250);
setrgbpalette(3,y,i,q);
setfillstyle(1,3);
bar(50,255,270,455);
rectangle(50,255,270,455);
}
}
OUTPUT:




Sunday, January 17, 2010

bresenhams line drawing algorithm

program:
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"
#include"dos.h"
#include"math.h"

void main()
{
int driver,mode,k;
int xa,xb,ya,yb,dx,dy,x,y,i,p,dy2,dydx2;
clrscr();
printf("\n\tEnter(xa,ya)...");
scanf("%d%d",&xa,&ya);
printf("\n\tEnter(xb,yb)...");
scanf("%d%d",&xb,&yb);
x=xa;
y=ya;
driver=DETECT;
initgraph(&driver,&mode,"");
dx=abs(xb-xa);
dy=abs(yb-ya);
p=(2*dy)-dx;
dy2=2*dy;
dydx2=2*(dy-dx);
line(100,0,100,500);
line(0,100,500,100);
putpixel(x,y,2);
for(i=0;i<=dx;i++)
{
if(p<0)
{
x=x+1;
putpixel(x,y,2);
p+=dy2;
}
else
{
x=x+1;
y=y+1;
putpixel(x,y,2);
p+=dydx2;
}
}
getch();
}
output:


Enter(xa,ya)...100 100
Enter(xb,yb)...250 252




Saturday, January 16, 2010

DDA LINE Drawing Algorithm



download this file : dda.c


program:
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"
#include"dos.h"
#include"math.h"
void main()
{
int i,gd,gm,delay=1;
float dx,dy,x1,x2,y1,y2,step,maxx,maxy,roundx,roundy;
float x,y,xi,yi;
char buf1[50],buf2[50],buf3[50],buf4[10],buf5[10];
clrscr();
gd=DETECT;
printf("\n Enter start co-ordinates (x1,y1) :");
scanf("%f%f",&x1,&y1);
printf("\n Enter end co-ordinates  (x2,y2) :");
scanf("%f%f",&x2,&y2);
clrscr();
x1+=100;
x2+=100;
y1+=100;
y2+=100;
initgraph(&gd,&gm,"");
dx=x2-x1;
dy=y2-y1;
maxx=getmaxx();
maxy=getmaxy();
line(100,0,100,maxy);
line(0,100,maxx,100);
outtextxy(80,90,"0");
outtextxy(80,200,"100");
outtextxy(80,300,"200");
outtextxy(80,400,"300");
outtextxy(80,500,"400");
outtextxy(180,90,"100");
outtextxy(280,90,"200");
outtextxy(380,90,"300");
outtextxy(480,90,"400");
outtextxy(580,90,"500");
sprintf(buf1," DDA LINE ALGORITHM ");
outtextxy(300,10,buf1);
sprintf(buf2,"x1=%3.0f,y1=%3.0f,x2=%3.0f,y2=%3.0f,",x1-100,y1-100,x2-100,y2-100);
outtextxy(200,20,buf2);
if(abs(dx)>abs(dy))
step=dx;
else
step=dy;
xi = dx / step;
yi = dy / step;
sprintf(buf3,"dx=%3.0f,dy=%3.0f,step=%3.0f,xi=%3.2f,yi=%3.2f",dx,dy,step,xi,yi);
outtextxy(200,30,buf3);
x=x1;
y=y1;
putpixel(x,y,delay);
circle(x,y,2);
sprintf(buf4,"(%3.0f,%3.0f)",x1-100,y1-100);
outtextxy(x1+10,y1,buf4);
for(i=1;i<=step;i++)
{
  if(delay==15)
  {
sleep(1);
delay=1;
  }
  delay++;
  x=x+xi;
  y=y+yi;
  roundx=ceil(x);
  roundy=ceil(y);
  putpixel(roundx,roundy,delay);
}
circle(roundx,roundy,2);
sprintf(buf5,"(%3.0f,%3.0f)",roundx-100,roundy-100);
outtextxy(roundx+10,roundy,buf5);

getch();
restorecrtmode();
}

download this file : dda.c


ouput:
 Enter start co-ordinates (x1,y1) : 100    125
 Enter end co-ordinates  (x2,y2) : 250    350








two dimensional transformation





Download this file : 2d.c




program:
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
int gd=0,gm,theta,xf,yf,x1,y1,x2,y2,xa1,xa2,ya1,ya2,a;
int midx,midy,tx,ty,choice,sx,sy,sh,xa[10],ya[10],first;
int i,n=5;
int xf,yf;
int xb[10],yb[10];
void main()
{
do
{
first=0;
org();
getch();
printf("\n1.translation\n2.rotation\n3.scaling\n4.reflection\n5.shear\n6.exit\nenter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the translation factor (x,y) :");
scanf("%d%d",&tx,&ty);
org();
rectangle(midx+tx,midy-100+ty,midx+100+tx,midy+ty);
getch();
closegraph();
break;
case 2:
printf("Enter the angle :");
scanf("%d",&a);
org();
rotation();
break;
case 3:
printf("Enter the scaling factor (x,y):");
scanf("%d%d",&sx,&sy);
org();
x1=0;
y1=-100;
x2=100;
y2=0;
xa1=x1*sx+midx;
ya1=y1*sy+midy;
xa2=x2*sx+midx;
ya2=y2*sy+midy;
rectangle(xa1,ya1,xa2,ya2);
getch();
closegraph();
break;
case 4:
org();
rectangle(midx,midy,midx+100,midy+100);
getch();
closegraph();
break;
case 5:
printf("enter the shear value");
scanf("%d",&sh);
cleardevice();
org();
for(i=0;i < n;i++)
{
xb[i]=xa[i]+sh*(ya[i]-yf);
yb[i]=ya[i];
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
}
}while(choice<6);
getch();
}
org()
{
initgraph(&gd,&gm,"");
midx=getmaxx()/2;
midy=getmaxy()/2;
xf=midx;
yf=midy;
xa[0]=midx;
ya[0]=midy-100;
xa[1]=midx+100;
ya[1]=midy-100;
xa[2]=midx+100;
ya[2]=midy;
xa[3]=midx;
ya[3]=midy;
xa[4]=midx;
ya[4]=midy-100;
cleardevice();
setcolor(WHITE);
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
setcolor(4);
if(first==0)
{
setlinestyle(SOLID_LINE,1,1);
first=1;
}
else
{
setlinestyle(DASHED_LINE,1,1);
}
rectangle(midx,midy-100,midx+100,midy);
setlinestyle(SOLID_LINE,1,1);
return;
}
rotation()
{
theta=(a*2*3.14)/180;
for(i=0;i < n;i++)
{
xb[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
yb[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
cleardevice();
return;
}


Download this file : 2d.c

Output:






























midpoint circle algorithm




Download this file  : circle.c




program:
#include"graphics.h"
#include"stdio.h"
#include"conio.h"
#include"dos.h"
int step=0,r;
void main()
{
int i,j,x,y,p,a,midx,midy;
int gd=DETECT,gm,xcenter,ycenter;
printf("Enter the radius  :");
scanf("%d",&r);
initgraph(&gd,&gm,"");
midx=getmaxx()/2;
midy=getmaxy()/2;
line(midx,0,midx,2*midy);
line(0,midy,2*midx,midy);
outtextxy(midx-10,midy+10,"0");
outtextxy(midx-20,10,"-y");
outtextxy(midx-20,2*midy-10,"+y");
outtextxy(10,midy-10,"-x");
outtextxy(2*midx-20,midy-10,"+x");
xcenter=midx;
ycenter=midy;
x=0;
y=r;
p=1-r;
midpoint(xcenter,ycenter,x,y,p);
getch();
closegraph();
restorecrtmode();

}
midpoint(int xcenter,int ycenter,int x,int y,int p)
{
while(x < y)
{
plotcircle(xcenter,ycenter,x,y);
if(p<0)
p=p+2*x+1;
else
{
p=p+2*(x-y)+1;
y=y-1;
}
x=x+1;
}
if(x==y)
plotcircle(xcenter,ycenter,x,y);
return;
}
plotcircle(int xcenter,int ycenter,int x,int y)
{
delay(50);
step++;
if(step==r/2)
{
outtextxy(xcenter+x,ycenter+y,"(+x,-y)");
outtextxy(xcenter-x-50,ycenter+y,"(-x,-y)");
outtextxy(xcenter+x,ycenter-y,"(+x,+y)");
outtextxy(xcenter-x-50,ycenter-y,"(-x,+y)");
outtextxy(xcenter+y,ycenter+x,"(+y,-x)");
outtextxy(xcenter-y-50,ycenter+x,"(-y,-x)");
outtextxy(xcenter+y,ycenter-x,"(+y,+x)");
outtextxy(xcenter-y-50,ycenter-x,"(-y,+x)");
}
putpixel(xcenter+x,ycenter+y,10);
putpixel(xcenter-x,ycenter+y,10);
putpixel(xcenter+x,ycenter-y,10);
putpixel(xcenter-x,ycenter-y,10);
putpixel(xcenter+y,ycenter+x,10);
putpixel(xcenter-y,ycenter+x,10);
putpixel(xcenter+y,ycenter-x,10);
putpixel(xcenter-y,ycenter-x,10);
putpixel(xcenter+x,ycenter-x,10);
return;
}




Download this file  : circle.c



output:


Enter the radius : 150









Download this file  : circle.c