Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <SFML\Graphics.hpp>
- bool check_if_prime(int&);
- int main(){
- const int WIDTH{ 800 };
- const int HEIGHT{ 800 };
- sf::RenderWindow main_window(sf::VideoMode(WIDTH, HEIGHT), "SFML Works!");
- std::vector<sf::CircleShape> vec_circ_shape;
- vec_circ_shape.reserve(37000);
- sf::RectangleShape circ_spawner(sf::Vector2f(1.0f, 1.0f));
- circ_spawner.setPosition(sf::Vector2f(WIDTH / 2.0f, HEIGHT / 2.0f));
- circ_spawner.setOrigin(0.5f, 0.5f);
- int prime_to_test{ 1 };
- //
- //Create square spiral by going from the middle outwards: right->up->left->down->...
- //There's is a loop for every direction because the distance of every direction is
- //increased by 2 for every iteration of the parent loop.
- //For every iteration of every "direction" loop it is checked whether that respective
- //point in the square spiral is a prime via check_if_prime(int&)
- //
- for (int i = 0; i <= 200; ++i)
- {
- //Right
- for (int a = 1; a <= 1 + (2*i); ++a)
- {
- ++prime_to_test;
- circ_spawner.move(1.0f, 0.0f);
- if (check_if_prime(prime_to_test))
- {
- vec_circ_shape.push_back(sf::CircleShape(0.3f));
- vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
- }
- }
- //Up
- for (int a = 1; a <= 1 + (2 * i); ++a)
- {
- ++prime_to_test;
- circ_spawner.move(0.0f, 1.0f);
- if (check_if_prime(prime_to_test))
- {
- vec_circ_shape.push_back(sf::CircleShape(0.3f));
- vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
- }
- }
- //Left
- for (int a = 1; a <= 1 + (2 * i)+1; ++a)
- {
- ++prime_to_test;
- circ_spawner.move(-1.0f, 0.0f);
- if (check_if_prime(prime_to_test))
- {
- vec_circ_shape.push_back(sf::CircleShape(0.3f));
- vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
- }
- }
- //Down
- for (int a = 1; a <= 1 + (2 * i)+1; ++a)
- {
- ++prime_to_test;
- circ_spawner.move(0.0f, -1.0f);
- if (check_if_prime(prime_to_test))
- {
- vec_circ_shape.push_back(sf::CircleShape(0.3f));
- vec_circ_shape[vec_circ_shape.size() - 1].setPosition(circ_spawner.getPosition());
- }
- }
- }
- //Print the number of elements in vec_circ_shape
- std::cout << vec_circ_shape.size() << "\n";
- //Print the total amount of bytes that all the elements encompass
- std::cout << sizeof(sf::CircleShape) * vec_circ_shape.size() << "\n";
- //main loop
- while (main_window.isOpen())
- {
- sf::Event event;
- while (main_window.pollEvent(event))
- {
- if (event.type = sf::Event::Closed)
- {
- main_window.close();
- }
- }
- //Clear window
- main_window.clear();
- //Draw all the spheres
- for (sf::CircleShape shape : vec_circ_shape)
- {
- main_window.draw(shape);
- }
- //Display buffer
- main_window.display();
- }
- return 0;
- }
- bool check_if_prime(int &input_number)
- {
- bool isPrime{ true };
- for (int it = 2; it <= 10; ++it) {
- //std::cout << it << "\n";
- if (it != input_number && input_number % it == 0)
- {
- isPrime = false;
- break;
- }
- }
- return isPrime;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement