From e16cfcd0c5844efb5b39c15cf3a47094ff6c4140 Mon Sep 17 00:00:00 2001 From: koma Date: Mon, 4 Jul 2022 19:22:36 +0200 Subject: [PATCH] initial commit --- Makefile | 2 + graphics.cpp | 49 +++++++++++++++ graphics.h | 23 +++++++ main.cpp | 6 ++ mandelbrot.cpp | 18 ++++++ mandelbrot.h | 14 +++++ utils.cpp | 54 ++++++++++++++++ utils.h | 168 +++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 334 insertions(+) create mode 100644 Makefile create mode 100644 graphics.cpp create mode 100644 graphics.h create mode 100644 main.cpp create mode 100644 mandelbrot.cpp create mode 100644 mandelbrot.h create mode 100644 utils.cpp create mode 100644 utils.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f80f620 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + g++ graphics.cpp main.cpp -o mandelbrot diff --git a/graphics.cpp b/graphics.cpp new file mode 100644 index 0000000..2cde550 --- /dev/null +++ b/graphics.cpp @@ -0,0 +1,49 @@ +#include "graphics.h" +#include "utils.h" +#include +#include +#include + +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); + } + } +} diff --git a/graphics.h b/graphics.h new file mode 100644 index 0000000..10d30bc --- /dev/null +++ b/graphics.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include +#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 diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..4c82edf --- /dev/null +++ b/main.cpp @@ -0,0 +1,6 @@ +#include "graphics.h" + +int main() +{ + return 0; +} diff --git a/mandelbrot.cpp b/mandelbrot.cpp new file mode 100644 index 0000000..dd79db3 --- /dev/null +++ b/mandelbrot.cpp @@ -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; +} diff --git a/mandelbrot.h b/mandelbrot.h new file mode 100644 index 0000000..994fdbe --- /dev/null +++ b/mandelbrot.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include +#include "utils.h" + +using namespace Util; + +namespace Mandelbrot +{ + +uint32_t mandelbrot(const BigFloat &c, Vec2bf &ret); + +} diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..cfbb93d --- /dev/null +++ b/utils.cpp @@ -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); +*/ diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..dda874d --- /dev/null +++ b/utils.h @@ -0,0 +1,168 @@ +#pragma once + +#include +#include +#include + +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); +}; + +}