finalcut/examples/mandelbrot.cpp

148 lines
3.3 KiB
C++
Raw Normal View History

// File: mandelbrot.cpp
2015-05-25 23:39:09 +02:00
#include <final/fapplication.h>
#include <final/fdialog.h>
#include <final/fmessagebox.h>
2015-05-25 23:39:09 +02:00
//----------------------------------------------------------------------
// class Mandelbrot
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class Mandelbrot : public FDialog
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit Mandelbrot (FWidget* = 0);
2017-09-11 03:06:02 +02:00
// Destructor
~Mandelbrot();
2017-09-11 03:06:02 +02:00
// Event handlers
void onAccel (FAccelEvent*);
void onClose (FCloseEvent*);
2017-09-11 03:06:02 +02:00
private:
// Methods
virtual void draw();
void adjustSize();
2015-05-25 23:39:09 +02:00
};
#pragma pack(pop)
//----------------------------------------------------------------------
2015-10-10 04:01:22 +02:00
Mandelbrot::Mandelbrot (FWidget* parent)
: 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;
FDialog::draw();
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;
2015-05-25 23:39:09 +02:00
Cols = getClientWidth();
Lines = getClientHeight();
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++;
setPrintPos (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(fc::Black, iter % 16);
2015-05-25 23:39:09 +02:00
else
setColor(fc::Black, 0);
2015-05-25 23:39:09 +02:00
print(' ');
}
}
}
//----------------------------------------------------------------------
void Mandelbrot::onAccel (FAccelEvent* ev)
{
close();
ev->accept();
}
//----------------------------------------------------------------------
2015-09-20 05:44:50 +02:00
void Mandelbrot::onClose (FCloseEvent* ev)
2015-05-25 23:39:09 +02:00
{
2015-09-22 04:18:20 +02:00
int ret = FMessageBox::info ( this, "Quit"
, "Do you really want\n"
"to quit the program ?"
, FMessageBox::Yes
, FMessageBox::No );
2015-05-25 23:39:09 +02:00
if ( ret == FMessageBox::Yes )
2015-09-20 05:44:50 +02:00
ev->accept();
2015-05-25 23:39:09 +02:00
else
2015-09-20 05:44:50 +02:00
ev->ignore();
2015-05-25 23:39:09 +02:00
}
//----------------------------------------------------------------------
void Mandelbrot::adjustSize()
{
int h = getParentWidget()->getHeight() - 1;
int w = getParentWidget()->getWidth() - 10;
2015-05-25 23:39:09 +02:00
setGeometry(6, 1, w, h, false);
FDialog::adjustSize();
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
FApplication app(argc, argv);
// Create a simple dialog box
Mandelbrot mb(&app);
mb.setGeometry (6, 1, 70, 23);
mb.addAccelerator('q'); // press 'q' to quit
mb.setShadow();
app.setMainWidget(&mb);
mb.show();
return app.exec();
}