Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML\Graphics.hpp>
  3.  
  4. bool check_if_prime(int&);
  5.  
  6. int main(){
  7.     const int WIDTH{ 800 };
  8.     const int HEIGHT{ 800 };
  9.  
  10.     sf::RenderWindow main_window(sf::VideoMode(WIDTH, HEIGHT), "SFML Works!");
  11.  
  12.     std::vector<sf::CircleShape> vec_circ_shape;
  13.     vec_circ_shape.reserve(37000);
  14.  
  15.     sf::RectangleShape circ_spawner(sf::Vector2f(1.0f, 1.0f));
  16.     circ_spawner.setPosition(sf::Vector2f(WIDTH / 2.0f, HEIGHT / 2.0f));
  17.     circ_spawner.setOrigin(0.5f, 0.5f);
  18.  
  19.     int prime_to_test{ 1 };
  20.    
  21.     //
  22.     //Create square spiral by going from the middle outwards: right->up->left->down->...
  23.     //There's is a loop for every direction because the distance of every direction is
  24.     //increased by 2 for every iteration of the parent loop.
  25.     //For every iteration of every "direction" loop it is checked whether that respective
  26.     //point in the square spiral is a prime via check_if_prime(int&)
  27.     //
  28.     for (int i = 0; i <= 200; ++i)
  29.     {
  30.         //Right
  31.         for (int a = 1; a <= 1 + (2*i); ++a)
  32.         {
  33.             ++prime_to_test;
  34.             circ_spawner.move(1.0f, 0.0f);
  35.             if (check_if_prime(prime_to_test))
  36.             {
  37.                 vec_circ_shape.push_back(sf::CircleShape(0.3f));
  38.                 vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
  39.             }
  40.         }
  41.  
  42.         //Up
  43.         for (int a = 1; a <= 1 + (2 * i); ++a)
  44.         {
  45.             ++prime_to_test;
  46.             circ_spawner.move(0.0f, 1.0f);
  47.             if (check_if_prime(prime_to_test))
  48.             {
  49.                 vec_circ_shape.push_back(sf::CircleShape(0.3f));
  50.                 vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
  51.             }
  52.         }
  53.  
  54.         //Left
  55.         for (int a = 1; a <= 1 + (2 * i)+1; ++a)
  56.         {
  57.             ++prime_to_test;
  58.             circ_spawner.move(-1.0f, 0.0f);
  59.             if (check_if_prime(prime_to_test))
  60.             {
  61.                 vec_circ_shape.push_back(sf::CircleShape(0.3f));
  62.                 vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
  63.             }
  64.         }
  65.  
  66.         //Down
  67.         for (int a = 1; a <= 1 + (2 * i)+1; ++a)
  68.         {
  69.             ++prime_to_test;
  70.             circ_spawner.move(0.0f, -1.0f);
  71.             if (check_if_prime(prime_to_test))
  72.             {
  73.                 vec_circ_shape.push_back(sf::CircleShape(0.3f));
  74.                 vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
  75.             }
  76.         }
  77.     }
  78.  
  79.     //Print the number of elements in vec_circ_shape
  80.     std::cout << vec_circ_shape.size() << "\n";
  81.     //Print the total amount of bytes that all the elements encompass
  82.     std::cout << sizeof(sf::CircleShape) *  vec_circ_shape.size() << "\n";
  83.  
  84.     //main loop
  85.     while (main_window.isOpen())
  86.     {
  87.         sf::Event event;
  88.         while (main_window.pollEvent(event))
  89.         {
  90.             if (event.type = sf::Event::Closed)
  91.             {
  92.                 main_window.close();
  93.             }
  94.         }
  95.  
  96.         //Clear window
  97.         main_window.clear();
  98.  
  99.         //Draw all the spheres
  100.         for (sf::CircleShape shape : vec_circ_shape)
  101.         {
  102.             main_window.draw(shape);
  103.         }
  104.  
  105.         //Display buffer
  106.         main_window.display();
  107.     }
  108.  
  109.     return 0;
  110. }
  111.  
  112. bool check_if_prime(int &input_number)
  113. {
  114.     bool isPrime{ true };
  115.  
  116.     for (int it = 2; it <= 10; ++it) {
  117.         //std::cout << it << "\n";
  118.         if (it != input_number && input_number % it == 0)
  119.         {
  120.             isPrime = false;
  121.             break;
  122.         }
  123.     }
  124.  
  125.     return isPrime;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement