icatalin

tema 3.2 abcd [GRAFICA]

Mar 17th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include <windows.h>
  2. #include <gl/freeglut.h>
  3. #include <cmath>
  4. #include <iostream>
  5.  
  6. GLint winWidth = 800, winHeight = 800;
  7. GLfloat xx0 = 13.0, yy0 = 11.0, zz0 = 14.0;
  8. GLfloat xref = 3.0, yref = 1.0, zref = 4.0;
  9. GLfloat Vx = 8.0, Vy = 0.0, Vz = -8.0;
  10.  
  11. GLfloat xwMin = -10.0, ywMin = -10.0, xwMax = 10.0, ywMax = 10.0;
  12.  
  13. GLfloat dnear = 0.0, dfar = 20.0;
  14.  
  15. void init(void)
  16. {
  17.     glClearColor(1.0, 1.0, 1.0, 0.0);
  18.  
  19.     glMatrixMode(GL_MODELVIEW);
  20.     glLoadIdentity();
  21.     gluLookAt(xx0, yy0, zz0, xref, yref, zref, Vx, Vy, Vz);
  22.  
  23.     glMatrixMode(GL_PROJECTION);
  24.     glLoadIdentity();
  25.     glOrtho(xwMin, xwMax, ywMin, ywMax, dnear, dfar);
  26. }
  27.  
  28.  
  29.  
  30. class Punct
  31. {
  32. public:
  33.     float x, y, z;
  34.  
  35.     Punct(int xx, int yy, int zz)
  36.     {
  37.         x = xx;
  38.         y = yy;
  39.         z = zz;
  40.     }
  41.  
  42.     Punct()
  43.     {
  44.         x = y = z = 0;
  45.     }
  46. };
  47.  
  48. void desen()
  49. {
  50.  
  51.     Punct A(3, 1, 0), B(-2, 2, 4), C(0, 0, 4);
  52.  
  53.     glLineWidth(3.0);
  54.     glPointSize(3.0);
  55.    
  56.     //a) determinati punctul M astfel incat sa fie in interiorul triunghiului ABC sau pe laturi
  57.     Punct M;
  58.     M.x = (B.x + C.x) / 2;
  59.     M.y = (B.y + C.y) / 2;
  60.     M.z = (B.z + C.z) / 2;
  61.  
  62.     glPointSize(10.0);
  63.     glBegin(GL_POINTS);
  64.     glColor3f(1.0, 0.5, 0.5);
  65.     glVertex3i(M.x, M.y, M.z);
  66.     glEnd();
  67.  
  68.     //b) deteminati D in planul triunghiului asa incat ABDC(atentie la ordine!!!) sa fie un patrulater convex
  69.  
  70.     Punct D; //am construit punctul D a.i. sa fie simetricul varfului A
  71.     D.x = 2 * M.x - A.x;
  72.     D.y = 2 * M.y - A.y;
  73.     D.z = 2 * M.z - A.z;
  74.  
  75.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  76.     glBegin(GL_POLYGON);
  77.     glColor3f(1.0, 0.0, 0.0);
  78.     glVertex3i(A.x, A.y, A.z);
  79.     glColor3f(0.0, 1.0, 0.0);
  80.     glVertex3i(B.x, B.y, B.z);
  81.     glColor3f(1.0, 0.0, 1.0);
  82.     glVertex3i(D.x, D.y, D.z);
  83.     glColor3f(0.0, 0.0, 1.0);
  84.     glVertex3i(C.x, C.y, C.z);
  85.     glEnd();
  86.  
  87.     //c) construiti normala n la patrulaterul ABDC
  88.  
  89.     Punct Normala;
  90.     Normala.x = (A.y * B.z + B.y * C.z + C.y * A.z) - (C.y * B.z + A.y * C.z + B.y * A.z);
  91.     Normala.y = (C.x * B.z + A.x * C.z + B.x * A.z) - (A.x * B.z + B.x * C.z + C.x * A.z);
  92.     Normala.z = (A.x * B.y + B.x * C.y + C.x * A.y) - (C.x * B.y + A.x * C.y + B.x * A.y);
  93.     float radical = std::sqrt(Normala.x * Normala.x + Normala.y * Normala.y + Normala.z * Normala.z);
  94.     Normala.x = Normala.x / radical;
  95.     Normala.y = Normala.y / radical;
  96.     Normala.z = Normala.z / radical;
  97.  
  98.     std::cout << "Normala la patrulater este n = " << Normala.x << "i + " << Normala.y << "j + "<< Normala.z << "k.\n";
  99.  
  100.     //d) folosind M si n construiti un punct O1 in fata patrulaterului si un punct O2 in spatele acestuia.
  101.     Punct O1(M.x * 7 * Normala.x, M.y * 7 * Normala.y, M.z * 7 * Normala.z);
  102.     Punct O2(M.x * (-7) * Normala.x, M.y * (-7) * Normala.y, M.z * (-7) * Normala.z);
  103.  
  104.     std::cout << "Punctul O1 de coordonate" << O1.x << "," << O1.y << ", " << O1.z << "\n";
  105.     std::cout << "Punctul O2 de coordonate" << O2.x << "," << O2.y << ", " << O2.z << "\n";
  106.  
  107.  
  108.     glFlush();
  109. }
  110.  
  111. void main(int argc, char** argv)
  112. {
  113.     glutInit(&argc, argv);
  114.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  115.     glutInitWindowPosition(50, 50);
  116.     glutInitWindowSize(winWidth, winHeight);
  117.     glutCreateWindow("Poligoane in context 3D. Fata si spatele unui poligon");
  118.  
  119.     init();
  120.     glClear(GL_COLOR_BUFFER_BIT);
  121.     glutDisplayFunc(desen);
  122.     glutMainLoop();
  123. }
Advertisement
Add Comment
Please, Sign In to add comment