P
P
Pavel2016-03-19 09:01:05
Programming
Pavel, 2016-03-19 09:01:05

How to generate Sierpinski triangle using IFS?

Hello. Here 's a link to a system of iterable functions. I tried to implement this method in my program, here is the part that is responsible for applying the transformations:

void MainWindow::CreateIFS()
{
    const int ifs_cnt = 3;
    _ifs.resize(ifs_cnt);
    _ifs[0] = QMatrix(0.5, 0, 0, 0.5, 0, 0.5);
    _ifs[1] = QMatrix(0.5, 0, 0, 0.5, -0.25, 0);
    _ifs[1] = QMatrix(0.5, 0, 0, 0.5, 0.25, 0);

}

void MainWindow::ApplyIFS()
{
    //Применить преобразование
    QImage img_copy = _img.copy();
    for (int y = 0; y < img_copy.height(); ++y) {
        for (int x = 0; x < img_copy.width(); ++x) {
            int pix_id = img_copy.pixelIndex(x,y);
            if (pix_id == _black_color_id) {
                _img.setPixel(x, y, _white_color_id);

                for (const QMatrix &mtr: _ifs) {
                    double new_x = -1, new_y = -1;
                    mtr.map(x, y, &new_x, &new_y);
                    new_x = qRound(new_x); new_y = qRound(new_y);
                    bool is_x_in_range = 0 <= new_x && new_x < _img.width();
                    bool is_y_in_range = 0 <= new_y && new_y < _img.height();
                    if (is_x_in_range && is_y_in_range) {
                        _img.setPixel(new_x, new_y, _black_color_id);
                    }
                }
            }
        }
    }
}

When running on a black triangle, I did not get the desired result, since after compressing the original triangle according to these formulas, it almost does not shift (shift components < 1). The question is: how then are these formulas supposed to be applied?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question