Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- C/C++ code for a computer graphics program to implement a few geometric operations (sclaing, translation, rotation etc.)
- Sandipan Dey
- BCSE, JU, Kolkata
- 2002
- */
- #include <math.h>
- #include <stdio.h>
- #include <conio.h>
- #include <graphics.h>
- #include <dos.h>
- #define SCALEFACTOR 0.5
- #define MAX 3
- #define PI 2.14159
- #define DIR 1
- void MatMult(int m,int n,int p,double a[MAX][MAX],double b[MAX][MAX],double c[MAX][MAX]) {
- for(int i=0;i<m;++i)
- for(int j=0;j<p;++j) {
- c[i][j]=0;
- for(int k=0;k<n;++k)
- c[i][j]+=a[i][k]*b[k][j];
- }
- }
- void Name() {
- rectangle(515,400,630,450);
- outtextxy(520,410,"SANDIPAN DEY");
- outtextxy(520,420,"BCSE-IV");
- outtextxy(520,430,"Roll-99710");
- }
- struct Point {
- int x,y;
- };
- class xyPlane {
- Point Origin;
- public:
- xyPlane() {
- Origin.x=Origin.y=0;
- }
- xyPlane(int xo,int yo) {
- Origin.x=xo;
- Origin.y=yo;
- }
- void DrawAxis() {
- int xo,yo;
- xo=Origin.x;
- yo=Origin.y;
- line(xo-300,yo,xo+300,yo);
- line(xo,yo-220,xo,yo+220);
- outtextxy(xo-10,yo+5,"O");
- outtextxy(xo+305,yo-2,"X");
- outtextxy(xo-4,yo-230,"Y");
- }
- void Line(int x1,int y1,int x2,int y2,int color=getmaxcolor()) {
- int xo,yo;
- xo=Origin.x;
- yo=Origin.y;
- setcolor(color);
- line(xo+x1,yo-y1,xo+x2,yo-y2);
- setcolor(getmaxcolor());
- }
- void Triangle(int x1,int y1,int x2,int y2,int x3,int y3,int color=getmaxcolor()) {
- Line(x1,y1,x2,y2,color);
- Line(x2,y2,x3,y3,color);
- Line(x3,y3,x1,y1,color);
- }
- };
- void main()
- {
- clrscr();
- double I[MAX][MAX]={{0}},T1[MAX][MAX]={{0}},T2[MAX][MAX]={{0}},O[MAX][MAX]={{0}},S[MAX][MAX]={{0}},O1[MAX][MAX]={{0}};
- T1[0][0]=T1[1][1]=0; //90 DEGREE ANTICLOCKWISE ROTATION
- T1[0][1]=1;T1[1][0]=-1;
- int ch;
- printf("\nEnter Coordinates Of The Triangle:\n");
- for(int i=0;i<3;++i) {
- printf("(X%d,Y%d): ",i+1,i+1);
- for(int j=0;j<2;++j)
- scanf("%lf",&I[i][j]);
- }
- MatMult(3,2,2,I,T1,O);
- T1[0][0]=T1[1][1]=T1[2][2]=1;
- T1[0][1]=T1[0][2]=T1[1][0]=T1[1][2]=0;
- T1[2][0]=-(O[0][0]+O[1][0]+O[2][0])/3;
- T1[2][1]=-(O[0][1]+O[1][1]+O[2][1])/3;
- S[0][0]=S[1][1]=sqrt(SCALEFACTOR);
- S[0][1]=S[0][2]=S[1][0]=S[1][2]=S[2][0]=S[2][1]=0;
- S[2][2]=1;
- MatMult(3,3,3,T1,S,T2);
- T1[2][0]=-T1[2][0];
- T1[2][1]=-T1[2][1];
- MatMult(3,3,3,T2,T1,S);
- O[0][2]=O[1][2]=O[2][2]=1;
- MatMult(3,3,3,O,S,O1);
- getch();
- int gd=DETECT,gm;
- initgraph(&gd,&gm,"");
- xyPlane pt(getmaxx()/2,getmaxy()/2);
- pt.DrawAxis();
- pt.Triangle(I[0][0],I[0][1],I[1][0],I[1][1],I[2][0],I[2][1],12);
- getch();
- pt.Triangle(O[0][0],O[0][1],O[1][0],O[1][1],O[2][0],O[2][1],9);
- Name();
- getch();
- pt.Triangle(I[0][0],I[0][1],I[1][0],I[1][1],I[2][0],I[2][1],12);
- pt.Triangle(O1[0][0],O1[0][1],O1[1][0],O1[1][1],O1[2][0],O1[2][1]);
- getch();
- cleardevice();
- pt.DrawAxis();
- pt.Triangle(I[0][0],I[0][1],I[1][0],I[1][1],I[2][0],I[2][1],14);
- for(double x=0;!kbhit();x+=PI/360) {
- T1[0][0]=cos(x);
- T1[0][1]=DIR*sin(x);
- T1[1][0]=DIR*(-sin(x));
- T1[1][1]=cos(x);
- pt.Triangle(O[0][0],O[0][1],O[1][0],O[1][1],O[2][0],O[2][1],0);
- MatMult(3,2,2,I,T1,O);
- pt.Triangle(O[0][0],O[0][1],O[1][0],O[1][1],O[2][0],O[2][1],14);
- pt.DrawAxis();
- Name();
- delay(10);
- }
- getch();
- closegraph();
- }
Add Comment
Please, Sign In to add comment