Advertisement
frentzy

Lab desen nr 3

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