Skip to content

Commit

Permalink
generic attractor wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Sep 27, 2023
1 parent a4e8ab4 commit 44aac16
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ add_library(clouds OBJECT
acidwarp.cpp
acidworm.cpp
hopalong.cpp
attractor.cpp
)

target_include_directories(clouds PRIVATE .)
Expand Down
2 changes: 2 additions & 0 deletions art.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ void Art::clear() {
fill0(m_pixels);
pixels_drawn = 0;
pixels_discarded = 0;
if (pb)
pb->buffer.clear();
}

void Art::render_pixel_buffer(uint32_t* screen) {
Expand Down
2 changes: 2 additions & 0 deletions artfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "acidwarp.h"
#include "acidworm.h"
#include "hopalong.h"
#include "attractor.h"


ArtFactory::ArtFactory() {
Expand All @@ -25,6 +26,7 @@ ArtFactory::ArtFactory() {
add_art<AcidWarp>("AcidWarp");
add_art<AcidWorm>("AcidWorm");
add_art<Hopalong>("Hopalong");
add_art<Attractor>("Attractor");

vc = VectorCombo("Art", art_items);
vc.set_index(1);
Expand Down
104 changes: 104 additions & 0 deletions attractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

#include "imgui_elements.h"
#include "attractor.h"
#include "random.h"
// from yarandom.h
#define LRAND() ((long) (xoshiro256plus() & 0x7fffffff))
#define MAXRAND (2147483648.0) /* unsigned 1<<31 as a float */
#define balance_rand(v) ((LRAND()/MAXRAND*(v))-((v)/2)) /* random around 0 */

#include <math.h>

// https://examples.holoviz.org/attractors/attractors.html

void Attractor::init()
{
int centerx, centery;
centerx = w / 2;
centery = h / 2;

double range = sqrt((double) centerx * centerx +
(double) centery * centery) / (1.0 + LRAND() / MAXRAND);
ai = aj = 0.0;
/*
inc = (int) ((LRAND() / MAXRAND) * 200) - 100;
a = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
b = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
if (LRAND() & 1)
c = ((LRAND() / MAXRAND) * 2.0 - 1.0) * range / 20.0;
else
c = 0.0;
*/
}


void Attractor::draw()
{

}


bool Attractor::render(uint32_t *p)
{
int points_per_frame = 1024*128;
int x, y;
double oldi, oldj;

for (int i=0; i < points_per_frame && count < pixel_buffer_maximum; ++i, ++count) {
oldj = aj;
oldi = ai + inc;
aj = a - ai;
ai = oldj + (ai < 0
? sqrt(fabs(b * oldi - c))
: -sqrt(fabs(b * oldi - c))
);
x = w/2 + (int) (ai + aj);
y = h/2 - (int) (ai - aj);

drawdot(x, y, pal.get_color((pixel_buffer_maximum - count)>>2));
}

return false;
}


bool Attractor::render_gui ()
{
bool up = false;

//up |= ScrollableSliderInt("op", &op, 0, 10, "%d", 1);
/*
if (ScrollableSliderInt("iterations kcount", &kcount, 0, 1024, "%d", 1)) {
count = kcount * 1024;
pal.rescale(count >> 1);
}
*/

up |= ScrollableSliderDouble("inc", &inc, -200, 200, "%.4f", 0.0001);
up |= ScrollableSliderDouble("a", &a, -20, 20, "%.4f", 0.000001);
up |= ScrollableSliderDouble("b", &b, -20, 20, "%.4f", 0.000001);
up |= ScrollableSliderDouble("c", &c, -20, 20, "%.4f", 0.000001);
up |= ScrollableSliderDouble("d", &d, -20, 20, "%.4f", 0.000001);


up |= pal.RenderGui();

//ImGui::Text("count %d, op %d", count, op);


if (up) {
resize(w, h);
}

return false;
}

void Attractor::resize(int _w, int _h) {
default_resize(_w, _h);

pal.rescale(pixel_buffer_maximum >> 2);
count = 0;

//clear();
init();
}
32 changes: 32 additions & 0 deletions attractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "art.hpp"
#include "imgui.h"
#include <vector>

#include "settings.hpp"



class Attractor : public Art {
public:
Attractor()
: Art("strange attractors") {
//use_pixel_buffer = true;
pixel_buffer_maximum = 1024*1024;
}

private:
virtual bool render_gui() override;
virtual void resize(int _w, int _h) override;
virtual bool render(uint32_t *p) override;

void init();
void draw();

int kcount = 16, count = 0;
PaletteSetting pal;

double a = 1, b = 2, c = 3, d = 4;
double ai, aj;
double inc = 1;

};

0 comments on commit 44aac16

Please sign in to comment.