From bffb0aae8a8ae14cec27bd4e8dba5d9a6049fec9 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 31 Dec 2019 14:39:10 +0100 Subject: [PATCH] + mandelbrot visualisation --- main/main.cpp | 2 +- src/MathFunctions.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++ src/MathFunctions.hpp | 6 +++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/MathFunctions.cpp create mode 100644 src/MathFunctions.hpp diff --git a/main/main.cpp b/main/main.cpp index fe93dbf..7bdb4e8 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1 +1 @@ -#include "internal.hpp" #include "vkvm.hpp" #include "../src/Bitmap.hpp" #include //time to sleep for in seconds constexpr int sleepTime = 10; void displayImage(const std::string &file, vkvm::GraphicMode graphicMode) { vkvm::setMode(graphicMode); Bitmap image(file); vkvm::setWidth(image.getWidth()); vkvm::setHeight(image.getHeight()); for (int x = 0; x < vkvm::getWidth(); x++) { for (int y = 0; y < vkvm::getHeight(); y++) { unsigned int hex = image.getPixel(x, y); vkvm::Color color = vkvm::Color(hex); vkvm::setPixel(x, y, color); } } vkvm::callEvent(vkvm::EventType::Redraw); } void displayImage(const std::string &file) { displayImage(file, vkvm::GraphicMode::RGB); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::Gray_256); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::TwoColors); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); } int main() { bool running = true; vkvm::initialize(0); //vkvm::setLogLevel(vkvm::LogLevel::DEBUG); while(running) { displayImage("../res/P6.bmp"); sleep(sleepTime); vkvm::setBackgroundColor(vkvm::red); vkvm::setForegroundColor(vkvm::blue); vkvm::callEvent(vkvm::EventType::Redraw); vkvm::setBackgroundColor(vkvm::black); vkvm::setForegroundColor(vkvm::white); sleep(sleepTime); displayImage("../res/P8.bmp"); displayImage("../res/P9.bmp"); displayImage("../res/P10.bmp"); vkvm::setText("Hello World"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::FuturisticBlack); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); vkvm::setText("Hello World, lorem ipsum dolor sit amet"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::ProFontIIX); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); } } \ No newline at end of file +#include "internal.hpp" #include "vkvm.hpp" #include "../src/Bitmap.hpp" #include "../src/MathFunctions.hpp" #include //time to sleep for in seconds constexpr int sleepTime = 10; void displayImage(const std::string &file, vkvm::GraphicMode graphicMode) { vkvm::setMode(graphicMode); Bitmap image(file); vkvm::setWidth(image.getWidth()); vkvm::setHeight(image.getHeight()); for (int x = 0; x < vkvm::getWidth(); x++) { for (int y = 0; y < vkvm::getHeight(); y++) { unsigned int hex = image.getPixel(x, y); vkvm::Color color = vkvm::Color(hex); vkvm::setPixel(x, y, color); } } } void setColor(vkvm::Color color) { for (int x = 0; x < vkvm::getWidth(); ++x) { for (int y = 0; y < vkvm::getHeight(); ++y) { vkvm::setPixel(x, y, color); } } } void displayImage(const std::string &file) { displayImage(file, vkvm::GraphicMode::RGB); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::Gray_256); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); vkvm::setMode(vkvm::GraphicMode::TwoColors); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); } int main() { bool running = true; vkvm::initialize(0); while(running) { displayImage("../res/P6.bmp"); sleep(sleepTime); vkvm::setBackgroundColor(vkvm::red); vkvm::setForegroundColor(vkvm::blue); vkvm::callEvent(vkvm::EventType::Redraw); sleep(sleepTime); vkvm::setBackgroundColor(vkvm::black); vkvm::setForegroundColor(vkvm::white); vkvm::callEvent(vkvm::EventType::Redraw); displayImage("../res/P8.bmp"); displayImage("../res/P9.bmp"); displayImage("../res/P10.bmp"); vkvm::setText("Hello World"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::FuturisticBlack); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); vkvm::setText("Hello World, lorem ipsum dolor sit amet"); sleep(sleepTime); vkvm::setFont(vkvm::FontType::ProFontIIX); vkvm::callEvent(vkvm::EventType::RenderText); sleep(sleepTime); mandelbrot(); sleep(sleepTime); sleep(sleepTime); setColor(vkvm::black); sleep(sleepTime); } } \ No newline at end of file diff --git a/src/MathFunctions.cpp b/src/MathFunctions.cpp new file mode 100644 index 0000000..fb3af1b --- /dev/null +++ b/src/MathFunctions.cpp @@ -0,0 +1,86 @@ +#include "vkvm.hpp" +#include +#include +#include +#include +#include +#include + +void sinus() { + for (int x = 0; x < vkvm::getWidth(); ++x) { + double y = 300 * sin(x); + vkvm::log(vkvm::LogLevel::DEBUG, y); + vkvm::setPixel(x, static_cast(y), vkvm::getForegroundColor()); + } +} + +long double map(long double value, long double inputMin, long double inputMax, long double outputMin, long double outputMax) { + return (value - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin; +} + +void mandelbrot() { + vkvm::setWidth(600); + vkvm::setHeight(800); + + int width = vkvm::getWidth(); + int height = vkvm::getHeight(); + + long double min = -2.84; + long double max = 1; + int maxIterations = 200; + double factor = 1; + + for (int count = 0; count < 10000; count++) { + max -= 0.1 * factor; + min += 0.15 * factor; + factor *= 0.9349; + maxIterations += 5; + + if(count > 30) { + maxIterations *= 1.02; + } + + for (int x = 0; x < width; ++x) { + for (int y = 0; y < height; ++y) { + + long double xMap = map(x, 0, width, min, max); + long double yMap = map(y, 0, height, min, max); + + long double xInitial = xMap; + long double yInitial = yMap; + + int n = 0; + + for (int i = 0; i < maxIterations; ++i) { + + long double x1 = xMap * xMap - yMap * yMap; + long double y1 = 2 * xMap * yMap; + + xMap = x1 + xInitial; + yMap = y1 + yInitial; + + if ((xMap + yMap) > 2) { + break; + } + + + n++; + } + + int brightness = map(n, 0, maxIterations, 0, 255); + if (n == maxIterations || (brightness < 20)) { + brightness = 0; + } + + int red = map(brightness * brightness, 0, 255 * 255, 0, 255); + int green = brightness; + int blue = map(sqrt(brightness), 0, sqrt(255), 0, 255); + + + vkvm::setPixel(x, y, vkvm::Color(red, green, blue)); + + } + } + vkvm::callEvent(vkvm::EventType::Redraw); + } +} diff --git a/src/MathFunctions.hpp b/src/MathFunctions.hpp new file mode 100644 index 0000000..5e6c111 --- /dev/null +++ b/src/MathFunctions.hpp @@ -0,0 +1,6 @@ +#ifndef DEMO_MATHFUNCTIONS_H +#define DEMO_MATHFUNCTIONS_H + +void sinus(); +void mandelbrot(); +#endif