Mahmoud_Hawara

Untitled

May 23rd, 2022
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include "graphics.h"
  3. #include <charconv>
  4. using namespace std;
  5.  
  6. const int W=1200,H=700;
  7. const double PI = acos(-1);
  8. float a0 = 0, degree, fp;
  9. int p, t;
  10. vector<float>v;
  11. vector<pair<float,float>>ab;
  12.  
  13. float rnd(float var)
  14. {
  15.     float value = (int)(var * 100 + .5);
  16.     return (float)value / 100;
  17. }
  18.  
  19. void drawline(int x,int y,int x2,int y2,int color,int textx,int texty,char* name)
  20. {
  21.      moveto(x,y);
  22.      setcolor(color);
  23.      lineto(x2,y2);
  24.      outtextxy(textx,texty,name);
  25.      return;
  26. }
  27.  
  28. void scalex(int step)
  29. {
  30.      for(int i=step+W/2;i<W-30;i+=step)
  31.      {
  32.          char num_char[5 + sizeof(char)];
  33.          to_chars(num_char, num_char + 5, i-W/2);
  34.          outtextxy(i-sizeof(num_char),H/2+5,num_char);
  35.      }
  36.      for(int i=W/2-step;i>0;i-=step)
  37.      {
  38.          char num_char[5 + sizeof(char)];
  39.          to_chars(num_char, num_char + 5, i-W/2);
  40.          outtextxy(i-sizeof(num_char),H/2+5,num_char);
  41.      }
  42.      return;
  43. }
  44.  
  45. void scaley(int step)
  46. {
  47.      for(int i=-step+H/2;i>0;i-=step)
  48.      {
  49.          char num_char[20 + sizeof(char)]="";
  50.          to_chars(num_char, num_char + 3,-1*(i-H/2)/2);
  51.          outtextxy(W/2-30,i,num_char);
  52.      }
  53.      for(int i=H/2+step;i<H;i+=step)
  54.      {
  55.          char num_char[5 + sizeof(char)]="";
  56.          to_chars(num_char, num_char + 5, -1*(i-H/2)/2);
  57.          outtextxy(W/2-30,i,num_char);
  58.      }
  59.      return;
  60. }
  61.  
  62. void getVolts()
  63. {
  64.     cout << "\nEnter The Number Of Samples : ";
  65.     cin >> p;
  66.     degree = 360 / p;
  67.     cout << "\nEnter The Degree of The First Theta : ";
  68.     cin >> fp;
  69.     cout << "\nYou will Enter " << p << " values one for each " << degree << " Degree\n";
  70.     for(int i = 1;i <= p; i++)
  71.     {
  72.          cout << "\n\t>> Enter value number " << i << " at " << fp + (i - 1) * degree << " Degree : ";
  73.          float volt;
  74.          cin>>volt;
  75.          v.push_back(volt);
  76.     }
  77.     return;
  78. }
  79.  
  80. void Draw_XY_Axis()
  81. {
  82.      initwindow(W, H);
  83.      moveto(W / 2, H / 2);
  84.      // X axis
  85.      drawline(0, H / 2, W, H / 2, WHITE, W - 40, H / 2 + 10, "theta");
  86.      // Y axis
  87.      drawline(W / 2, 0, W / 2, H, WHITE, W / 2 + 10, 0, "V");
  88.      outtextxy(W - 50, H / 2 + 30, "(degree)");
  89.      outtextxy(W / 2 + 10, 15, "(volt)");
  90.      scalex(60);
  91.      scaley(30);
  92.      return;
  93. }
  94.  
  95. void getFourierCofficients()
  96. {
  97.     for(auto volt : v)a0 += volt;
  98.     for(int i = 1; i < 4; i++)
  99.     {
  100.          float an = 0,bn = 0;
  101.          for(int j = 1; j <= p; j++)
  102.          {
  103.              an += v[j - 1] * cos((fp + (i - 1) * degree) * j * PI / 180);
  104.              bn += v[j - 1] * sin((fp + (i - 1) * degree) * j * PI / 180);
  105.          }
  106.          an = an * 2 / p;
  107.          bn = bn * 2 / p;
  108.          ab.push_back({an,bn});
  109.     }
  110.     a0 /= p;
  111.     return;
  112. }
  113.  
  114. void printFourierCofficientsOnConsole()
  115. {
  116.     cout << '\n';
  117.     for (int i = 0; i <= 20; i++)cout << '-';
  118.     cout << '\n';
  119.     cout << "Fourier Cofficients : \n";
  120.     cout << "\t>>a0 = " << a0 << '\n';
  121.     cout << "\t>>a1 - " << ab[0].first << '\n';
  122.     cout << "\t>>a2 = " << ab[1].first << '\n';
  123.     cout << "\t>>a3 = " << ab[2].first << '\n';
  124.     cout << "\t>>b1 = " << ab[0].second << '\n';
  125.     cout << "\t>>b2 = " << ab[1].second << '\n';
  126.     cout << "\t>>b3 = " << ab[2].second << '\n';
  127.     for (int i = 0; i <= 20; i++)cout << '-';
  128.     cout << '\n';
  129.     return;
  130. }
  131.  
  132. void DrawTheSignal()
  133. {
  134.      moveto(W / 2, -v[11] + H / 2);
  135.      for(float i = 0.1; i <= 360;i += 0.1)
  136.      {
  137.          float y = a0;
  138.          for(int m = 0; m < ab.size(); m++)
  139.          {
  140.              float var = ab[m].first * cos((m + 1) * i * PI / 180);
  141.              y += rnd(var);
  142.              var = ab[m].second * sin((m + 1) * i * PI / 180);
  143.              y += rnd(var);
  144.          }
  145.          lineto(i + W / 2, (-y * 2 + H / 2));
  146.      }
  147.      return;
  148. }
  149.  
  150. void printFx()
  151. {
  152.      string str = "v = " + to_string(a0) + " + " + to_string(ab[0].first) + "cos(th) + " + to_string(ab[1].first) + "cos(2*th)+ " + to_string(ab[2].first) + "cos(3*th) .....";
  153.      char *c = new char[110];
  154.      copy(str.begin(), str.end(), c);
  155.      c[str.size()] = '\0';
  156.      outtextxy(10, 20, c);
  157.      str = " + " + to_string(ab[0].second) + "sin(th) + " + to_string(ab[1].second) + "sin(2*th) + " + to_string(ab[2].second) + "sin(3*th) .....";
  158.      copy(str.begin(), str.end(), c);
  159.      c[str.size()] = '\0';
  160.      outtextxy(10, 40, c);
  161.      return;
  162. }
  163.  
  164. void goToDraw()
  165. {
  166.      getVolts();
  167.  
  168.      getFourierCofficients();
  169.  
  170.      printFourierCofficientsOnConsole();
  171.  
  172.      Draw_XY_Axis();
  173.  
  174.      DrawTheSignal();
  175.  
  176.      printFx();
  177.  
  178.      cout << "\n\n\n\t\t\t\tEnter 0 to go to the Next Signal\n";
  179.  
  180.      while (cin.get() != '0');
  181.  
  182.      return;
  183. }
  184.  
  185. void reset()
  186. {
  187.     system("cls");
  188.     a0 = 0;
  189.     v.clear();
  190.     ab.clear();
  191. }
  192.  
  193. int main()
  194. {
  195.      cout << "\t\t\tEnter The Number of Signals You want to draw : ";
  196.      cin >> t;
  197.  
  198.      for (int i = 1; i <= t; i++)
  199.      {
  200.         reset();
  201.  
  202.         cout << "\t\t\t\t\t\t Signla Number " << i << '\n';
  203.  
  204.         goToDraw();
  205.      }
  206.      return 0;
  207. }
  208.  
Advertisement
Add Comment
Please, Sign In to add comment