I have made visualization of
DLA processI used two vectors, for moving particles and second for stucked.
My programm basically works, but sometimes I have error - exception for sfml-graphics-2.dll
I can`t find where I made mistakes.
Here is my source#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <vector>
int main()
{ // create the window
int W = 800;
int H = 600;
int r = 5;
std::size_t count = 1000;
sf::RenderWindow window(sf::VideoMode(W, H), "Particles");
window.setFramerateLimit(30);
window.setVerticalSyncEnabled(true);
std::vector<sf::CircleShape> circle_free;
std::vector<sf::CircleShape> circle_stucked;
//create moving particles
for (std::size_t i = 0; i < count; i++)
{
sf::CircleShape circle;
int x = std::rand() % W;
int y = std::rand() % H;
//printf("%i %i\n", x , y);
circle.setPointCount(8);
circle.setRadius(r);
circle.setPosition(x, y);
sf::Color color = sf::Color(255, 0, 0, 100);
circle.setFillColor(color);
circle_free.push_back(circle);
}
//Stucked point
std::srand(time(NULL));
for (std::size_t i = 0; i < 30; i++){
sf::CircleShape circle2;
int x = std::rand() % W;
int y = std::rand() % H;
circle2.setPosition(x, y);
circle2.setPointCount(6);
circle2.setRadius(5);
sf::Color color = sf::Color(0, 255, 0, 255);
circle2.setFillColor(color);
circle_stucked.push_back(circle2);
}
// run the main loop
while (window.isOpen())
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
window.close();
}
window.clear();
//moving random particles
for (std::size_t i = 0; i < circle_free.size(); i++) {
sf::Vector2f offset;
float speed = 10;
offset.x = ( speed / 2.0 - static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / speed)) );
offset.y = ( speed / 2.0 - static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / speed)) );
circle_free[i].move(offset);
//printf("%i\n", sz);
//back particles to window
if (circle_free[i].getPosition().x > W) circle_free[i].setPosition(W-10, circle_free[i].getPosition().y);
if (circle_free[i].getPosition().y > H) circle_free[i].setPosition(circle_free[i].getPosition().x, H-10);
if (circle_free[i].getPosition().x < 0) circle_free[i].setPosition(10, circle_free[i].getPosition().y);
if (circle_free[i].getPosition().y < 0) circle_free[i].setPosition(circle_free[i].getPosition().x, 10);
}
for (std::size_t i = 0; i < circle_free.size(); i++) {
for (std::size_t j = 0; j < circle_stucked.size(); j++) {
//printf("OK");
float dist = pow(circle_free[i].getPosition().x - circle_stucked[j].getPosition().x, 2) +
pow(circle_free[i].getPosition().y - circle_stucked[j].getPosition().y, 2);
dist = sqrt(dist);
if (dist <= (r * 2) ) {//here we stuck moving particles
sf::Color color = sf::Color(0, 255, 0, 200);
circle_free[i].setFillColor(color);
//add moving particle to stucked vector
circle_stucked.push_back(circle_free[i]);
//delete moving particles from vector
circle_free.erase(circle_free.begin() + i);
}
}
}
//draw moving particles
for (std::size_t i = 0; i < circle_free.size(); i++) {
window.draw(circle_free[i]);
}
//draw stucked particles
for (std::size_t i = 0; i < circle_stucked.size(); i++) {
window.draw(circle_stucked[i]);
}
window.display();
}
return 0;
}