#include<stdio.h> #include<graphics.h> struct point { int x,y; }pol[3],polnew[3],wmin,wmax,vmin,vmax; float sx=0.5,sy=0.5; int n=3,max; float trans[3][3],result[3][1],xandy[3][1]; float minside() { int i; float a,s; i=0; s=sqrt((pol[i].x-pol[(i+1)%3].x)*(pol[i].x-pol[(i+1)%3].x)+ (pol[i].y-pol[(i+1)%3].y)*(pol[i].y-pol[(i+1)%3].y)); for(i=1;i<3;i++) { a=sqrt((pol[i].x-pol[(i+1)%3].x)*(pol[i].x-pol[(i+1)%3].x)+ (pol[i].y-pol[(i+1)%3].y)*(pol[i].y-pol[(i+1)%3].y)); if(a<s) s=a; } return s; } void readpoly() { int i; for(i=0;i<n;i++) { printf("\nEnter the %d co=ordinates ",i+1); scanf("%d%d",&pol[i].x,&pol[i].y); } } void drawpolygon(struct point p[]) { int i; for(i=0;i<n;i++) line(p[i].x,p[i].y,p[(i+1)%n].x,p[(i+1)%n].y); } void mulmat() { int i,j,k; for(i=0;i<3;i++) { for(j=0;j<1;j++) { result[i][j]=0.0; for(k=0;k<3;k++) result[i][j]+=trans[i][k]*xandy[k][j]; } } } void transformpoly(struct point p1[],struct point p2[]) { int i; for(i=0;i<n;i++) { xandy[0][0]=p1[i].x; xandy[1][0]=p1[i].y; xandy[2][0]=1; mulmat(); p2[i].x=result[0][0]; p2[i].y=result[1][0]; } } void makeIdentity() { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) trans[i][j]=0.0; trans[i][i]=1.0; } } void sierp(struct point pp[],int level) { struct point p1[3],p2[3],p3[3]; int dx,dy,i; level++; if(level==max) return; makeIdentity(); trans[0][0]=sx; trans[0][2]=(1-sx)*(float)pp[0].x; trans[1][1]=sy; trans[1][2]=(1-sy)*(float)pp[0].y; transformpoly(pp,p1); drawpolygon(p1); makeIdentity(); dx=-p1[1].x; dy=-p1[1].y; trans[0][2]=dx; trans[1][2]=dy; transformpoly(p1,p2); makeIdentity(); dx=pp[1].x; dy=pp[1].y; trans[0][2]=dx; trans[1][2]=dy; transformpoly(p2,p2); drawpolygon(p2); makeIdentity(); dx=-p1[2].x; dy=-p1[2].y; trans[0][2]=dx; trans[1][2]=dy; transformpoly(p1,p3); makeIdentity(); dx=pp[2].x; dy=pp[2].y; trans[0][2]=dx; trans[1][2]=dy; transformpoly(p3,p3); drawpolygon(p3); sierp(p1,level); sierp(p2,level); sierp(p3,level); } int main() { int gd=DETECT,gm=VGAMAX; int choice,dx,dy,i; float small; readpoly(); initgraph(&gd,&gm,NULL); drawpolygon(pol); setcolor(RED); getch(); small=minside(); max=2+(int)small/50; sierp(pol,0); while (!kbhit()); closegraph(); return 0; }
Graphics programming using c
Sunday, December 12, 2010
Draw a Sierpinski triangle using fractal geometric method

Sunday, November 21, 2010
Draw a piechart

#include<stdio.h> #include<graphics.h> #include<math.h> #define ROUND(a)((int)(a+0.5)) int p[10]; float trans[3][3],result[3][1],xandy[3][1]; void plot(int,int,int,int); void boundaryfill4(int,int,int,int); void DDA(int xa,int ya,int xb,int yb) { int dx,dy,steps,k; dx=xb-xa; dy=yb-ya; float x=xa,y=ya,xinc,yinc; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; putpixel(ROUND(x),ROUND(y),10); for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(ROUND(x),ROUND(y),10); } } void midpoint(int xc,int yc,int r) { int x=0; int y=r; int p=1-r; plot(xc,yc,x,y); while(x<y) { x++; if(p<0) p+=2*x+1; else { y--; p+=2*(x-y)+1; } plot(xc,yc,x,y); } } void plot(int xc,int yc,int x,int y) { putpixel(xc+x,yc+y,10); putpixel(xc-x,yc+y,10); putpixel(xc+x,yc-y,10); putpixel(xc-x,yc-y,10); putpixel(xc+y,yc+x,10); putpixel(xc-y,yc+x,10); putpixel(xc+y,yc-x,10); putpixel(xc-y,yc-x,10); } void boundaryfill4(int x,int y,int new,int bdry) { int current=getpixel(x,y); if((current!=bdry)&&(current!=new)) { putpixel(x,y,new); boundaryfill4(x+1,y,new,bdry); boundaryfill4(x-1,y,new,bdry); boundaryfill4(x,y+1,new,bdry); boundaryfill4(x,y-1,new,bdry); } } void mulmat() { int i,j,k; for(i=0;i<3;i++) { for(j=0;j<1;j++) { result[i][j]=0.0; for(k=0;k<3;k++) result[i][j]+=trans[i][k]*xandy[k][j]; } } } void transformpoint(int x,int y) { xandy[0][0]=x; xandy[1][0]=y; xandy[2][0]=1; mulmat(); } void makeIdentity() { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) trans[i][j]=0.0; trans[i][i]=1.0; } } void rotate(int rx,int ry,int an) { float ang; ang=(float)(an*(3.14/180)); trans[0][0]=cos(ang); trans[0][1]=-sin(ang); trans[0][2]=rx*(1-cos(ang))+ry*sin(ang); trans[1][0]=sin(ang); trans[1][1]=cos(ang); trans[1][2]=ry*(1-cos(ang))-rx*sin(ang); } int main() { int gd=DETECT,gm=VGAMAX,n,i,tot=0,r=100,rx,ry,x,y,midx,midy,prx,pry; float an1=0.0,an=0.0; printf("enter the no. of data"); scanf("%d",&n); do{ for(i=0;i<n;i++) { printf("\nEnter the data "); scanf("%d",&p[i]); tot+=p[i]; } if(tot!=100) { printf("Error in data. Enter it again "); tot=0; } }while(tot!=100); initgraph(&gd,&gm,"NULL"); rx=getmaxx()/2; ry=getmaxy()/2; x=rx; y=r+ry; midx=rx; midy=(y+ry)/2; midpoint(rx,ry,r); DDA(rx,ry,x,y); for(i=0;i<n;i++) { makeIdentity(); an1=an+(float)(p[i]*3.6)/2; an+=(float)p[i]*3.6; rotate(rx,ry,an); transformpoint(x,y); DDA(rx,ry,result[0][0],result[1][0]); makeIdentity(); rotate(rx,ry,an1); transformpoint(midx,midy); boundaryfill4(result[0][0],result[1][0],i+1,10); } while(!kbhit()); closegraph(); return 0; }
Draw a Sierpinski triangle

#include<stdio.h> #include<graphics.h> #include<math.h> #define pi 3.142857 #define ROUND(a)((int)(a+0.5)) int max; void DDA(int xa,int ya,int xb,int yb) { int dx,dy,steps,k; dx=xb-xa; dy=yb-ya; float x=xa,y=ya,xinc,yinc; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; putpixel(ROUND(x),ROUND(y),5); for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(ROUND(x),ROUND(y),5); } } float minside(int x1,int y1,int x2,int y2,int x3,int y3) { float a,b,c,s; a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); b=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3)); if(a>b) s=a; else s=b; if(s>c) s=c; return s; } void triangle(int x1,int y1,int x2,int y2,int x3,int y3,int i) { float newx1,newy1,newx2,newy2,newx3,newy3; i++; if(i==max) return; else { DDA(x1,y1,x2,y2); DDA(x2,y2,x3,y3); DDA(x3,y3,x1,y1); newx1=(x1+x2)/2 ; newy1=(y1+y2)/2; newx2=(x2+x3)/2 ; newy2=(y2+y3)/2; newx3=(x1+x3)/2 ; newy3=(y1+y3)/2; triangle(x1,y1,newx1,newy1,newx3,newy3,i); triangle(x2,y2,newx1,newy1,newx2,newy2,i); triangle(x3,y3,newx2,newy2,newx3,newy3,i); } } int main() { int gd=DETECT,gm=VGAMAX; int x1,y1,x2,y2,x3,y3; float small; printf("\n Enter the 1st coordinates of the triangle"); scanf("%d%d",&x1,&y1); printf("\n enter the 2nd coordinates of the triangle"); scanf("%d%d",&x2,&y2); printf("\n enter the 3rd coordinates of the triangle"); scanf("%d%d",&x3,&y3); initgraph(&gd,&gm,"null"); small=minside(x1,y1,x2,y2,x3,y3); max=2+(int)small/20; triangle(x1,y1,x2,y2,x3,y3,0); while (!kbhit()); closegraph(); return 0; }
Draw a polygon. Use Bresenham line drawing algorithm

//Reference:- David F. Rogers "Procedural Elements for Computer Graphics" Second Edition #include<stdio.h> #include<graphics.h> #include<math.h> #define ROUND(a)((int)(a+0.5)) struct point { int x,y; }pol[10]; int n; void DDA(int,int,int,int); void Bresenham(int,int,int,int); void readpoly() { int i; for(i=0;i<n;i++) { printf("\nEnter the %d coordinates ",i+1); scanf("%d%d",&pol[i].x,&pol[i].y); } } void drawpolygonDDA() { int i; for(i=0;i<n;i++) DDA(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y); } void drawpolygonBres() { int i; for(i=0;i<n;i++) { delay(500); Bresenham(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y); } } int main() { int xa,ya,xb,yb,choice,gd=DETECT,gm=VGAMAX; printf("\nEnter the no of sides "); scanf("%d",&n); readpoly(); initgraph(&gd,&gm,NULL); printf("Press any key to draw traingle using Bresenham line drawing algortihm over the triangle drawn using DDA line drawing algorithm"); drawpolygonDDA(); getch(); drawpolygonBres(); while (!kbhit()); closegraph(); return 0; } void DDA(int xa,int ya,int xb,int yb) { int dx,dy,steps,k; dx=xb-xa; dy=yb-ya; float x=xa,y=ya,xinc,yinc; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; putpixel(ROUND(x),ROUND(y),5); for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(ROUND(x),ROUND(y),5); } } void Bresenham(int xa,int ya,int xb,int yb) { int i,dx=xb-xa,dy=yb-ya,p,twod,twodd,s; float m=2; if(dx!=0) m=(float)dy/(float)dx; if(m>=-1&&m<=1) { if(xa>xb) { Bresenham(xb,yb,xa,ya); return; } p=2*dx-dy; if(dy<0) { s=-1; dy=-dy; } else s=1; putpixel(xa,ya,10); for(i=0;i<dy;i++) { while(p>0) { xa++; p-=2*dy; putpixel(xa,ya,10); } ya+=s; p+=2*dx; putpixel(xa,ya,10); } } else { if(ya>yb) { Bresenham(xb,yb,xa,ya); return; } p=2*dy-dx; if(dx<0) { s=-1; dx=-dx; } else s=1; putpixel(xa,ya,10); for(i=0;i<dx;i++) { while(p>0) { ya++; p-=2*dx; putpixel(xa,ya,10); } xa+=s; p+=2*dy; putpixel(xa,ya,10); } } }
Tuesday, October 26, 2010
Draw a polygon. Use Bresenham/Midpoint line drawing algorithm

Write a program to draw a polygon. Use Bresenham/Midpoint line drawing algorithm to draw line.
Note:- Bresenham/Midpoint algorithm used here can be used to draw lines having any slope.
Note:- Bresenham/Midpoint algorithm used here can be used to draw lines having any slope.
#include<stdio.h> #include<graphics.h> #include<math.h> #define ROUND(a)((int)(a+0.5)) struct point { int x,y; }pol[10]; int n; void DDA(int,int,int,int); void Bresenham(int,int,int,int); void readpoly() { int i; for(i=0;i<n;i++) { printf("\nEnter the %d co=ordinates ",i+1); scanf("%d%d",&pol[i].x,&pol[i].y); } } void drawpolygonDDA() { int i; for(i=0;i<n;i++) DDA(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y); } void drawpolygonBres() { int i; for(i=0;i<n;i++) { delay(500); Bresenham(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y); } } int main() { int xa,ya,xb,yb,choice,gd=DETECT,gm=VGAMAX; printf("\nEnter the no of sides "); scanf("%d",&n); readpoly(); initgraph(&gd,&gm,NULL); printf("Press any key to draw traingle using Bresenham Midpoint line drawing algortihm over the triangle drawn using DDA line drawing algorithm"); drawpolygonDDA(); getch(); drawpolygonBres(); while (!kbhit()); closegraph(); return 0; } void DDA(int xa,int ya,int xb,int yb) { int dx,dy,steps,k; dx=xb-xa; dy=yb-ya; float x=xa,y=ya,xinc,yinc; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; putpixel(ROUND(x),ROUND(y),5); for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(ROUND(x),ROUND(y),5); } } void Bresenham(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya,p,twod,twodd; float m=2; if(dx!=0) m=(float)dy/(float)dx; if(m>=-1&&m<=1) { if(xa>xb) { Bresenham(xb,yb,xa,ya); return; } if(dy<0) { m=-1; dy=-dy; } else m=1; putpixel(xa,ya,10); p=2*dy-dx; twod=2*dy; twodd=2*dy-2*dx; while(xa<xb) { xa++; if(p<0) p+=twod; else { ya+=m; p+=twodd; } putpixel(xa,ya,10); } } else { if(ya>yb) { Bresenham(xb,yb,xa,ya); return; } if(dx<0) { m=-1; dx=-dx; } else m=1; putpixel(xa,ya,10); p=2*dx-dy; twod=2*dx; twodd=2*dx-2*dy; while(ya<yb) { ya++; if(p<0) p+=twod; else { xa+=m; p+=twodd; } putpixel(xa,ya,10); } } }
Monday, October 25, 2010
Window to viewport transformation

#include<stdio.h> #include<graphics.h> struct point { int x,y; } pol[10],wmin,wmax,vmin,vmax; int n; float trans[3][3],result[3][1],xandy[3][1]; void readpoly() { int i; for(i=0;i<n;i++) { printf("\nEnter the %d co=ordinates ",i+1); scanf("%d%d",&pol[i].x,&pol[i].y); } } void drawpolygon() { int i; for(i=0;i<n;i++) line(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y); } void mulmat() { int i,j,k; for(i=0;i<3;i++) { for(j=0;j<1;j++) { result[i][j]=0.0; for(k=0;k<3;k++) result[i][j]+=trans[i][k]*xandy[k][j]; } } } void transformpoly() { int i; for(i=0;i<n;i++) { xandy[0][0]=pol[i].x; xandy[1][0]=pol[i].y; xandy[2][0]=1; mulmat(); pol[i].x=result[0][0]; pol[i].y=result[1][0]; } } void makeIdentity() { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) trans[i][j]=0.0; trans[i][i]=1.0; } } int main() { int gd=DETECT,gm=VGAMAX; int choice,dx,dy; float sx,sy; printf("\nEnter the minx and miny of window "); scanf("%d%d",&wmin.x,&wmin.y); printf("\nEnter the maxx and maxy of window "); scanf("%d%d",&wmax.x,&wmax.y); printf("\nEnter the minx and miny of view port "); scanf("%d%d",&vmin.x,&vmin.y); printf("\nEnter the maxx and maxy of view port "); scanf("%d%d",&vmax.x,&vmax.y); while(1) { makeIdentity(); printf("\nMENU\n-------\n1.POLYGON\n2.EXIT"); printf("\nEnter the choice "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the no of sides "); scanf("%d",&n); readpoly(); initgraph(&gd,&gm,NULL); outtextxy(200,10,"Window"); rectangle(wmin.x,wmin.y,wmax.x,wmax.y); drawpolygon(); break; case 2: exit(0); default:printf("\nInvalid choice "); } getch(); cleardevice(); outtextxy(200,10,"View Port"); sx=(float)(vmax.x-vmin.x)/(float)(wmax.x-wmin.x); sy=(float)(vmax.y-vmin.y)/(float)(wmax.y-wmin.y); trans[0][0]=sx; trans[0][2]=(1-sx)*(float)wmin.x; trans[1][1]=sy; trans[1][2]=(1-sy)*(float)wmin.y; transformpoly(); dx=-wmin.x; dy=-wmin.y; makeIdentity(); trans[0][2]=dx; trans[1][2]=dy; transformpoly(); dx=vmin.x; dy=vmin.y; makeIdentity(); trans[0][2]=dx; trans[1][2]=dy; transformpoly(); setcolor(RED); rectangle(vmin.x,vmin.y,vmax.x,vmax.y); drawpolygon(); while (!kbhit()); closegraph(); } return 0; }
Sunday, October 24, 2010
Analog clock

#include<stdio.h> #include<graphics.h> #include<math.h> #include<time.h> #define round(a)((int)(a+0.5)) int main() { int gd=DETECT,gm=VGAMAX,sx=0,sy=0,mx=0,my=0,hx=0,hy=0,f=0; float hour,min; time_t now; struct tm *t; initgraph(&gd,&gm,"NULL"); setcolor(2); circle(300,300,100); setcolor(14); circle(300,300,110); setcolor(3); circle(300,300,120); do { time(&now); t=localtime(&now); if(f!=0) { setcolor(BLACK); line(300,300,sx,sy); line(300,300,mx,my); line(300,300,hx,hy); } hour=(t->tm_hour)%12+((float)t->tm_min/60.0); min=t->tm_min+((float)t->tm_sec/60.0); sx=300+round(90*cos(3.1412*(270+(6*t->tm_sec))/180)); sy=300+round(90*sin(3.1412*(270+(6*t->tm_sec))/180)); mx=300+round(90*cos(3.1412*(270+(6*min))/180)); my=300+round(90*sin(3.1412*(270+(6*min))/180)); hx=300+round(50*cos(3.1412*(270+(30*hour))/180)); hy=300+round(50*sin(3.1412*(270+(30*hour))/180)); outtextxy(294,380,"6"); outtextxy(380,300,"3"); outtextxy(293,220,"12"); outtextxy(220,300,"9"); setcolor(YELLOW); line(300,300,mx,my); setcolor(10); line(300,300,hx,hy); setcolor(RED); line(300,300,sx,sy); f=1; }while(!kbhit()); printf("Current time %d:%d:%d",t->tm_hour,t->tm_min,t->tm_sec); getch(); while(!kbhit()); closegraph(); return 0; }
Subscribe to:
Posts (Atom)