#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; }
Monday, October 25, 2010
Window to viewport transformation
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; }
Wednesday, October 13, 2010
Draw the following figure(1)
#include<stdio.h> #include<graphics.h> struct point { int x,y; }pol[3],polnew[3],circ,insid[12]; int n,r; float trans[3][3],result[3][1],xandy[3][1]; void plot(int,int,int,int); void boundaryfill4(int,int,int,int); 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 drawpol() { int i; for(i=0;i<2;i++) line(pol[i].x,pol[i].y,pol[(i+1)].x,pol[(i+1)].y); } void drawnewpol() { int i; for(i=0;i<2;i++) line(polnew[i].x,polnew[i].y,polnew[(i+1)].x,polnew[(i+1)].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 transformpoint(int x,int y) { xandy[0][0]=x; xandy[1][0]=y; xandy[2][0]=1; mulmat(); } void transformpoly() { int i; for(i=0;i<3;i++) { xandy[0][0]=pol[i].x; xandy[1][0]=pol[i].y; xandy[2][0]=1; mulmat(); polnew[i].x=result[0][0]; polnew[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 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); } 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); } } int main() { int i,j,angle=0,gd=DETECT,gm=VGAMAX,ch; int color[]={1,14,3,4,5,6,7,8,9,11,12,13}; printf("Enter the orgin "); scanf("%d%d",&circ.x,&circ.y); printf("Enter the radius "); scanf("%d",&r); do{ printf("\nMENU\n-------\n1.Enter inside points to fill\n 2.Take the inside points automatically"); printf("\nEnter ur choice "); scanf("%d",&ch); }while(ch>2&&ch<1); if(ch==1) { printf("\nEnter the inside point of circle "); scanf("%d%d",&insid[0].x,&insid[0].y); for(i=1;i<12;i++) { printf("\nEnter the inside point %d ",i); scanf("%d%d",&insid[i].x,&insid[i].y); } } initgraph(&gd,&gm,"NULL"); setcolor(10); midpoint(circ.x,circ.y,r); makeIdentity(); j=360/11; pol[0].x=circ.x; pol[0].y=circ.y+r; pol[1].x=pol[0].x; pol[1].y=pol[0].y+50; rotate(circ.x,circ.y,j); transformpoint(pol[0].x,pol[0].y); pol[2].x=result[0][0]; pol[2].y=result[1][0]; rotate(circ.x,circ.y,360/22); transformpoint(pol[1].x,pol[1].y); pol[1].x=result[0][0]; pol[1].y=result[1][0]; if(ch==2) { insid[0].x=circ.x; insid[0].y=circ.y; insid[1].x=(pol[0].x+pol[1].x+pol[2].x)/3; insid[1].y=(pol[0].y+pol[1].y+pol[2].y)/3; } boundaryfill4(insid[0].x,insid[0].y,color[0],10); drawpol(); boundaryfill4(insid[1].x,insid[1].y,color[1],10); for(i=0;i<10;i++) { angle+=j; rotate(circ.x,circ.y,angle); transformpoly(); drawnewpol(); if(ch==2) { insid[i+2].x=(polnew[0].x+polnew[1].x+polnew[2].x)/3; insid[i+2].y=(polnew[0].y+polnew[1].y+polnew[2].y)/3; } boundaryfill4(insid[i+2].x,insid[i+2].y,color[i+2],10); } while(!kbhit()); closegraph(); return 0; }
Tuesday, October 12, 2010
Rotate a triangle along the circumference of a circle, also rotate the triangle with reference to its centroid
Rotate a triangle along the circumference of a circle, also rotate the triangle with reference to its centroid.
Use midpoint algorithm for circle drawing.
Use midpoint algorithm for circle drawing.
Tuesday, October 5, 2010
Monday, October 4, 2010
Subscribe to:
Posts (Atom)