Guest User

Untitled

a guest
Apr 8th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.36 KB | None | 0 0
  1. /*
  2. Optimum Theory - Gary Lee
  3. c++ / sfml implementation - James Liam Keough (just put this name in whatever history, dont contact it)
  4.  
  5. Display:
  6. left - actual data
  7. middle - change in data
  8. right - local maximums
  9.  
  10. Controls:
  11. OP - change color filter
  12. [] - change the frequency that the change array is calculated and the image is drawn (exponential, dont go too high!)
  13. Up/Down/PageUP/PageDown - change scale from 7.00..1 to 8
  14. Hold Space - Pause
  15. R - reset to random
  16. T - reset to black hole
  17. I - show current iteration
  18. */
  19.  
  20.  
  21. #include <SFML/Graphics.hpp>//RenderWindow, VertexArray
  22. #include <iostream>
  23. #include <stdlib.h>//srand, rand
  24. #include <time.h>//time
  25. int main()
  26. {
  27.     const int wSize = 400;
  28.     const int wArea = wSize * wSize;
  29.  
  30.     float averageDivide = 7.0001f;
  31.     const float noiseScale = 0.01f;
  32.  
  33.     sf::RenderWindow window(sf::VideoMode(wSize * 3, wSize * 1), "universe2D");
  34.  
  35.     float e[wArea] = {0};//energy
  36.     float en[wArea] = {0};//next energy
  37.     float c[wArea] = {0};//change
  38.     float cd[wArea] = {0};//directional change
  39.  
  40.     sf::VertexArray filterValue(sf::Points, wArea);
  41.     for(int x = 0; x < wSize; x++)
  42.         for(int y = 0; y < wSize; y++)
  43.             filterValue[x + y * wSize].position = sf::Vector2f(x, y);
  44.     sf::VertexArray filterChange(sf::Points, wArea);
  45.     for(int x = 0; x < wSize; x++)
  46.         for(int y = 0; y < wSize; y++)
  47.             filterChange[x + y * wSize].position = sf::Vector2f(x + wSize, y);
  48.     sf::VertexArray filterChangeDirection(sf::Points, wArea);
  49.     for(int x = 0; x < wSize; x++)
  50.         for(int y = 0; y < wSize; y++)
  51.             filterChangeDirection[x + y * wSize].position = sf::Vector2f(x + (wSize << 1), y);
  52.  
  53.     char drawColor = 3;
  54.  
  55.     unsigned int iteration = 0;
  56.     unsigned int drawFreq = 16;
  57.  
  58.     while(window.isOpen())
  59.     {
  60.         sf::Event event;
  61.         while(window.pollEvent(event))
  62.         {
  63.             switch(event.type)
  64.             {
  65.             case sf::Event::Closed:    window.close(); break;
  66.             case sf::Event::KeyPressed:
  67.                 switch(event.key.code)
  68.                 {
  69.                 case sf::Keyboard::R:
  70.                     srand (time(NULL));
  71.                     for(int i = 0; i < wArea; i++)
  72.                         e[i] = en[i] = (float)(rand() - RAND_MAX / 2 - 1) * 100.0f;
  73.                     iteration = 0;
  74.                     break;
  75.                 case sf::Keyboard::T:
  76.                     srand (time(NULL));
  77.                     for(int i = 0; i < wArea; i++)
  78.                         e[i] = en[i] = 0;
  79.                     e[wSize / 2 + wArea / 2] = en[wSize / 2 + wArea / 2] = -100000000000;
  80.                     iteration = 0;
  81.                     break;
  82.                 case sf::Keyboard::O:   drawColor ^= 1; break;
  83.                 case sf::Keyboard::P:   drawColor ^= 2; break;
  84.                 case sf::Keyboard::LBracket:
  85.                     if(drawFreq < 2) break;
  86.                     drawFreq >>= 1;
  87.                     std::cout << "Draw Frequency: " << drawFreq << std::endl;
  88.                     break;
  89.                 case sf::Keyboard::RBracket:
  90.                     drawFreq <<= 1;
  91.                     std::cout << "Draw Frequency: " << drawFreq << std::endl;
  92.                     break;
  93.                 case sf::Keyboard::I:
  94.                     std::cout << "Iteration: " << iteration << std::endl;
  95.                     break;
  96.                 }
  97.                 break;
  98.             }
  99.         }
  100.  
  101.         {//controls
  102.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  103.             if(averageDivide < 7.99999)
  104.             {
  105.                 averageDivide += 0.00001f;
  106.                 std::cout << "Scale:" << averageDivide - 7 << std::endl;
  107.             }
  108.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  109.             if(averageDivide > 7.00001)
  110.             {
  111.                 averageDivide -= 0.00001f;
  112.                 std::cout << "Scale:" << averageDivide - 7 << std::endl;
  113.             }
  114.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::PageUp))
  115.             if(averageDivide < 7.999)
  116.             {
  117.                 averageDivide += 0.001f;
  118.                 std::cout << "Scale:" << averageDivide - 7 << std::endl;
  119.             }
  120.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::PageDown))
  121.             if(averageDivide > 7.001)
  122.             {
  123.                 averageDivide -= 0.001f;
  124.                 std::cout << "Scale:" << averageDivide - 7 << std::endl;
  125.             }
  126.         }
  127.         if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  128.         {
  129.             for(int i = 0; i < wArea; i++)
  130.                 c[i] = e[i];
  131.  
  132.             for(int d = 0; d < drawFreq; d++)
  133.             {
  134.                 if(true)
  135.                 { //x y style
  136.                 for(unsigned int x = 0; x < wSize; x++)
  137.                     for(unsigned int y = 0; y < wSize; y++)
  138.                     {
  139.                         unsigned int my = ((y - 1) % wSize) * wSize;
  140.                         unsigned int py = ((y + 1) % wSize) * wSize;
  141.                         unsigned int mx = (x - 1) % wSize;
  142.                         unsigned int px = (x + 1) % wSize;
  143.                         en[x + y * wSize] =
  144.                             (e[x + y * wSize] +
  145.                              e[mx + y * wSize] +
  146.                              e[px + y * wSize] +
  147.                              e[x + my] +
  148.                              e[x + py] +
  149.                              (e[mx + my] +
  150.                               e[px + my] +
  151.                               e[mx + py] +
  152.                               e[px + py]) * 0.5f +
  153.                              ((float)rand() - (float)RAND_MAX * 0.5f) * noiseScale)
  154.                              / averageDivide;
  155.                     }
  156.                 }
  157.                 else
  158.                 { //i style
  159.                 for(unsigned int i = 0; i < wArea; i++)
  160.                     en[i] =
  161.                     (e[i] +
  162.                      e[(i - 1) % wArea] +
  163.                      e[(i + 1) % wArea] +
  164.                      e[(i - wSize) % wArea] +
  165.                      e[(i + wSize) % wArea] +
  166.                      (e[(i - wSize - 1) % wArea] +
  167.                       e[(i + wSize - 1) % wArea] +
  168.                       e[(i - wSize + 1) % wArea] +
  169.                       e[(i + wSize + 1) % wArea]) * 0.5f +
  170.                      (float)(rand() - RAND_MAX / 2 - 1) * noiseScale)
  171.                     / averageDivide;
  172.                 }
  173.  
  174.                 for(unsigned int i = 0; i < wArea; i++)
  175.                     e[i] = en[i];
  176.  
  177.                 if(iteration % 1000 == 0) std::cout << "Iteration: " << iteration << std::endl;
  178.                 iteration++;
  179.             }
  180.             for(int i = 0; i < wArea; i++)
  181.                 c[i] -= e[i];
  182.         }
  183.  
  184.         float average = 0;
  185.         for(unsigned int i = 0; i < wArea; i++)
  186.             average += e[i];
  187.         average /= wArea;
  188.         for(unsigned int i = 0; i < wArea; i++)
  189.             e[i] -= average;
  190.         float varience = 0;
  191.         for(unsigned int i = 0; i < wArea; i++)
  192.             varience += abs(e[i]);
  193.         varience /= wArea;
  194.  
  195.         float changeVarience = 0;
  196.         for(unsigned int i = 0; i < wArea; i++)
  197.             changeVarience += abs(c[i]);
  198.         changeVarience /= wArea;
  199.  
  200.  
  201.         for(int i = 0; i < wArea; i++)
  202.         {
  203.             if(e[i] > 0)    filterValue[i].color = sf::Color(      drawColor & 1 ? 255 - 255 / (e[i] / varience + 1) : 0, 0, 0);
  204.             else            filterValue[i].color = sf::Color(0, 0, drawColor & 2 ? 255 - 255 / (- e[i] / varience + 1) : 0);
  205.  
  206.             if(e[i] > 0)    filterChange[i].color = sf::Color(      drawColor & 1 ? 255 - 255 / (   (c[i] / changeVarience) + 1) : 0, 0, 0);
  207.             else            filterChange[i].color = sf::Color(0, 0, drawColor & 2 ? 255 - 255 / ( - (c[i] / changeVarience) + 1) : 0);
  208.  
  209.             if(e[i] > 0)
  210.             {
  211.                 if(e[i] > e[(i - 1) % wArea] &&
  212.                    e[i] > e[(i + 1) % wArea] &&
  213.                    e[i] > e[(i - wSize) % wArea] &&
  214.                    e[i] > e[(i + wSize) % wArea] &&
  215.                    e[i] > e[(i - wSize - 1) % wArea] &&
  216.                    e[i] > e[(i + wSize - 1) % wArea] &&
  217.                    e[i] > e[(i - wSize + 1) % wArea] &&
  218.                    e[i] > e[(i + wSize + 1) % wArea])
  219.                     filterChangeDirection[i].color = sf::Color(drawColor & 1 ? 255 - 255 / (e[i] / varience + 1) : 0, 0, 0);
  220.                 else
  221.                     filterChangeDirection[i].color = sf::Color(0, 0, 0);
  222.             }
  223.             else
  224.             {
  225.                 if(e[i] < e[(i - 1) % wArea] &&
  226.                    e[i] < e[(i + 1) % wArea] &&
  227.                    e[i] < e[(i - wSize) % wArea] &&
  228.                    e[i] < e[(i + wSize) % wArea] &&
  229.                    e[i] < e[(i - wSize - 1) % wArea] &&
  230.                    e[i] < e[(i + wSize - 1) % wArea] &&
  231.                    e[i] < e[(i - wSize + 1) % wArea] &&
  232.                    e[i] < e[(i + wSize + 1) % wArea])
  233.                     filterChangeDirection[i].color = sf::Color(0, 0, drawColor & 2 ? 255 - 255 / (- e[i] / varience + 1) : 0);
  234.                 else
  235.                     filterChangeDirection[i].color = sf::Color(0, 0, 0);
  236.             }
  237.         }
  238.  
  239.         window.clear();
  240.         window.draw(filterValue);
  241.         window.draw(filterChange);
  242.         window.draw(filterChangeDirection);
  243.         window.display();
  244.     }
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment