Advertisement
cartagenae

ComplexPlane.cpp

Apr 25th, 2025 (edited)
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.84 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Audio.hpp>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <vector>
  6. #include "ComplexPlane.h"
  7. #include <complex>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11. using namespace sf;
  12.  
  13. ComplexPlane::ComplexPlane(int pixelWidth, int pixelHeight)
  14. {
  15.     m_vArray.setPrimitiveType(Points);
  16.     m_vArray.resize(pixelWidth * pixelHeight);
  17.     m_state = State::CALCULATING;
  18.     Vector2f m_mouseLocation = {0.f, 0.f};
  19.     Vector2i m_pixel_size = { pixelWidth, pixelHeight };
  20.     Vector2f m_plane_center = {0, 0};
  21.     Vector2f m_plane_size = { BASE_WIDTH, BASE_HEIGHT * m_aspectRatio };
  22.     m_zoom_count = 0;
  23.     float m_aspectRatio = static_cast<float>(pixelHeight) / static_cast<float>(pixelWidth);
  24. }
  25.  
  26. void ComplexPlane::draw(RenderTarget& target, RenderStates states) const
  27. {
  28.     target.draw(m_vArray);
  29. }
  30.  
  31. void ComplexPlane::updateRender()
  32. {
  33.     if(m_state == State::CALCULATING)
  34.     {
  35.         for(int j = 0; j < m_pixel_size.x; j++)
  36.         {
  37.             for(int i = 0; i < m_pixel_size.y; i++)
  38.             {
  39.                 m_vArray[j + i * m_pixel_size.x].position = { (float)j, (float)i };
  40.                 ComplexPlane::mapPixelToCoords(m_pixel_size);
  41.                 size_t numIterations = ComplexPlane::countIterations(m_plane_size);
  42.                 Uint8 r, g, b;
  43.                 ComplexPlane::iterationsToRGB(numIterations, r, g, b);
  44.                 m_vArray[j + i * m_pixel_size.x].color = { r,g,b };
  45.             }
  46.         }
  47.         m_state = State::DISPLAYING;
  48.     }
  49. }
  50.  
  51. void ComplexPlane::zoomIn()
  52. {
  53.     m_zoom_count++;
  54.    
  55.     float xSize = BASE_WIDTH * pow(BASE_ZOOM, m_zoom_count);
  56.     float ySize = BASE_HEIGHT * m_aspectRatio * pow(BASE_ZOOM, m_zoom_count);
  57.  
  58.     m_plane_size = { xSize, ySize };
  59.  
  60.     m_state = State::CALCULATING;
  61. }
  62.  
  63. void ComplexPlane::zoomOut()
  64. {
  65.     m_zoom_count--;
  66.    
  67.     float xSize = BASE_WIDTH * pow(BASE_ZOOM, m_zoom_count);
  68.     float ySize = BASE_HEIGHT * m_aspectRatio * pow(BASE_ZOOM, m_zoom_count);
  69.  
  70.     m_plane_size = { xSize, ySize };
  71.  
  72.     m_state = State::CALCULATING;
  73. }
  74.  
  75. void ComplexPlane::setCenter(Vector2i mousePixel)
  76. {
  77.     m_plane_center = ComplexPlane::mapPixelToCoords(mousePixel);
  78.     m_state = State::CALCULATING;
  79. }
  80.  
  81. void ComplexPlane::setMouseLocation(Vector2i mousePixel)
  82. {
  83.     m_mouseLocation = ComplexPlane::mapPixelToCoords(mousePixel);
  84. }
  85.  
  86. void ComplexPlane::loadText(Text& text)
  87. {
  88.     stringstream ss;
  89.  
  90.     ss << "Cursor: (" << m_mouseLocation.x << ", " << m_mouseLocation.y << ")\n";
  91.     ss << "Center: (" << m_plane_center.x << ", " << m_plane_center.y << ")\n";
  92.  
  93.     text.setString(ss.str());
  94. }
  95.  
  96. size_t ComplexPlane::countIterations(Vector2f coord)
  97. {
  98.     size_t iterations;
  99.     for(int x = 0; x < coord.x; x++)
  100.     {
  101.         for(int y = 0; x < coord.y; y++)
  102.         {
  103.             iterations++;
  104.         }
  105.     }
  106.  
  107.     return iterations;
  108. }
  109.  
  110. void ComplexPlane::iterationsToRGB(size_t count, Uint8& r, Uint8& g, Uint8& b)
  111. {
  112.     // for(size_t i = 0; i < count; i++)
  113.     // {
  114.     //     if(i < 51)
  115.     //     {
  116.     //         // iteration less than 51
  117.     //         // hex: #0d0630
  118.     //         r = 13;
  119.     //         g = 6;
  120.     //         b = 48;
  121.     //     }
  122.     //     else if(i >= 51 && i < 102)
  123.     //     {
  124.     //         // iteration between 51 inclusive and 102 non-inclusive
  125.     //         // hex: #18314f
  126.     //         r = 24;
  127.     //         g = 49;
  128.     //         b = 79;
  129.     //     }
  130.     //     else if(i >= 102 && i < 153)
  131.     //     {
  132.     //         // iteration between 102 inclusive and 153 non-inclusive
  133.     //         // hex: #384e77
  134.     //         r = 56;
  135.     //         g = 78;
  136.     //         b = 119;
  137.     //     }
  138.     //     else if(i >= 153 && i < 204)
  139.     //     {
  140.     //         // iteration between 153 inclusive and 204 non-inclusive
  141.     //         // hex: #8bbeb2
  142.     //         r = 139;
  143.     //         g = 190;
  144.     //         b = 178;
  145.     //     }
  146.     //     else if(i >= 204 && i < 255)
  147.     //     {
  148.     //         // iteration between 204 inclusive and 255 non-inclusive
  149.     //         // hex: #e6f9af
  150.     //         r = 230;
  151.     //         g = 249;
  152.     //         b = 175;
  153.     //     }
  154.     //     else
  155.     //     {
  156.     //         // iteration 255 inclusive and above
  157.     //         // hex: #000000
  158.     //         r = 0;
  159.     //         g = 0;
  160.     //         b = 0;
  161.     //     }
  162.     // }
  163.  
  164.     if(count < 51)
  165.     {
  166.         // iteration less than 51
  167.         // hex: #0d0630
  168.         r = 13;
  169.         g = 6;
  170.         b = 48;
  171.     }
  172.     else if(count >= 51 && count < 102)
  173.     {
  174.         // iteration between 51 inclusive and 102 non-inclusive
  175.         // hex: #18314f
  176.         r = 24;
  177.         g = 49;
  178.         b = 79;
  179.     }
  180.     else if(count >= 102 && count < 153)
  181.     {
  182.         // iteration between 102 inclusive and 153 non-inclusive
  183.         // hex: #384e77
  184.         r = 56;
  185.         g = 78;
  186.         b = 119;
  187.     }
  188.     else if(count >= 153 && count < 204)
  189.     {
  190.         // iteration between 153 inclusive and 204 non-inclusive
  191.         // hex: #8bbeb2
  192.         r = 139;
  193.         g = 190;
  194.         b = 178;
  195.     }
  196.     else if(count >= 204 && count < 255)
  197.     {
  198.         // iteration between 204 inclusive and 255 non-inclusive
  199.         // hex: #e6f9af
  200.         r = 230;
  201.         g = 249;
  202.         b = 175;
  203.     }
  204.     else
  205.     {
  206.         // iteration 255 inclusive and above
  207.         // hex: #000000
  208.         r = 0;
  209.         g = 0;
  210.         b = 0;
  211.     }
  212. }
  213.  
  214. Vector2f ComplexPlane::mapPixelToCoords(Vector2i mousePixel)
  215. {
  216.     Vector2f coords;
  217.  
  218.     float minX = m_plane_center.x - m_plane_size.x / 2.0f;
  219.     float minY = m_plane_center.y - m_plane_size.y / 2.0f;
  220.  
  221.     coords.x = (static_cast<float>(mousePixel.x) / m_pixel_size.x) * m_plane_size.x + minX;
  222.     coords.y = (static_cast<float>(mousePixel.y) / m_pixel_size.y) * m_plane_size.y + minY;
  223.    
  224.     return coords;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement