我写了一个应该绘制Mandelbrot分形的程序。不幸的是,它似乎喝醉了。这是输出:
相关功能:
#include <memory>
struct ImageDeleter
{
void operator()(void* ptr) const;
};
class Image
{
public: // but don't touch it
std::unique_ptr<void, ImageDeleter> internal;
private:
/* private operations on surface */
public:
/* public operations */
void save(const std::string& path) const;
Image(int width, int height);
};
// EXAMPLE USAGE
// Image img(640, 480);
// img.save("aaa.bmp");
// IN THE DEFINITION FILE
#include <SDL2/SDL.h>
namespace detail
{
SDL_Surface* as_surface(const Image& img)
{
return static_cast<SDL_Surface*>(img.internal.get());
}
}
void ImageDeleter::operator()(void* ptr) const
{
SDL_FreeSurface(static_cast<SDL_Surface*>(ptr));
}
Image::Image(int width, int height) :
internal(SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0))
{
}
void Image::save(const std::string& path) const
{
SDL_SaveBMP(detail::as_surface(*this), path.c_str());
}
我相信void drawMandelbrot(float x, float y, float width, float height, float delta) {
for (float currentX = -2; currentX < 2; currentX += delta) {
for (float currentY = -2; currentY < 2; currentY += delta) {
Complex z(0, 0);
Complex c(currentX, currentY);
int iterations = 0;
do {
z = z * z + c;
++iterations;
}
while (z.getAbsoluteValue() <= 2 && iterations < MANDELBROT_ITERATION_LIMIT);
ALLEGRO_COLOR pixelColor;
float pixelX = (currentX + 2) / 4 * width;
float pixelY = (currentY + 2) / 4 * width;
if (z.getAbsoluteValue() <= 2) {
pixelColor = blackColor;
// Commented because you might not want this much junk in your terminal
//std::cout << "Stayed small! " << z.toString() << std::endl;
} else {
pixelColor = al_color_hsv(iterations / MANDELBROT_ITERATION_LIMIT, 1.0f, 1.0f);
// idem
//std::cout << "Blew up! " << z.toString() << std::endl;
}
al_draw_pixel(pixelX, pixelY, pixelColor);
}
}
}
方法是正确的,我怀疑上述函数中存在逻辑错误。检查上面(注释的)std :: cout语句的输出时,输出中复数的绝对值确实小于2,但图像看起来并不像绘制的mandelbrot集。
错误的情况在哪里?
编辑历史记录中包含mcve。
答案 0 :(得分:1)
您对复数模量的计算不正确。
float Complex::getAbsoluteValue() const {
return sqrt(real * real + imaginary + imaginary);
}
您已删除帖子的这一部分,但应该说
float Complex::getAbsoluteValue() const {
return sqrt(real * real + imaginary * imaginary);
}