Skip to content

Instantly share code, notes, and snippets.

@Sam-Belliveau
Created February 9, 2018 01:42
Show Gist options
  • Save Sam-Belliveau/1339c83a43a62e332ec762ab29e040ec to your computer and use it in GitHub Desktop.
Save Sam-Belliveau/1339c83a43a62e332ec762ab29e040ec to your computer and use it in GitHub Desktop.
you need sfml
#include <SFML/Graphics.hpp>
typedef long double num;
const unsigned int w = 1200, h = 800;
sf::RenderWindow app(sf::VideoMode(w, h), "Mandelbrot Set");
num x = 0, y = 0, zoom = 2;
const unsigned int colors = 90;
const unsigned char r[colors] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
const unsigned char g[colors] = {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
const unsigned char b[colors] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11};
const unsigned int pixelC = w * h << 2;
sf::Uint8 pixels[pixelC];
unsigned int iter = 45;
const num zoomTime = 1.f/64.f;
const num oZoomTime = 1.f + zoomTime;
const num nZoomTime = 1.f - zoomTime;
const num zoomSpeed = 4;
const num rZoomSpeed = 1/zoomSpeed;
void drawFract(const unsigned int threadNum)
{
const num bail = 4, ratio = ((num)app.getSize().x)/((num)app.getSize().y);
const num xStart = x - zoom,
xIter = 2 * ratio * zoom / w,
yIter = 2 * zoom / h;
const unsigned int threadHeight = h/8;
num py = y - zoom + (2 * zoom * threadNum) / 8;
unsigned int counter = (w * threadHeight * 4) * threadNum;
for(unsigned int iy = 0; iy < threadHeight; iy++)
{
num px = xStart;
for(unsigned int ix = 0; ix < w; ix++)
{
pixels[counter + 0] = 0; pixels[counter + 1] = 0;
pixels[counter + 2] = 0; pixels[counter + 3] = 255;
num zr = px, zi = py;
for(unsigned int i = 0; i < iter; i++)
{
const num sqzr = zr * zr,
sqzi = zi * zi,
add = zr + zi;
zi = py + add*add - sqzr - sqzi;
zr = sqzr - sqzi + px;
if(sqzr + sqzi > bail)
{
i %= colors;
pixels[counter + 0] = r[i];
pixels[counter + 1] = g[i];
pixels[counter + 2] = b[i];
break;
}
}
px += xIter;
counter += 4;
}
py += yIter;
}
return;
}
void reset(){ const num ratio = ((num)app.getSize().y)/((num)app.getSize().x); x = -zoom; y = 0; zoom = 2; }
#define drawPixels; texture.update(pixels); app.draw(sprite); app.display();
#define launchBots; bot0.launch(); bot1.launch(); bot2.launch(); bot3.launch(); bot4.launch(); bot5.launch(); bot6.launch(); bot7.launch();
#define waitBots; bot0.wait(); bot1.wait(); bot2.wait(); bot3.wait(); bot4.wait(); bot5.wait(); bot6.wait(); bot7.wait();
int main()
{
sf::Texture texture;
texture.create(w,h);
texture.setSmooth(true);
sf::Sprite sprite(texture);
app.setFramerateLimit(64);
sf::Thread bot0(drawFract,0), bot1(drawFract,1),
bot2(drawFract,2), bot3(drawFract,3),
bot4(drawFract,4), bot5(drawFract,5),
bot6(drawFract,6), bot7(drawFract,7);
reset();
bool draw = true;
while (app.isOpen())
{
sf::Event event;
while (app.pollEvent(event))
{
if (event.type == sf::Event::Closed)
app.close();
}
draw = true;
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
const num ratio = ((num)app.getSize().y)/((num)app.getSize().x);
const num wRatio = ((num)w)/((num)app.getSize().x);
const num hRatio = ((num)h)/((num)app.getSize().y);
num tx = sf::Mouse::getPosition(app).x*wRatio,
ty = sf::Mouse::getPosition(app).y*hRatio;
sprite.setOrigin(tx,ty);
sprite.setPosition(tx,ty);
tx -= ((num)w)*0.5*ratio; ty -= ((num)h)*0.5;
tx *= zoom; ty *= zoom;
tx /= ((num)w)*0.5*ratio; ty /= ((num)h)*0.5;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{
x -= tx * (zoomSpeed - 1); y -= ty * (zoomSpeed - 1);
zoom *= zoomSpeed;
launchBots;
for(num scale = 1; scale >= rZoomSpeed; scale *= nZoomTime)
{
app.clear(sf::Color(64,64,64));
sprite.setScale(scale,scale);
app.draw(sprite); app.display();
}
} else
{
x += tx * (1 - rZoomSpeed); y += ty * (1 - rZoomSpeed);
zoom *= rZoomSpeed;
launchBots;
for(num scale = 1; scale <= zoomSpeed; scale *= oZoomTime)
{
app.clear(sf::Color(64,64,64));
sprite.setScale(scale,scale);
app.draw(sprite); app.display();
}
}
waitBots;
sprite.setScale(1,1);
sprite.setOrigin(0,0);
sprite.setPosition(0,0);
drawPixels;
draw = false;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{ iter *= 2; while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){} }
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && iter > 45)
{ iter *= 0.5; while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){} }
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{ reset(); while(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){} }
else { draw = false; }
if(draw)
{
/** UNLEASH THE BOTS!!! **/
bot0.launch(); bot1.launch();
bot2.launch(); bot3.launch();
bot4.launch(); bot5.launch();
bot6.launch(); bot7.launch();
/** Ok bots bring it back in **/
bot0.wait(); bot1.wait();
bot2.wait(); bot3.wait();
bot4.wait(); bot5.wait();
bot6.wait(); bot7.wait();
/** THE BOTS DID WHAT?!?!?! **/
drawPixels;
}
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment