Graphics programming using c
Home
Question Paperz
Java Projectz
ASP Projectz
8086
Data structures
c++
8085
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.
#include<stdio.h> #include<graphics.h> struct point { int x,y; }pol[3],polnew[3],circ,cent; int n,r; float trans[3][3],result[3][1],xandy[3][1]; void plot(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 drawpolygon() { int i; for(i=0;i<3;i++) line(pol[i].x,pol[i].y,pol[(i+1)%3].x,pol[(i+1)%3].y); } void drawnewpolygon() { int i; for(i=0;i<3;i++) line(polnew[i].x,polnew[i].y,polnew[(i+1)%3].x,polnew[(i+1)%3].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<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 transformnewpoly() { int i; for(i=0;i<3;i++) { xandy[0][0]=polnew[i].x; xandy[1][0]=polnew[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); } int main() { int i,j,gd=DETECT,gm=VGAMAX; printf("\nEnter the sides of triangle "); for(i=0;i<3;i++) { printf("\nEnter the %d co=ordinates ",i+1); scanf("%d%d",&pol[i].x,&pol[i].y); } printf("Enter the orgin "); scanf("%d%d",&circ.x,&circ.y); cent.x=(pol[0].x+pol[1].x+pol[2].x)/3; cent.y=(pol[0].y+pol[1].y+pol[2].y)/3; r=sqrt((circ.x-cent.x)*(circ.x-cent.x)+(circ.y-cent.y)*(circ.y-cent.y)); initgraph(&gd,&gm,"NULL"); midpoint(circ.x,circ.y,r); drawpolygon(); putpixel(cent.x,cent.y,5); makeIdentity(); i=1; j=1; do{ midpoint(circ.x,circ.y,r); drawnewpolygon(); rotate(cent.x,cent.y,i); transformpoly(); rotate(circ.x,circ.y,j); transformnewpoly(); delay(40); i=(i+10)%360; j=(j+5)%360; cleardevice(); }while(!kbhit()); closegraph(); return 0; }
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment