finalcut/examples/mandelbrot.cpp

171 lines
4.9 KiB
C++
Raw Normal View History

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 *
* *
* Copyright 2015-2019 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
#include <final/final.h>
2015-05-25 23:39:09 +02:00
using finalcut::FPoint;
using finalcut::FSize;
2015-05-25 23:39:09 +02:00
//----------------------------------------------------------------------
// class Mandelbrot
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class Mandelbrot : public finalcut::FDialog
2015-05-25 23:39:09 +02:00
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit Mandelbrot (finalcut::FWidget* = nullptr);
2017-09-11 03:06:02 +02:00
// Destructor
~Mandelbrot();
2017-09-11 03:06:02 +02:00
// Event handlers
virtual void onKeyPress (finalcut::FKeyEvent*) override;
2018-12-31 06:18:39 +01:00
virtual void onClose (finalcut::FCloseEvent*) override;
2017-09-11 03:06:02 +02:00
private:
// Methods
2018-12-31 06:18:39 +01:00
virtual void draw() override;
virtual void adjustSize() override;
2015-05-25 23:39:09 +02:00
};
#pragma pack(pop)
//----------------------------------------------------------------------
Mandelbrot::Mandelbrot (finalcut::FWidget* parent)
: finalcut::FDialog(parent)
2015-05-25 23:39:09 +02:00
{
setText ("Mandelbrot set");
}
//----------------------------------------------------------------------
Mandelbrot::~Mandelbrot()
2015-09-22 04:18:20 +02:00
{ }
2015-05-25 23:39:09 +02:00
//----------------------------------------------------------------------
void Mandelbrot::draw()
{
int iter, max_iter;
int Cols, Lines, xoffset, yoffset, current_line;
double x, y, xtemp, x0, y0, dX, dY;
double x_min, x_max, y_min, y_max;
finalcut::FDialog::draw();
2015-05-25 23:39:09 +02:00
x_min = -2.20;
2015-05-26 23:08:46 +02:00
x_max = 1.00;
2015-05-25 23:39:09 +02:00
y_min = -1.05;
y_max = 1.05;
max_iter = 99;
xoffset = 2;
yoffset = 2;
current_line = 0;
Cols = int(getClientWidth());
Lines = int(getClientHeight());
2015-05-25 23:39:09 +02:00
dX = (x_max - x_min) / (Cols - 1);
dY = (y_max - y_min) / Lines;
for (y0 = y_min; y0 < y_max && current_line < Lines; y0 += dY)
2015-05-25 23:39:09 +02:00
{
current_line++;
print() << FPoint(xoffset, yoffset + current_line);
2015-05-25 23:39:09 +02:00
for (x0 = x_min; x0 < x_max; x0 += dX)
2015-05-25 23:39:09 +02:00
{
x = 0.0;
y = 0.0;
iter = 0;
while ( x * x + y * y < 4 && iter < max_iter )
2015-05-25 23:39:09 +02:00
{
xtemp = x * x - y * y + x0;
y = 2 * x * y + y0;
2015-05-25 23:39:09 +02:00
x = xtemp;
iter++;
}
2015-05-25 23:39:09 +02:00
if ( iter < max_iter )
setColor(finalcut::fc::Black, iter % 16);
2015-05-25 23:39:09 +02:00
else
setColor(finalcut::fc::Black, 0);
2015-05-25 23:39:09 +02:00
print(' ');
}
}
}
//----------------------------------------------------------------------
void Mandelbrot::onKeyPress (finalcut::FKeyEvent* ev)
2015-05-25 23:39:09 +02:00
{
if ( ! ev )
return;
if ( ev->key() == 'q' )
{
close();
ev->accept();
}
else
finalcut::FDialog::onKeyPress(ev);
2015-05-25 23:39:09 +02:00
}
//----------------------------------------------------------------------
void Mandelbrot::onClose (finalcut::FCloseEvent* ev)
2015-05-25 23:39:09 +02:00
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
2015-05-25 23:39:09 +02:00
}
//----------------------------------------------------------------------
void Mandelbrot::adjustSize()
{
std::size_t h = getDesktopHeight() - 1;
std::size_t w = getDesktopWidth() - 10;
setGeometry(FPoint(6, 1), FSize(w, h), false);
finalcut::FDialog::adjustSize();
2015-05-25 23:39:09 +02:00
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
finalcut::FApplication app(argc, argv);
2015-05-25 23:39:09 +02:00
// Create a simple dialog box
Mandelbrot mb(&app);
mb.setGeometry (FPoint(6, 1), FSize(70, 23));
2015-05-25 23:39:09 +02:00
mb.setShadow();
// Set the mandelbrot object as main widget
2015-05-25 23:39:09 +02:00
app.setMainWidget(&mb);
// Show and start the application
2015-05-25 23:39:09 +02:00
mb.show();
return app.exec();
}