2017-11-04 07:03:53 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* mandelbrot.cpp - Shows a ASCII based Mandelbrot set *
|
|
|
|
* *
|
|
|
|
* This file is part of the Final Cut widget toolkit *
|
|
|
|
* *
|
2020-01-20 21:40:00 +01:00
|
|
|
* Copyright 2015-2020 Markus Gans *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
|
|
|
* The Final Cut is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 3 of *
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* The Final Cut is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this program. If not, see *
|
|
|
|
* <http://www.gnu.org/licenses/>. *
|
|
|
|
***********************************************************************/
|
2015-05-25 23:39:09 +02:00
|
|
|
|
2017-10-31 00:41:59 +01:00
|
|
|
#include <final/final.h>
|
2015-05-25 23:39:09 +02:00
|
|
|
|
2019-04-26 21:06:04 +02:00
|
|
|
namespace fc = finalcut::fc;
|
2019-01-21 03:42:18 +01:00
|
|
|
using finalcut::FPoint;
|
|
|
|
using finalcut::FSize;
|
|
|
|
|
2017-10-02 07:32:33 +02:00
|
|
|
|
2015-05-25 23:39:09 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class Mandelbrot
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2020-04-14 23:46:42 +02:00
|
|
|
class Mandelbrot final : public finalcut::FDialog
|
2015-05-25 23:39:09 +02:00
|
|
|
{
|
2017-09-11 03:06:02 +02:00
|
|
|
public:
|
|
|
|
// Constructor
|
2018-12-10 01:48:26 +01:00
|
|
|
explicit Mandelbrot (finalcut::FWidget* = nullptr);
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
// Destructor
|
2020-02-19 21:59:13 +01:00
|
|
|
~Mandelbrot() override;
|
2016-09-30 04:55:28 +02:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
// Event handlers
|
2019-08-06 23:45:28 +02:00
|
|
|
void onKeyPress (finalcut::FKeyEvent*) override;
|
|
|
|
void onClose (finalcut::FCloseEvent*) override;
|
2016-11-02 00:37:58 +01:00
|
|
|
|
2017-09-11 03:06:02 +02:00
|
|
|
private:
|
|
|
|
// Methods
|
2019-08-06 23:45:28 +02:00
|
|
|
void draw() override;
|
|
|
|
void adjustSize() override;
|
2015-05-25 23:39:09 +02:00
|
|
|
};
|
2019-09-08 02:04:24 +02:00
|
|
|
|
2015-05-25 23:39:09 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
Mandelbrot::Mandelbrot (finalcut::FWidget* parent)
|
|
|
|
: finalcut::FDialog(parent)
|
2015-05-25 23:39:09 +02:00
|
|
|
{
|
2020-04-19 20:38:52 +02:00
|
|
|
FDialog::setText ("Mandelbrot set");
|
2015-05-25 23:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
Mandelbrot::~Mandelbrot()
|
2015-09-22 04:18:20 +02:00
|
|
|
{ }
|
2015-05-25 23:39:09 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Mandelbrot::draw()
|
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FDialog::draw();
|
2015-05-25 23:39:09 +02:00
|
|
|
|
2020-02-02 22:34:27 +01:00
|
|
|
const double x_min{-2.20};
|
|
|
|
const double x_max{+1.00};
|
|
|
|
const double y_min{-1.05};
|
|
|
|
const double y_max{+1.05};
|
|
|
|
const int max_iter{99};
|
|
|
|
|
|
|
|
const int xoffset{2};
|
|
|
|
const int yoffset{2};
|
|
|
|
const int Cols = int(getClientWidth());
|
|
|
|
const int Lines = int(getClientHeight());
|
2019-08-25 22:16:00 +02:00
|
|
|
int current_line{0};
|
2015-05-25 23:39:09 +02:00
|
|
|
|
2020-01-20 21:40:00 +01:00
|
|
|
if ( Cols < 2 || Lines < 2 )
|
|
|
|
return;
|
|
|
|
|
2020-02-02 22:34:27 +01:00
|
|
|
const double dX = (x_max - x_min) / (Cols - 1);
|
|
|
|
const double dY = (y_max - y_min) / Lines;
|
2020-04-13 12:40:11 +02:00
|
|
|
double y0 = y_min;
|
2015-05-25 23:39:09 +02:00
|
|
|
|
2020-04-13 12:40:11 +02:00
|
|
|
while ( y0 < y_max && current_line < Lines )
|
2015-05-25 23:39:09 +02:00
|
|
|
{
|
2016-10-06 23:15:09 +02:00
|
|
|
current_line++;
|
2020-05-02 00:07:35 +02:00
|
|
|
print() << FPoint{xoffset, yoffset + current_line};
|
2020-04-13 12:40:11 +02:00
|
|
|
double x0 = x_min;
|
2015-05-25 23:39:09 +02:00
|
|
|
|
2020-04-13 12:40:11 +02:00
|
|
|
while ( x0 < x_max )
|
2015-05-25 23:39:09 +02:00
|
|
|
{
|
2019-08-25 22:16:00 +02:00
|
|
|
double x{0.0};
|
|
|
|
double y{0.0};
|
|
|
|
int iter{0};
|
2015-05-25 23:39:09 +02:00
|
|
|
|
2017-08-27 09:50:30 +02:00
|
|
|
while ( x * x + y * y < 4 && iter < max_iter )
|
2015-05-25 23:39:09 +02:00
|
|
|
{
|
2020-02-02 22:34:27 +01:00
|
|
|
const double xtemp = x * x - y * y + x0;
|
2017-08-27 09:50:30 +02:00
|
|
|
y = 2 * x * y + y0;
|
2015-05-25 23:39:09 +02:00
|
|
|
x = xtemp;
|
|
|
|
iter++;
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-25 23:39:09 +02:00
|
|
|
if ( iter < max_iter )
|
2019-04-26 21:06:04 +02:00
|
|
|
setColor(fc::Black, iter % 16);
|
2015-05-25 23:39:09 +02:00
|
|
|
else
|
2019-04-26 21:06:04 +02:00
|
|
|
setColor(fc::Black, 0);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-25 23:39:09 +02:00
|
|
|
print(' ');
|
2020-04-13 12:40:11 +02:00
|
|
|
x0 += dX;
|
2015-05-25 23:39:09 +02:00
|
|
|
}
|
2020-04-13 12:40:11 +02:00
|
|
|
|
|
|
|
y0 += dY;
|
2015-05-25 23:39:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2019-01-21 03:42:18 +01:00
|
|
|
void Mandelbrot::onKeyPress (finalcut::FKeyEvent* ev)
|
2015-05-25 23:39:09 +02:00
|
|
|
{
|
2019-01-21 03:42:18 +01:00
|
|
|
if ( ! ev )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( ev->key() == 'q' )
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
ev->accept();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
finalcut::FDialog::onKeyPress(ev);
|
2015-05-25 23:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2018-09-20 23:59:01 +02:00
|
|
|
void Mandelbrot::onClose (finalcut::FCloseEvent* ev)
|
2015-05-25 23:39:09 +02:00
|
|
|
{
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FApplication::closeConfirmationDialog (this, ev);
|
2015-05-25 23:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void Mandelbrot::adjustSize()
|
|
|
|
{
|
2020-02-19 21:59:13 +01:00
|
|
|
std::size_t h = getDesktopHeight();
|
|
|
|
std::size_t w = getDesktopWidth();
|
|
|
|
|
|
|
|
if ( h > 1 )
|
|
|
|
h--;
|
|
|
|
|
|
|
|
if ( w > 10 )
|
|
|
|
w -= 10;
|
|
|
|
|
2020-05-02 00:07:35 +02:00
|
|
|
setGeometry(FPoint{6, 1}, FSize{w, h}, false);
|
2018-09-20 23:59:01 +02:00
|
|
|
finalcut::FDialog::adjustSize();
|
2015-05-25 23:39:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// main part
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
int main (int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// Create the application object
|
2020-05-02 00:07:35 +02:00
|
|
|
finalcut::FApplication app{argc, argv};
|
2015-05-25 23:39:09 +02:00
|
|
|
|
|
|
|
// Create a simple dialog box
|
2020-05-02 00:07:35 +02:00
|
|
|
Mandelbrot mb{&app};
|
|
|
|
mb.setGeometry (FPoint{6, 1}, FSize{70, 23});
|
2015-05-25 23:39:09 +02:00
|
|
|
mb.setShadow();
|
|
|
|
|
2017-09-19 06:18:03 +02:00
|
|
|
// Set the mandelbrot object as main widget
|
2020-04-13 12:40:11 +02:00
|
|
|
finalcut::FWidget::setMainWidget(&mb);
|
2017-09-19 06:18:03 +02:00
|
|
|
|
|
|
|
// Show and start the application
|
2015-05-25 23:39:09 +02:00
|
|
|
mb.show();
|
|
|
|
return app.exec();
|
|
|
|
}
|