initial commit

This commit is contained in:
Akos Horvath 2022-07-04 19:22:36 +02:00
commit e16cfcd0c5
8 changed files with 334 additions and 0 deletions

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
all:
g++ graphics.cpp main.cpp -o mandelbrot

49
graphics.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "graphics.h"
#include "utils.h"
#include <SDL2/SDL_render.h>
#include <cstdlib>
#include <iostream>
using namespace Mandelbrot;
Graphics::Graphics(int w, int h, std::string title) : w(w), h(h), title(title)
{
SDL_Window *window = SDL_CreateWindow(this->title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
this->w, this->h, 0);
if (!window)
{
std::cout << "Could not create window. "
<< SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
this->window = window;
SDL_Renderer *r = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!r)
{
std::cout << "Could not create renderer. "
<< SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
this->renderer = r;
}
void Graphics::drawPoint(const Vec2i &pos, const rgb &col)
{
SDL_RenderDrawPoint(this->renderer, pos.x, pos.y);
}
void Graphics::plot(const Vec2bf &from, const Vec2bf &to)
{
for (double i = 0.0; i < this->w; i++) {
for (double j = 0.0; j < this->h; j++) {
BigFloat f = from.x + (i / this->w) * (from.x - to.x);
}
}
}

23
graphics.h Normal file
View File

@ -0,0 +1,23 @@
#pragma once
#include <SDL2/SDL.h>
#include <cstdint>
#include <string>
#include "utils.h"
using namespace Util;
namespace Mandelbrot {
class Graphics {
public:
std::string title;
int w, h;
SDL_Window *window;
SDL_Renderer *renderer;
Graphics(int w, int h, std::string title);
void plot(const Vec2bf &from, const Vec2bf &to);
void drawPoint(const Vec2i &pos, const rgb &col);
};
} // namespace Mandelbrot

6
main.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "graphics.h"
int main()
{
return 0;
}

18
mandelbrot.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "mandelbrot.h"
#include "utils.h"
uint32_t mandelbrot(const BigFloat &c, Vec2bf &ret)
{
uint32_t iter = 0;
const uint32_t max_iter = 100;
BigFloat z = BigFloat(c.precision);
z.setValue(0.0);
while(z <= 2 || iter < max_iter)
{
z = z*z+c;
iter++;
}
return iter;
}

14
mandelbrot.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <mpfr.h>
#include <cmath>
#include "utils.h"
using namespace Util;
namespace Mandelbrot
{
uint32_t mandelbrot(const BigFloat &c, Vec2bf &ret);
}

54
utils.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "utils.h"
using namespace Util;
rgb::rgb(uint8_t r, uint8_t g, uint8_t b)
: r(r), g(g), b(b)
{
}
Vec2i::Vec2i(int x, int y) : x(x), y(y)
{
}
Vec2f::Vec2f(float x, float y) : x(x), y(y)
{
}
Vec2d::Vec2d(double x, double y) : x(x), y(y)
{
}
BigFloat::BigFloat(uint32_t prec)
{
mpfr_init2(this->val, prec);
}
void BigFloat::setValue(double val)
{
mpfr_set_d(this->val, val, MPFR_RNDD);
}
std::string BigFloat::to_str()
{
char buf[this->precision/2];
mpfr_snprintf(buf, this->precision/2, "%.60RNf", this->val);
std::string s(buf);
return s;
}
Vec2bf::Vec2bf(double x, double y, uint32_t prec)
: x(BigFloat(prec)), y(BigFloat(prec))
{
this->x.setValue(x);
this->y.setValue(y);
}
/*
BigFloat operator + (BigFloat const &f);
BigFloat operator - (BigFloat const &f);
BigFloat operator * (BigFloat const &f);
BigFloat operator / (BigFloat const &f);
*/

168
utils.h Normal file
View File

@ -0,0 +1,168 @@
#pragma once
#include <cstdint>
#include <mpfr.h>
#include <string>
namespace Util {
class rgb {
public:
uint8_t r;
uint8_t g;
uint8_t b;
rgb(uint8_t r, uint8_t g, uint8_t b);
};
class Vec2i {
public:
int x;
int y;
Vec2i(int x, int y);
};
class Vec2f {
public:
float x;
float y;
Vec2f(float x, float y);
};
class Vec2d {
public:
double x;
double y;
Vec2d(double x, double y);
};
class BigFloat {
public:
uint32_t precision;
mpfr_t val;
BigFloat(uint32_t prec);
void setValue(double val);
std::string to_str();
BigFloat operator / (const BigFloat &f);
inline BigFloat operator - (const BigFloat &f)
{
BigFloat res = BigFloat(f.precision);
mpfr_sub(res.val, this->val, f.val, MPFR_RNDD);
return res;
}
inline BigFloat operator - (const double d)
{
BigFloat res = BigFloat(this->precision);
mpfr_sub_d(res.val, this->val, d, MPFR_RNDD);
return res;
}
inline BigFloat operator * (const BigFloat &f)
{
BigFloat res = BigFloat(f.precision);
mpfr_mul(res.val, this->val, f.val, MPFR_RNDD);
return res;
}
inline BigFloat operator * (const double d)
{
BigFloat res = BigFloat(this->precision);
mpfr_mul_d(res.val, this->val, d, MPFR_RNDD);
return res;
}
inline BigFloat operator + (const BigFloat &f)
{
BigFloat res = BigFloat(f.precision);
mpfr_add(res.val, this->val, f.val, MPFR_RNDD);
return res;
}
inline BigFloat operator + (const double d)
{
BigFloat res = BigFloat(this->precision);
mpfr_add_d(res.val, this->val, d, MPFR_RNDD);
return res;
}
inline bool operator < (const BigFloat &f)
{
if (mpfr_cmp(this->val, f.val) < 0)
return true;
return false;
}
inline bool operator > (const BigFloat &f)
{
if (mpfr_cmp(this->val, f.val) > 0)
return true;
return false;
}
inline bool operator <= (const BigFloat &f)
{
int v = mpfr_cmp(this->val, f.val);
if (v < 0 || v == 0)
return true;
return false;
}
inline bool operator >= (const BigFloat &f)
{
int v = mpfr_cmp(this->val, f.val);
if (v > 0 || v == 0)
return true;
return false;
}
inline bool operator < (const double d)
{
if (mpfr_cmp_d(this->val, d) < 0)
return true;
return false;
}
inline bool operator > (const double d)
{
if (mpfr_cmp_d(this->val, d) > 0)
return true;
return false;
}
inline bool operator <= (const double d)
{
int v = mpfr_cmp_d(this->val, d);
if (v < 0 || v == 0)
return true;
return false;
}
inline bool operator >= (const double d)
{
int v = mpfr_cmp_d(this->val, d);
if (v > 0 || v == 0)
return true;
return false;
}
};
class Vec2bf {
public:
BigFloat x;
BigFloat y;
Vec2bf(double x, double y, uint32_t prec);
};
}