Skip to content

Commit

Permalink
Added export roots
Browse files Browse the repository at this point in the history
The first 6 roots are static now. The rest is being generated at runtime.
  • Loading branch information
chrizbee committed Jun 29, 2019
1 parent a3b9cd0 commit e5a1065
Show file tree
Hide file tree
Showing 24 changed files with 457 additions and 247 deletions.
2 changes: 1 addition & 1 deletion NewtonFractal.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ QT += core gui widgets concurrent
TARGET = NewtonFractal
TEMPLATE = app
CONFIG += c++14 debug_and_release
VERSION = 1.4.0
VERSION = 1.4.1
DEFINES += APP_VERSION=\\\"$$VERSION\\\"

CONFIG(release, debug|release) {
Expand Down
12 changes: 5 additions & 7 deletions resources.qrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<RCC>
<qresource prefix="/">
<file>resources/icons/iteration.png</file>
<file>resources/icons/blue.png</file>
<file>resources/icons/cyan.png</file>
<file>resources/icons/green.png</file>
<file>resources/icons/magenta.png</file>
<file>resources/icons/red.png</file>
<file>resources/icons/yellow.png</file>
<file>resources/icons/save.png</file>
<file>resources/icons/user1.png</file>
<file>resources/icons/user2.png</file>
Expand All @@ -26,7 +20,11 @@
<file>resources/icons/position.png</file>
<file>resources/icons/icon.ico</file>
<file>resources/icons/icon.png</file>
<file>resources/icons/add.png</file>
<file>resources/icons/mirrorx.png</file>
<file>resources/icons/mirrory.png</file>
<file>resources/icons/remove.png</file>
<file>resources/icons/color.png</file>
<file>resources/icons/import.png</file>
<file>resources/icons/root.png</file>
</qresource>
</RCC>
Binary file removed resources/icons/add.png
Binary file not shown.
Binary file removed resources/icons/blue.png
Binary file not shown.
Binary file added resources/icons/color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed resources/icons/cyan.png
Binary file not shown.
Binary file removed resources/icons/green.png
Binary file not shown.
Binary file added resources/icons/import.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed resources/icons/magenta.png
Binary file not shown.
Binary file added resources/icons/mirrorx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/mirrory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed resources/icons/red.png
Binary file not shown.
Binary file modified resources/icons/remove.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/icons/root.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed resources/icons/yellow.png
Binary file not shown.
4 changes: 4 additions & 0 deletions src/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include <complex>
#include <QVector>
#include <QColor>

typedef std::complex<double> complex;

static const QColor colors[6] = { Qt::red, Qt::green, Qt::blue, Qt::cyan, Qt::magenta, Qt::yellow };

static constexpr double PI = 3.141592653589793238463; // Pi as a constexpr

static constexpr double EPS = 1e-3; // Max error allowed
Expand All @@ -29,4 +32,5 @@ static constexpr quint16 MSI = 128; // Minimum size
static constexpr quint16 DZS = 2; // Default complex size [-DZS -> +DZS]
static constexpr double DSF = 0.5 * DZS / DSI; // Resulting size factor


#endif // DEFAULTS_H
44 changes: 41 additions & 3 deletions src/fractalwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QMouseEvent>
#include <QHBoxLayout>
#include <QDateTime>
#include <QSettings>
#include <QPainter>
#include <QAction>
#include <QIcon>
Expand Down Expand Up @@ -70,7 +71,9 @@ FractalWidget::FractalWidget(QWidget *parent) :
// Connect settingswidget signals
connect(settingsWidget_, &SettingsWidget::paramsChanged, this, &FractalWidget::updateParams);
connect(settingsWidget_, &SettingsWidget::sizeChanged, this, QOverload<const QSize &>::of(&FractalWidget::resize));
connect(settingsWidget_, &SettingsWidget::exportTo, this, &FractalWidget::exportTo);
connect(settingsWidget_, &SettingsWidget::exportImage, this, &FractalWidget::exportImage);
connect(settingsWidget_, &SettingsWidget::exportRoots, this, &FractalWidget::exportRoots);
connect(settingsWidget_, &SettingsWidget::importRoots, this, &FractalWidget::importRoots);
connect(settingsWidget_, &SettingsWidget::reset, this, &FractalWidget::reset);
connect(this, &FractalWidget::rootMoved, settingsWidget_, &SettingsWidget::moveRoot);
connect(this, &FractalWidget::zoomChanged, settingsWidget_, &SettingsWidget::changeZoom);
Expand All @@ -94,10 +97,10 @@ void FractalWidget::updateParams()
renderThread_.render(*params_);
}

void FractalWidget::exportTo(const QString &exportDir)
void FractalWidget::exportImage(const QString &dir)
{
// Export fractal to file
QString filePath = exportDir + "/fractal_" +
QString filePath = dir + "/fractal_" +
QDateTime::currentDateTime().toString("yyMMdd_HHmmss_") +
QString::number(params_->roots.size()) + "roots_" +
QString::number(params_->size.width()) + "x" + QString::number(params_->size.width()) + ".png";
Expand All @@ -106,6 +109,41 @@ void FractalWidget::exportTo(const QString &exportDir)
pixmap_.save(&f, "png");
}

void FractalWidget::exportRoots(const QString &dir)
{
// Export roots to file
quint8 rootCount = params_->roots.size();
QString filePath = dir + "/fractal_" +
QDateTime::currentDateTime().toString("yyMMdd_HHmmss_") +
QString::number(rootCount) + "roots_" +
QString::number(params_->size.width()) + "x" + QString::number(params_->size.width()) + ".roots";
QSettings rootsIni(filePath, QSettings::IniFormat, this);
rootsIni.beginGroup("Roots");
for (quint8 i = 0; i < rootCount; ++i) {
rootsIni.setValue("root" + QString::number(i), complex2string(params_->roots[i].value(), 16));
}
}

bool FractalWidget::importRoots(const QString &file)
{
// Remove old roots
quint8 rootCount = params_->roots.size();
for (quint8 i = 0; i < rootCount; ++i) {
settingsWidget_->removeRoot();
}

// Import roots from file
quint8 i = 0;
QSettings rootsIni(file, QSettings::IniFormat, this);
for (QString key : rootsIni.allKeys()) {
settingsWidget_->addRoot(string2complex(rootsIni.value(key).toString()));
++i;
}

// Return true if more than 1 roots were added
return i >= 2;
}

void FractalWidget::reset()
{
// Reset roots to be equidistant
Expand Down
4 changes: 3 additions & 1 deletion src/fractalwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class FractalWidget : public QWidget
public:
FractalWidget(QWidget *parent = nullptr);
void updateParams();
void exportTo(const QString &exportDir);
void exportImage(const QString &dir);
void exportRoots(const QString &dir);
bool importRoots(const QString &file);
void reset();

public slots:
Expand Down
34 changes: 31 additions & 3 deletions src/parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,40 @@ void Parameters::reset()
scaleDown = false;
}

QString complex2string(complex z)
complex string2complex(const QString &text)
{
// Convert string to complex
bool sign = false;
double real = 0, imag = 0;
QStringList parts = text.split('i');
QString rstr = parts.first().simplified();

// Get imag sign from first part
int si = rstr.indexOf('-', 1);
if (si > 0) {
sign = true;
rstr.replace(si, 1, ' ');
} else rstr.replace('+', ' ');

// Get real
real = rstr.simplified().toDouble();

// Get imag
if (parts.length() >= 2) {
imag = parts[1].simplified().toDouble();
if (sign) imag = -imag;
}

// Return complex number
return (complex(real, imag));
}

QString complex2string(complex z, quint8 precision)
{
// Convert complex to string
static QString complexFormat("%1 %2 i%3");
QString real = QString::number(z.real(), 'f', 2);
QString imag = QString::number(abs(z.imag()), 'f', 2);
QString real = QString::number(z.real(), 'f', precision);
QString imag = QString::number(abs(z.imag()), 'f', precision);
QString sign = z.imag() >= 0 ? "+" : "-";
return complexFormat.arg(real, sign, imag);
}
Expand Down
3 changes: 2 additions & 1 deletion src/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ struct Parameters {
QPoint orbitStart;
};

QString complex2string(complex z);
complex string2complex(const QString &text);
QString complex2string(complex z, quint8 precision = 2);
QPoint complex2point(complex z, const Parameters &params);
complex point2complex(QPoint p, const Parameters &params);
complex distance2complex(QPointF d, const Parameters &params);
Expand Down
24 changes: 1 addition & 23 deletions src/rootedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,7 @@ void RootEdit::setValue(complex value)

void RootEdit::on_editingFinished()
{
// Create complex number from string
bool sign = false;
double real = 0, imag = 0;
QStringList parts = text().split('i');
QString rstr = parts.first().simplified();

// Get imag sign from first part
int si = rstr.indexOf('-', 1);
if (si > 0) {
sign = true;
rstr.replace(si, 1, ' ');
} else rstr.replace('+', ' ');

// Get real
real = rstr.simplified().toDouble();

// Get imag
if (parts.length() >= 2) {
imag = parts[1].simplified().toDouble();
if (sign) imag = -imag;
}

// Set new root value
setValue(complex(real, imag));
setValue(string2complex(text()));
emit rootChanged();
}
Loading

0 comments on commit e5a1065

Please sign in to comment.