Advertisement
Dr4noel

Lab 4 - Algoritmi Geometrici

Apr 4th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include "graphics.h"
  2. #include <math.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7.  
  8. #define pi 3.14
  9.  
  10. float x_1, x_2, y_1, y_2, alfa = 3.1415 / 8;
  11. int xemax, yemax;
  12. float theta = pi / 36.f; //unghiul de rotatie, in radiani
  13. float xa, ya, xb, yb, xc, yc;//coord. Triunghiului
  14.  
  15. int xe(float x)
  16. // normalizarea coordonatei x
  17. {
  18.     return((int)floor((x - x_1) / (x_2 - x_1)*xemax));
  19. }
  20. int ze(float z)
  21. // normalizarea coordonatei y
  22. {
  23.     return((int)floor((y_2 - z) / (y_2 - y_1)*yemax));
  24. }
  25. void axe3D()
  26. {
  27.     setcolor(0);
  28.     line(xe(x_1 / 2), ze(0), xe(x_2), ze(0));
  29.     line(xe(0), ze(0), xe(0), ze(y_2));
  30.     line(xe(0), ze(0), xe(0 + x_1 * cos(alfa)), ze(0 + x_1 * sin(alfa)));
  31.     outtextxy(xe(0) - 15, ze(0) - 15, "O");
  32.     outtextxy(xe(x_2) - 20, ze(0) - 20, "x");
  33.     outtextxy(xe(x_2) - 6, ze(0) - 7, ">");
  34.     outtextxy(xe(0) + 15, ze(y_2) + 5, "z");
  35.     outtextxy(xe(0) - 1, ze(y_2) + 1, "^");
  36.     outtextxy(xe(x_1*cos(alfa)), ze(x_1*sin(alfa)), "y");
  37. }
  38. void linie3D(float x0, float y0, float z0, float x, float y, float z)
  39. {
  40.     line(xe(x0 - y0 * cos(alfa)), ze(z0 - y0 * sin(alfa)),
  41.         xe(x - y * cos(alfa)), ze(z - y * sin(alfa)));
  42. }
  43. typedef struct { float x, y, z; } punct;
  44. punct G[50][50];
  45.  
  46. float f(float x, float y)
  47. {
  48.     //return (0.2*sin(x*x + y * y));
  49.     //return (sqrt(16 - x * x - y * y));
  50.     //return pow((4 * x / y), 2) + pow(z, 2) - 1;
  51.     return (x * x - y * y);
  52. }
  53.  
  54. void deseneazaSuprafata()
  55. {
  56.     int i, j, n;
  57.     float h;
  58.     h = fabs(x_1) / 25; //dimensiunea unui patrat mic
  59.     n = 25; //numarul de linii ale gridului
  60.     for (i = 0; i < n; i++) //constructia gridului
  61.         for (j = 0; j < n; j++)
  62.         {
  63.             G[i][j].x = x_1 / 2.0 + i * h;
  64.             G[i][j].y = x_1 / 2.0 + j * h;
  65.             G[i][j].z = f(G[i][j].x, G[i][j].y);
  66.         }
  67.     setcolor(0); //desenare propriu-zisa
  68.     for (j = 1; j < n; j++)
  69.         for (i = 1; i < n; i++)
  70.         {
  71.             linie3D(G[i - 1][j].x, G[i - 1][j].y, G[i - 1][j].z, G[i][j].x,G[i][j].y, G[i][j].z);
  72.             linie3D(G[i][j - 1].x, G[i][j - 1].y, G[i][j - 1].z, G[i][j].x,G[i][j].y, G[i][j].z);
  73.         }
  74. }
  75. int main()
  76. {
  77.     printf("Limitele domeniului orizontal:\n");
  78.     printf("x_1="); scanf("%f", &x_1);
  79.     printf("x_2="); scanf("%f", &x_2);
  80.     y_1 = x_1; y_2 = x_2;
  81.     initwindow(800, 800, "Grafic suprafata: 2/10*sin(x^2+y^2)", 200, 200);
  82.     xemax = getmaxx(); yemax = getmaxy();
  83.     do {
  84.         setbkcolor(14);
  85.         cleardevice();
  86.         axe3D();
  87.         deseneazaSuprafata();
  88.  
  89.         for (int i = 0; i < 25; i++)
  90.             for (int j = 0; j < 25; j++) {
  91.                 G[i][j].x = (G[i][j].x * cos(theta) + G[i][j].y * sin(theta));
  92.                 G[i][j].y = (G[i][j].y * (-sin(theta)) + G[i][j].y * cos(theta));
  93.             }
  94.         Sleep(100);
  95.         theta += pi / 36.f;
  96.     } while (theta <= 2 * pi);
  97.     getchar(); getchar();
  98.     closegraph();
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement