sandipan

C++ program to demonstrate geometric operations

Sep 6th, 2018
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. /*
  2.  C/C++ code for a computer graphics program to implement a few geometric operations (sclaing, translation, rotation etc.)
  3.  Sandipan Dey
  4.  BCSE, JU, Kolkata
  5.  2002
  6. */
  7.  
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <conio.h>
  11. #include <graphics.h>
  12. #include <dos.h>
  13. #define SCALEFACTOR 0.5
  14. #define MAX 3
  15. #define PI 2.14159
  16. #define DIR 1
  17.  
  18. void MatMult(int m,int n,int p,double a[MAX][MAX],double b[MAX][MAX],double c[MAX][MAX]) {
  19.     for(int i=0;i<m;++i)
  20.         for(int j=0;j<p;++j) {
  21.             c[i][j]=0;
  22.             for(int k=0;k<n;++k)
  23.                 c[i][j]+=a[i][k]*b[k][j];
  24.         }
  25. }
  26.  
  27. void Name() {
  28.     rectangle(515,400,630,450);
  29.     outtextxy(520,410,"SANDIPAN DEY");
  30.     outtextxy(520,420,"BCSE-IV");
  31.     outtextxy(520,430,"Roll-99710");
  32. }
  33.  
  34. struct Point {
  35.     int x,y;
  36. };
  37.  
  38. class xyPlane {
  39.  
  40.     Point Origin;
  41.     public:
  42.     xyPlane() {
  43.         Origin.x=Origin.y=0;
  44.     }
  45.     xyPlane(int xo,int yo) {
  46.         Origin.x=xo;
  47.         Origin.y=yo;
  48.     }
  49.     void DrawAxis() {
  50.         int xo,yo;
  51.         xo=Origin.x;
  52.         yo=Origin.y;
  53.         line(xo-300,yo,xo+300,yo);
  54.         line(xo,yo-220,xo,yo+220);
  55.         outtextxy(xo-10,yo+5,"O");
  56.         outtextxy(xo+305,yo-2,"X");
  57.         outtextxy(xo-4,yo-230,"Y");
  58.     }
  59.     void Line(int x1,int y1,int x2,int y2,int color=getmaxcolor()) {
  60.         int xo,yo;
  61.         xo=Origin.x;
  62.         yo=Origin.y;
  63.         setcolor(color);
  64.         line(xo+x1,yo-y1,xo+x2,yo-y2);
  65.         setcolor(getmaxcolor());
  66.     }
  67.     void Triangle(int x1,int y1,int x2,int y2,int x3,int y3,int color=getmaxcolor()) {
  68.         Line(x1,y1,x2,y2,color);
  69.         Line(x2,y2,x3,y3,color);
  70.         Line(x3,y3,x1,y1,color);
  71.     }
  72. };
  73.  
  74. void main()
  75. {
  76.     clrscr();
  77.     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}};
  78.     T1[0][0]=T1[1][1]=0;          //90 DEGREE ANTICLOCKWISE ROTATION
  79.     T1[0][1]=1;T1[1][0]=-1;
  80.     int ch;
  81.     printf("\nEnter Coordinates Of The Triangle:\n");
  82.     for(int i=0;i<3;++i) {
  83.         printf("(X%d,Y%d): ",i+1,i+1);
  84.         for(int j=0;j<2;++j)
  85.             scanf("%lf",&I[i][j]);
  86.     }
  87.     MatMult(3,2,2,I,T1,O);
  88.     T1[0][0]=T1[1][1]=T1[2][2]=1;
  89.     T1[0][1]=T1[0][2]=T1[1][0]=T1[1][2]=0;
  90.     T1[2][0]=-(O[0][0]+O[1][0]+O[2][0])/3;
  91.     T1[2][1]=-(O[0][1]+O[1][1]+O[2][1])/3;
  92.     S[0][0]=S[1][1]=sqrt(SCALEFACTOR);
  93.     S[0][1]=S[0][2]=S[1][0]=S[1][2]=S[2][0]=S[2][1]=0;
  94.     S[2][2]=1;
  95.     MatMult(3,3,3,T1,S,T2);
  96.     T1[2][0]=-T1[2][0];
  97.     T1[2][1]=-T1[2][1];
  98.     MatMult(3,3,3,T2,T1,S);
  99.     O[0][2]=O[1][2]=O[2][2]=1;
  100.     MatMult(3,3,3,O,S,O1);
  101.     getch();
  102.     int gd=DETECT,gm;
  103.     initgraph(&gd,&gm,"");
  104.     xyPlane pt(getmaxx()/2,getmaxy()/2);
  105.     pt.DrawAxis();
  106.     pt.Triangle(I[0][0],I[0][1],I[1][0],I[1][1],I[2][0],I[2][1],12);
  107.     getch();
  108.     pt.Triangle(O[0][0],O[0][1],O[1][0],O[1][1],O[2][0],O[2][1],9);
  109.     Name();
  110.     getch();
  111.     pt.Triangle(I[0][0],I[0][1],I[1][0],I[1][1],I[2][0],I[2][1],12);
  112.     pt.Triangle(O1[0][0],O1[0][1],O1[1][0],O1[1][1],O1[2][0],O1[2][1]);
  113.     getch();
  114.     cleardevice();
  115.     pt.DrawAxis();
  116.     pt.Triangle(I[0][0],I[0][1],I[1][0],I[1][1],I[2][0],I[2][1],14);
  117.     for(double x=0;!kbhit();x+=PI/360)  {
  118.         T1[0][0]=cos(x);
  119.         T1[0][1]=DIR*sin(x);
  120.         T1[1][0]=DIR*(-sin(x));
  121.         T1[1][1]=cos(x);
  122.         pt.Triangle(O[0][0],O[0][1],O[1][0],O[1][1],O[2][0],O[2][1],0);
  123.         MatMult(3,2,2,I,T1,O);
  124.         pt.Triangle(O[0][0],O[0][1],O[1][0],O[1][1],O[2][0],O[2][1],14);
  125.         pt.DrawAxis();
  126.         Name();
  127.         delay(10);
  128.     }
  129.     getch();
  130.     closegraph();
  131. }
Add Comment
Please, Sign In to add comment