Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Optimum Theory - Gary Lee
- c++ / sfml implementation - James Liam Keough (just put this name in whatever history, dont contact it)
- Display:
- left - actual data
- middle - change in data
- right - local maximums
- Controls:
- OP - change color filter
- [] - change the frequency that the change array is calculated and the image is drawn (exponential, dont go too high!)
- Up/Down/PageUP/PageDown - change scale from 7.00..1 to 8
- Hold Space - Pause
- R - reset to random
- T - reset to black hole
- I - show current iteration
- */
- #include <SFML/Graphics.hpp>//RenderWindow, VertexArray
- #include <iostream>
- #include <stdlib.h>//srand, rand
- #include <time.h>//time
- int main()
- {
- const int wSize = 400;
- const int wArea = wSize * wSize;
- float averageDivide = 7.0001f;
- const float noiseScale = 0.01f;
- sf::RenderWindow window(sf::VideoMode(wSize * 3, wSize * 1), "universe2D");
- float e[wArea] = {0};//energy
- float en[wArea] = {0};//next energy
- float c[wArea] = {0};//change
- float cd[wArea] = {0};//directional change
- sf::VertexArray filterValue(sf::Points, wArea);
- for(int x = 0; x < wSize; x++)
- for(int y = 0; y < wSize; y++)
- filterValue[x + y * wSize].position = sf::Vector2f(x, y);
- sf::VertexArray filterChange(sf::Points, wArea);
- for(int x = 0; x < wSize; x++)
- for(int y = 0; y < wSize; y++)
- filterChange[x + y * wSize].position = sf::Vector2f(x + wSize, y);
- sf::VertexArray filterChangeDirection(sf::Points, wArea);
- for(int x = 0; x < wSize; x++)
- for(int y = 0; y < wSize; y++)
- filterChangeDirection[x + y * wSize].position = sf::Vector2f(x + (wSize << 1), y);
- char drawColor = 3;
- unsigned int iteration = 0;
- unsigned int drawFreq = 16;
- while(window.isOpen())
- {
- sf::Event event;
- while(window.pollEvent(event))
- {
- switch(event.type)
- {
- case sf::Event::Closed: window.close(); break;
- case sf::Event::KeyPressed:
- switch(event.key.code)
- {
- case sf::Keyboard::R:
- srand (time(NULL));
- for(int i = 0; i < wArea; i++)
- e[i] = en[i] = (float)(rand() - RAND_MAX / 2 - 1) * 100.0f;
- iteration = 0;
- break;
- case sf::Keyboard::T:
- srand (time(NULL));
- for(int i = 0; i < wArea; i++)
- e[i] = en[i] = 0;
- e[wSize / 2 + wArea / 2] = en[wSize / 2 + wArea / 2] = -100000000000;
- iteration = 0;
- break;
- case sf::Keyboard::O: drawColor ^= 1; break;
- case sf::Keyboard::P: drawColor ^= 2; break;
- case sf::Keyboard::LBracket:
- if(drawFreq < 2) break;
- drawFreq >>= 1;
- std::cout << "Draw Frequency: " << drawFreq << std::endl;
- break;
- case sf::Keyboard::RBracket:
- drawFreq <<= 1;
- std::cout << "Draw Frequency: " << drawFreq << std::endl;
- break;
- case sf::Keyboard::I:
- std::cout << "Iteration: " << iteration << std::endl;
- break;
- }
- break;
- }
- }
- {//controls
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- if(averageDivide < 7.99999)
- {
- averageDivide += 0.00001f;
- std::cout << "Scale:" << averageDivide - 7 << std::endl;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- if(averageDivide > 7.00001)
- {
- averageDivide -= 0.00001f;
- std::cout << "Scale:" << averageDivide - 7 << std::endl;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::PageUp))
- if(averageDivide < 7.999)
- {
- averageDivide += 0.001f;
- std::cout << "Scale:" << averageDivide - 7 << std::endl;
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::PageDown))
- if(averageDivide > 7.001)
- {
- averageDivide -= 0.001f;
- std::cout << "Scale:" << averageDivide - 7 << std::endl;
- }
- }
- if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
- {
- for(int i = 0; i < wArea; i++)
- c[i] = e[i];
- for(int d = 0; d < drawFreq; d++)
- {
- if(true)
- { //x y style
- for(unsigned int x = 0; x < wSize; x++)
- for(unsigned int y = 0; y < wSize; y++)
- {
- unsigned int my = ((y - 1) % wSize) * wSize;
- unsigned int py = ((y + 1) % wSize) * wSize;
- unsigned int mx = (x - 1) % wSize;
- unsigned int px = (x + 1) % wSize;
- en[x + y * wSize] =
- (e[x + y * wSize] +
- e[mx + y * wSize] +
- e[px + y * wSize] +
- e[x + my] +
- e[x + py] +
- (e[mx + my] +
- e[px + my] +
- e[mx + py] +
- e[px + py]) * 0.5f +
- ((float)rand() - (float)RAND_MAX * 0.5f) * noiseScale)
- / averageDivide;
- }
- }
- else
- { //i style
- for(unsigned int i = 0; i < wArea; i++)
- en[i] =
- (e[i] +
- e[(i - 1) % wArea] +
- e[(i + 1) % wArea] +
- e[(i - wSize) % wArea] +
- e[(i + wSize) % wArea] +
- (e[(i - wSize - 1) % wArea] +
- e[(i + wSize - 1) % wArea] +
- e[(i - wSize + 1) % wArea] +
- e[(i + wSize + 1) % wArea]) * 0.5f +
- (float)(rand() - RAND_MAX / 2 - 1) * noiseScale)
- / averageDivide;
- }
- for(unsigned int i = 0; i < wArea; i++)
- e[i] = en[i];
- if(iteration % 1000 == 0) std::cout << "Iteration: " << iteration << std::endl;
- iteration++;
- }
- for(int i = 0; i < wArea; i++)
- c[i] -= e[i];
- }
- float average = 0;
- for(unsigned int i = 0; i < wArea; i++)
- average += e[i];
- average /= wArea;
- for(unsigned int i = 0; i < wArea; i++)
- e[i] -= average;
- float varience = 0;
- for(unsigned int i = 0; i < wArea; i++)
- varience += abs(e[i]);
- varience /= wArea;
- float changeVarience = 0;
- for(unsigned int i = 0; i < wArea; i++)
- changeVarience += abs(c[i]);
- changeVarience /= wArea;
- for(int i = 0; i < wArea; i++)
- {
- if(e[i] > 0) filterValue[i].color = sf::Color( drawColor & 1 ? 255 - 255 / (e[i] / varience + 1) : 0, 0, 0);
- else filterValue[i].color = sf::Color(0, 0, drawColor & 2 ? 255 - 255 / (- e[i] / varience + 1) : 0);
- if(e[i] > 0) filterChange[i].color = sf::Color( drawColor & 1 ? 255 - 255 / ( (c[i] / changeVarience) + 1) : 0, 0, 0);
- else filterChange[i].color = sf::Color(0, 0, drawColor & 2 ? 255 - 255 / ( - (c[i] / changeVarience) + 1) : 0);
- if(e[i] > 0)
- {
- if(e[i] > e[(i - 1) % wArea] &&
- e[i] > e[(i + 1) % wArea] &&
- e[i] > e[(i - wSize) % wArea] &&
- e[i] > e[(i + wSize) % wArea] &&
- e[i] > e[(i - wSize - 1) % wArea] &&
- e[i] > e[(i + wSize - 1) % wArea] &&
- e[i] > e[(i - wSize + 1) % wArea] &&
- e[i] > e[(i + wSize + 1) % wArea])
- filterChangeDirection[i].color = sf::Color(drawColor & 1 ? 255 - 255 / (e[i] / varience + 1) : 0, 0, 0);
- else
- filterChangeDirection[i].color = sf::Color(0, 0, 0);
- }
- else
- {
- if(e[i] < e[(i - 1) % wArea] &&
- e[i] < e[(i + 1) % wArea] &&
- e[i] < e[(i - wSize) % wArea] &&
- e[i] < e[(i + wSize) % wArea] &&
- e[i] < e[(i - wSize - 1) % wArea] &&
- e[i] < e[(i + wSize - 1) % wArea] &&
- e[i] < e[(i - wSize + 1) % wArea] &&
- e[i] < e[(i + wSize + 1) % wArea])
- filterChangeDirection[i].color = sf::Color(0, 0, drawColor & 2 ? 255 - 255 / (- e[i] / varience + 1) : 0);
- else
- filterChangeDirection[i].color = sf::Color(0, 0, 0);
- }
- }
- window.clear();
- window.draw(filterValue);
- window.draw(filterChange);
- window.draw(filterChangeDirection);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment