added 3 changesets to branch 'refs/remotes/waddlesplash-github/mandelbrot_new'
old head: a60b0085b81dca25a902807f2c5830d39d2b20c5
new head: 44380df06b4ea28da3d4595a03b72a77b15f3fa0
overview:
https://github.com/waddlesplash/haiku/compare/a60b0085b81d...44380df06b4e
----------------------------------------------------------------------------
e03eae7dc5a7: Mandelbrot: Rename the last global constants and remove some
unused ones.
bae2f4172fbc: Mandelbrot: Add 'Iterations' menu.
44380df06b4e: Mandelbrot: Add 'box-select-to-zoom' support.
[ Augustin Cavalier <waddlesplash@xxxxxxxxx> ]
----------------------------------------------------------------------------
3 files changed, 148 insertions(+), 71 deletions(-)
src/apps/mandelbrot/FractalEngine.cpp | 87 ++++++--------------
src/apps/mandelbrot/FractalEngine.h | 4 +
src/apps/mandelbrot/Mandelbrot.cpp | 128 +++++++++++++++++++++++++++---
############################################################################
Commit: e03eae7dc5a7ed21238358fe9a44365d3caeefaa
Author: Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date: Sat Jun 18 13:59:45 2016 UTC
Mandelbrot: Rename the last global constants and remove some unused ones.
----------------------------------------------------------------------------
diff --git a/src/apps/mandelbrot/FractalEngine.cpp
b/src/apps/mandelbrot/FractalEngine.cpp
index 77a2141..2041ef8 100644
--- a/src/apps/mandelbrot/FractalEngine.cpp
+++ b/src/apps/mandelbrot/FractalEngine.cpp
@@ -94,18 +94,13 @@ void FractalEngine::MessageReceived(BMessage* msg)
}
-double zReal_end = 0;
-double zImaginary_end = 0;
-
-const double juliaC_a = 0;
-const double juliaC_b = 1;
+// Magic numbers & other general constants
+const double gJuliaA = 0;
+const double gJuliaB = 1;
const uint8 gEscapeHorizon = 4;
-
const int32 gIterations = 1024;
-const double gPower = 0;
-
void FractalEngine::RenderPixel(uint32 x, uint32 y, double real,
double imaginary)
@@ -120,9 +115,10 @@ void FractalEngine::RenderPixel(uint32 x, uint32 y, double
real,
}
uint32 offsetBase = fWidth * y * 3 + x * 3;
- fRenderBuffer[offsetBase + 0] = fColorset[loc * 3 + 0];
- fRenderBuffer[offsetBase + 1] = fColorset[loc * 3 + 1];
- fRenderBuffer[offsetBase + 2] = fColorset[loc * 3 + 2];
+ loc *= 3;
+ fRenderBuffer[offsetBase + 0] = fColorset[loc + 0];
+ fRenderBuffer[offsetBase + 1] = fColorset[loc + 1];
+ fRenderBuffer[offsetBase + 2] = fColorset[loc + 2];
}
@@ -131,10 +127,7 @@ int32 FractalEngine::DoSet_Mandelbrot(double real, double
imaginary)
double zReal = 0;
double zImaginary = 0;
- int32 iterations = gIterations;
- uint8 escapeHorizon = gEscapeHorizon;
-
- for (int32 i = 0; i < iterations; i++) {
+ for (int32 i = 0; i < gIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq + (-1 * (zImaginarySq)));
@@ -146,10 +139,7 @@ int32 FractalEngine::DoSet_Mandelbrot(double real, double
imaginary)
zImaginary += imaginary;
// If it is outside the 2 unit circle...
- if ((zRealSq) + (zImaginarySq) > escapeHorizon) {
- zReal_end = zReal;
- zImaginary_end = zImaginary;
-
+ if ((zRealSq) + (zImaginarySq) > gEscapeHorizon) {
return i; // stop it from running longer
}
}
@@ -165,10 +155,7 @@ int32 FractalEngine::DoSet_BurningShip(double real, double
imaginary)
// It looks "upside down" otherwise.
imaginary = -imaginary;
- int32 iterations = gIterations;
- uint8 escapeHorizon = gEscapeHorizon;
-
- for (int32 i = 0; i < iterations; i++) {
+ for (int32 i = 0; i < gIterations; i++) {
zReal = fabs(zReal);
zImaginary = fabs(zImaginary);
@@ -183,10 +170,7 @@ int32 FractalEngine::DoSet_BurningShip(double real, double
imaginary)
zImaginary += imaginary;
// If it is outside the 2 unit circle...
- if ((zRealSq) + (zImaginarySq) > escapeHorizon) {
- zReal_end = zReal;
- zImaginary_end = zImaginary;
-
+ if ((zRealSq) + (zImaginarySq) > gEscapeHorizon) {
return i; // stop it from running longer
}
}
@@ -201,10 +185,7 @@ int32 FractalEngine::DoSet_Tricorn(double real, double
imaginary)
real = -real;
- int32 iterations = gIterations;
- uint8 escapeHorizon = gEscapeHorizon;
-
- for (int32 i = 0; i < iterations; i++) {
+ for (int32 i = 0; i < gIterations; i++) {
double znRe = zImaginary * -1;
zImaginary = zReal * -1;
zReal = znRe; // Swap the real and complex parts each time.
@@ -220,10 +201,7 @@ int32 FractalEngine::DoSet_Tricorn(double real, double
imaginary)
zImaginary += imaginary;
// If it is outside the 2 unit circle...
- if ((zRealSq) + (zImaginarySq) > escapeHorizon) {
- zReal_end = zReal;
- zImaginary_end = zImaginary;
-
+ if ((zRealSq) + (zImaginarySq) > gEscapeHorizon) {
return i; // stop it from running longer
}
}
@@ -236,13 +214,10 @@ int32 FractalEngine::DoSet_Julia(double real, double
imaginary)
double zReal = real;
double zImaginary = imaginary;
- double muRe = juliaC_a;
- double muIm = juliaC_b;
+ double muRe = gJuliaA;
+ double muIm = gJuliaB;
- int32 iterations = gIterations;
- uint8 escapeHorizon = gEscapeHorizon;
-
- for (int32 i = 0; i < iterations; i++) {
+ for (int32 i = 0; i < gIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq + (-1 * (zImaginarySq)));
@@ -254,10 +229,7 @@ int32 FractalEngine::DoSet_Julia(double real, double
imaginary)
zImaginary += muIm;
// If it is outside the 2 unit circle...
- if ((zRealSq) + (zImaginarySq) > escapeHorizon) {
- zReal_end = zReal;
- zImaginary_end = zImaginary;
-
+ if ((zRealSq) + (zImaginarySq) > gEscapeHorizon) {
return i; // stop it from running longer
}
}
@@ -274,10 +246,7 @@ int32 FractalEngine::DoSet_OrbitTrap(double real, double
imaginary)
double distance = 0;
double lineDist = 0;
- int32 iterations = gIterations;
- uint8 escapeHorizon = gEscapeHorizon;
-
- for (int32 i = 0; i < iterations; i++) {
+ for (int32 i = 0; i < gIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq + (-1 * (zImaginarySq)));
@@ -295,9 +264,7 @@ int32 FractalEngine::DoSet_OrbitTrap(double real, double
imaginary)
if (lineDist < closest)
closest = lineDist;
- if (distance > escapeHorizon) {
- zReal_end = zReal;
- zImaginary_end = zImaginary;
+ if (distance > gEscapeHorizon) {
return static_cast<int32>(floor(4 * log(4 / closest)));
}
}
@@ -310,10 +277,7 @@ int32 FractalEngine::DoSet_Multibrot(double real, double
imaginary)
double zReal = 0;
double zImaginary = 0;
- int32 iterations = gIterations;
- uint8 escapeHorizon = gEscapeHorizon;
-
- for (int32 i = 0; i < iterations; i++) {
+ for (int32 i = 0; i < gIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq * zReal - 3 * zReal * (zImaginarySq));
@@ -325,10 +289,7 @@ int32 FractalEngine::DoSet_Multibrot(double real, double
imaginary)
zImaginary += imaginary;
// If it is outside the 2 unit circle...
- if ((zRealSq) + (zImaginarySq) > escapeHorizon) {
- zReal_end = zReal;
- zImaginary_end = zImaginary;
-
+ if ((zRealSq) + (zImaginarySq) > gEscapeHorizon) {
return i; // stop it from running longer
}
}
diff --git a/src/apps/mandelbrot/FractalEngine.h
b/src/apps/mandelbrot/FractalEngine.h
index fce2384..148f321 100644
--- a/src/apps/mandelbrot/FractalEngine.h
+++ b/src/apps/mandelbrot/FractalEngine.h
@@ -35,8 +35,10 @@ private:
BMessenger fMessenger;
BBitmap* fBitmapStandby;
BBitmap* fBitmapDisplay;
+
uint16 fWidth;
uint16 fHeight;
+
uint8* fRenderBuffer;
uint32 fRenderBufferLen;
############################################################################
Commit: bae2f4172fbcfe2fec27e43f44ff980af1c2d749
Author: Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date: Sat Jun 18 14:56:43 2016 UTC
Mandelbrot: Add 'Iterations' menu.
----------------------------------------------------------------------------
diff --git a/src/apps/mandelbrot/FractalEngine.cpp
b/src/apps/mandelbrot/FractalEngine.cpp
index 2041ef8..ec3e478 100644
--- a/src/apps/mandelbrot/FractalEngine.cpp
+++ b/src/apps/mandelbrot/FractalEngine.cpp
@@ -22,6 +22,7 @@ FractalEngine::FractalEngine(BHandler* parent, BLooper*
looper)
fMessenger(parent, looper),
fBitmapStandby(NULL),
fBitmapDisplay(NULL),
+ fIterations(1024),
fWidth(0), fHeight(0),
fRenderBuffer(NULL),
fRenderBufferLen(0),
@@ -62,6 +63,10 @@ void FractalEngine::MessageReceived(BMessage* msg)
case 8: fColorset = Colorset_HighContrast; break;
}
break;
+ case MSG_SET_ITERATIONS:
+ fIterations = msg->GetUInt16("iterations", 0);
+ break;
+
case MSG_RESIZE: {
delete fBitmapStandby;
// We don't delete the "display" bitmap; the viewer now owns it
@@ -99,7 +104,6 @@ const double gJuliaA = 0;
const double gJuliaB = 1;
const uint8 gEscapeHorizon = 4;
-const int32 gIterations = 1024;
void FractalEngine::RenderPixel(uint32 x, uint32 y, double real,
@@ -127,7 +131,7 @@ int32 FractalEngine::DoSet_Mandelbrot(double real, double
imaginary)
double zReal = 0;
double zImaginary = 0;
- for (int32 i = 0; i < gIterations; i++) {
+ for (int32 i = 0; i < fIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq + (-1 * (zImaginarySq)));
@@ -155,7 +159,7 @@ int32 FractalEngine::DoSet_BurningShip(double real, double
imaginary)
// It looks "upside down" otherwise.
imaginary = -imaginary;
- for (int32 i = 0; i < gIterations; i++) {
+ for (int32 i = 0; i < fIterations; i++) {
zReal = fabs(zReal);
zImaginary = fabs(zImaginary);
@@ -185,7 +189,7 @@ int32 FractalEngine::DoSet_Tricorn(double real, double
imaginary)
real = -real;
- for (int32 i = 0; i < gIterations; i++) {
+ for (int32 i = 0; i < fIterations; i++) {
double znRe = zImaginary * -1;
zImaginary = zReal * -1;
zReal = znRe; // Swap the real and complex parts each time.
@@ -217,7 +221,7 @@ int32 FractalEngine::DoSet_Julia(double real, double
imaginary)
double muRe = gJuliaA;
double muIm = gJuliaB;
- for (int32 i = 0; i < gIterations; i++) {
+ for (int32 i = 0; i < fIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq + (-1 * (zImaginarySq)));
@@ -246,7 +250,7 @@ int32 FractalEngine::DoSet_OrbitTrap(double real, double
imaginary)
double distance = 0;
double lineDist = 0;
- for (int32 i = 0; i < gIterations; i++) {
+ for (int32 i = 0; i < fIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq + (-1 * (zImaginarySq)));
@@ -277,7 +281,7 @@ int32 FractalEngine::DoSet_Multibrot(double real, double
imaginary)
double zReal = 0;
double zImaginary = 0;
- for (int32 i = 0; i < gIterations; i++) {
+ for (int32 i = 0; i < fIterations; i++) {
double zRealSq = zReal * zReal;
double zImaginarySq = zImaginary * zImaginary;
double nzReal = (zRealSq * zReal - 3 * zReal * (zImaginarySq));
diff --git a/src/apps/mandelbrot/FractalEngine.h
b/src/apps/mandelbrot/FractalEngine.h
index 148f321..21773c5 100644
--- a/src/apps/mandelbrot/FractalEngine.h
+++ b/src/apps/mandelbrot/FractalEngine.h
@@ -21,6 +21,7 @@ public:
enum {
MSG_CHANGE_SET = 'Frct',
MSG_SET_PALETTE,
+ MSG_SET_ITERATIONS,
MSG_RESIZE,
MSG_RENDER,
MSG_RENDER_COMPLETE,
@@ -36,6 +37,7 @@ private:
BBitmap* fBitmapStandby;
BBitmap* fBitmapDisplay;
+ uint16 fIterations;
uint16 fWidth;
uint16 fHeight;
diff --git a/src/apps/mandelbrot/Mandelbrot.cpp
b/src/apps/mandelbrot/Mandelbrot.cpp
index 63b2f76..3e30ab3 100644
--- a/src/apps/mandelbrot/Mandelbrot.cpp
+++ b/src/apps/mandelbrot/Mandelbrot.cpp
@@ -178,6 +178,14 @@ public:
MSG_LIGHTNING_PALETTE,
MSG_SPRING_PALETTE,
MSG_HIGHCONTRAST_PALETTE,
+
+ MSG_ITER_128,
+ MSG_ITER_512,
+ MSG_ITER_1024,
+ MSG_ITER_4096,
+ MSG_ITER_8192,
+ MSG_ITER_12288,
+ MSG_ITER_16384
};
MandelbrotWindow(BRect frame);
~MandelbrotWindow() {}
@@ -201,6 +209,7 @@ MandelbrotWindow::MandelbrotWindow(BRect frame)
BMenuBar* menuBar = new BMenuBar("MenuBar");
BMenu* setMenu;
BMenu* paletteMenu;
+ BMenu* iterMenu;
BLayoutBuilder::Menu<>(menuBar)
.AddMenu(B_TRANSLATE("File"))
.AddItem(B_TRANSLATE("Quit"), B_QUIT_REQUESTED, 'Q')
@@ -226,11 +235,23 @@ MandelbrotWindow::MandelbrotWindow(BRect frame)
.AddItem(B_TRANSLATE("Spring"), MSG_SPRING_PALETTE)
.AddItem(B_TRANSLATE("High contrast"),
MSG_HIGHCONTRAST_PALETTE)
.End()
+ .AddMenu(B_TRANSLATE("Iterations"))
+ .GetMenu(iterMenu)
+ .AddItem("128", MSG_ITER_128)
+ .AddItem("512", MSG_ITER_512)
+ .AddItem("1024", MSG_ITER_1024)
+ .AddItem("4096", MSG_ITER_4096)
+ .AddItem("8192", MSG_ITER_8192)
+ .AddItem("12288", MSG_ITER_12288)
+ .AddItem("16384", MSG_ITER_16384)
+ .End()
.End();
setMenu->SetRadioMode(true);
setMenu->FindItem(MSG_MANDELBROT_SET)->SetMarked(true);
paletteMenu->SetRadioMode(true);
paletteMenu->FindItem(MSG_ROYAL_PALETTE)->SetMarked(true);
+ iterMenu->SetRadioMode(true);
+ iterMenu->FindItem(MSG_ITER_1024)->SetMarked(true);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.SetInsets(0)
@@ -256,6 +277,14 @@ MandelbrotWindow::MandelbrotWindow(BRect frame)
fFractalView->RedrawFractal(); \
break; \
}
+#define HANDLE_ITER(uiwhat, id) \
+ case uiwhat: { \
+ BMessage msg(FractalEngine::MSG_SET_ITERATIONS); \
+ msg.AddUInt16("iterations", id); \
+ fFractalView->fFractalEngine->PostMessage(&msg); \
+ fFractalView->RedrawFractal(); \
+ break; \
+ }
void
MandelbrotWindow::MessageReceived(BMessage* msg)
{
@@ -277,6 +306,14 @@ MandelbrotWindow::MessageReceived(BMessage* msg)
HANDLE_PALETTE(MSG_SPRING_PALETTE, 7)
HANDLE_PALETTE(MSG_HIGHCONTRAST_PALETTE, 8)
+ HANDLE_ITER(MSG_ITER_128, 128)
+ HANDLE_ITER(MSG_ITER_512, 512)
+ HANDLE_ITER(MSG_ITER_1024, 1024)
+ HANDLE_ITER(MSG_ITER_4096, 4096)
+ HANDLE_ITER(MSG_ITER_8192, 8192)
+ HANDLE_ITER(MSG_ITER_12288, 12288)
+ HANDLE_ITER(MSG_ITER_16384, 16384)
+
default:
BWindow::MessageReceived(msg);
break;
@@ -284,6 +321,7 @@ MandelbrotWindow::MessageReceived(BMessage* msg)
}
#undef HANDLE_SET
#undef HANDLE_PALETTE
+#undef HANDLE_ITER
bool
############################################################################
Commit: 44380df06b4ea28da3d4595a03b72a77b15f3fa0
Author: Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date: Sat Jun 18 17:11:28 2016 UTC
Mandelbrot: Add 'box-select-to-zoom' support.
----------------------------------------------------------------------------
diff --git a/src/apps/mandelbrot/Mandelbrot.cpp
b/src/apps/mandelbrot/Mandelbrot.cpp
index 3e30ab3..3949bc0 100644
--- a/src/apps/mandelbrot/Mandelbrot.cpp
+++ b/src/apps/mandelbrot/Mandelbrot.cpp
@@ -15,6 +15,8 @@
#include <View.h>
#include <Window.h>
+#include <algorithm>
+
#include "FractalEngine.h"
#undef B_TRANSLATION_CONTEXT
@@ -32,17 +34,30 @@ public:
virtual void AttachedToWindow();
virtual void FrameResized(float, float);
virtual void Pulse();
+
virtual void MouseDown(BPoint where);
+ virtual void MouseMoved(BPoint where, uint32 mode, const BMessage*);
+ virtual void MouseUp(BPoint where);
+
virtual void MessageReceived(BMessage* msg);
virtual void Draw(BRect updateRect);
- FractalEngine* fFractalEngine;
- void RedrawFractal();
+ void RedrawFractal();
+ FractalEngine* fFractalEngine;
private:
+ BRect GetDragFrame();
+
bool fSizeChanged;
bool fOwnBitmap;
+
+ BPoint fSelectStart;
+ BPoint fSelectEnd;
+ bool fSelecting;
+ uint32 fMouseButtons;
+
BBitmap* fDisplayBitmap;
+
double fLocationX;
double fLocationY;
double fSize;
@@ -55,11 +70,13 @@ FractalView::FractalView()
fFractalEngine(NULL),
fSizeChanged(false),
fOwnBitmap(false),
+ fSelecting(false),
fDisplayBitmap(NULL),
fLocationX(0),
fLocationY(0),
fSize(0.005)
{
+ SetHighColor(make_color(255, 255, 255, 255));
}
@@ -102,19 +119,68 @@ void FractalView::Pulse()
RedrawFractal();
}
+BRect FractalView::GetDragFrame()
+{
+ BRect dragZone = BRect(std::min(fSelectStart.x, fSelectEnd.x),
+ std::min(fSelectStart.y, fSelectEnd.y),
+ std::max(fSelectStart.x, fSelectEnd.x),
+ std::max(fSelectStart.y, fSelectEnd.y)),
+ frame = Frame();
+ float width = dragZone.Width(),
+ height = width * (frame.Height() / frame.Width());
+
+ float x1 = fSelectStart.x, y1 = fSelectStart.y, x2, y2;
+ if (fSelectStart.x < fSelectEnd.x)
+ x2 = x1 + width;
+ else
+ x2 = x1 - width;
+ if (fSelectStart.y < fSelectEnd.y)
+ y2 = y1 + height;
+ else
+ y2 = y1 - height;
+ return BRect(x1, y1, x2, y2);
+}
+
void FractalView::MouseDown(BPoint where)
{
- uint32 buttons;
- GetMouse(&where, &buttons);
+ fSelecting = true;
+ fSelectStart = where;
+ GetMouse(NULL, &fMouseButtons);
+}
+
+void FractalView::MouseMoved(BPoint where, uint32 mode, const BMessage*)
+{
+ if (fSelecting) {
+ fSelectEnd = where;
+ Invalidate();
+ }
+}
+
+
+void FractalView::MouseUp(BPoint where)
+{
BRect frame = Frame();
- fLocationX = ((where.x - frame.Width() / 2) * fSize + fLocationX);
- fLocationY = ((where.y - frame.Height() / 2) * -fSize + fLocationY);
- if (buttons & B_PRIMARY_MOUSE_BUTTON)
- fSize /= 2;
- else
- fSize *= 2;
+ fSelecting = false;
+ if (fabs(fSelectStart.x - where.x) > 4) {
+ fSelectEnd = where;
+ BRect dragFrame = GetDragFrame();
+ BPoint lt = dragFrame.LeftTop();
+ float centerX = lt.x + dragFrame.Width() / 2,
+ centerY = lt.y + dragFrame.Height() / 2;
+ fLocationX = ((centerX - frame.Width() / 2) * fSize +
fLocationX);
+ fLocationY = ((centerY - frame.Height() / 2) * -fSize +
fLocationY);
+
+ fSize = (dragFrame.Width() * fSize) / frame.Width();
+ } else {
+ fLocationX = ((where.x - frame.Width() / 2) * fSize +
fLocationX);
+ fLocationY = ((where.y - frame.Height() / 2) * -fSize +
fLocationY);
+ if (fMouseButtons & B_PRIMARY_MOUSE_BUTTON)
+ fSize /= 2;
+ else
+ fSize *= 2;
+ }
RedrawFractal();
}
@@ -152,6 +218,10 @@ void FractalView::RedrawFractal()
void FractalView::Draw(BRect updateRect)
{
DrawBitmap(fDisplayBitmap, updateRect, updateRect);
+
+ if (fSelecting) {
+ StrokeRect(GetDragFrame());
+ }
}