Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Mandelbrot CPP

The document contains C++ code that defines a complex number struct and uses it to draw a Mandelbrot set fractal to the screen. It initializes graphics objects, defines a complex number struct with methods for squaring, adding, and calculating the norm. The main function initializes graphics, sets up an event loop, and calls a DRAW macro that iterates over pixels to calculate the Mandelbrot iteration count and color each pixel accordingly. It allows zooming and panning the fractal by processing keyboard and mouse inputs.

Uploaded by

joiudsfs
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Mandelbrot CPP

The document contains C++ code that defines a complex number struct and uses it to draw a Mandelbrot set fractal to the screen. It initializes graphics objects, defines a complex number struct with methods for squaring, adding, and calculating the norm. The main function initializes graphics, sets up an event loop, and calls a DRAW macro that iterates over pixels to calculate the Mandelbrot iteration count and color each pixel accordingly. It allows zooming and panning the fractal by processing keyboard and mouse inputs.

Uploaded by

joiudsfs
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <math.h>

#include <SFML/Graphics.hpp>
#define DRAW graph.create(1024,1024,sf::Color::Black);\
float q=0;\
for(short x=-512;x<512;x++){for(short y=-512;y<512;y++){\
point.R=x+xp*mag; point.R/=256*mag; \
point.I=y+yp*mag; point.I/=256*mag;\
R=point.R; I=point.I;\
for(unsigned short i=70;i!=0;i--){\
point.square();\
point.add(R,I);\
c=i;\
if(point.norm()>2){ i=1;}\
}\
if(c>60) graph.setPixel(x+512,y+512,sf::Color::White);\
else if(c>50)
graph.setPixel(x+512,y+512,sf::Color(200,255,200));\
else if(c>40)
graph.setPixel(x+512,y+512,sf::Color(100,200,100));\
else if(c>30)
graph.setPixel(x+512,y+512,sf::Color(100,50,50));\
else if(c>20)
graph.setPixel(x+512,y+512,sf::Color(10,20,50));\
\
}}
graph.setPixel(512,512,sf::Color(255,0,0));graph.setPixel(513,512,sf::Color(255,0,0
));graph.setPixel(511,512,sf::Color(255,0,0));graph.setPixel(512,511,sf::Color(255,
0,0));graph.setPixel(512,513,sf::Color(255,0,0));\
\
tex.loadFromImage(graph);\
spr.setTexture(tex);
struct complexN{
float R,I;
void square(){

float r=R;

r=(R*R)-(I*I);
I=2*(I)*(R);
R=r;
}
void add(float r, float i){
R+=r;
I+=i;
}
float norm(){
return(R*R+I*I);
}
};
int main(int argc, char** argv){
sf::RenderWindow window(sf::VideoMode(1024,1024), "celt");
float R; float I;
sf::Mouse mouse;
complexN point;point.R=0; point.I=0;
sf::Texture tex;
sf::Sprite spr;
sf::Image graph;
graph.create(1024,1024,sf::Color::Black);
sf::Event event;
unsigned short c;
short mag=1;
float x,y;
int xp=0;float yp=0;

DRAW

while (window.isOpen()){

while (window.pollEvent(event)){
if (event.type == sf::Event::EventType::Closed ){
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Enter))
{xp=0;yp=0;mag=1;DRAW}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{mag+=2;DRAW}
/*if(sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)) {DRAW}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{mag+=2;DRAW}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{mag+=5;DRAW}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {yp+=50;}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {yp-=50;}
*/

if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
xp+=(-512+sf::Mouse::getPosition(window).x)/mag;
yp+=(-512+sf::Mouse::getPosition(window).y)/mag;
DRAW
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) {mag+=1;DRAW}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {mag*=2;DRAW}

window.clear();
window.draw(spr);
window.display();

return 0;
}

You might also like