# Drawing Ikeda Function

**URL:** https://discourse.processing.org/t/drawing-ikeda-function/9536
**Category:** Coding Questions
**Created:** [March 22, 2019, 5:38pm UTC](https://discourse.processing.org/t/drawing-ikeda-function/9536 "2019-03-22T17:38:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)
#### Post date: [March 22, 2019, 5:38pm UTC](https://discourse.processing.org/t/drawing-ikeda-function/9536/1 "2019-03-22T17:38:01Z")

</div>

I want to reproduce the [Ikeda chaos map example on Wikipedia](https://en.wikipedia.org/wiki/Ikeda_map#Octave/MATLAB_code_for_point_trajectories) but I dont manage to have the same result, complexity. I have only one attractor, the center… any advice ?

```auto
size(500, 700); 
background(250);
int margin = 20;

int iterations = 4500;
float tn;
float u = 0.918;
float[] xn = new float[iterations];
float[] yn = new float[iterations];
xn[0] = random(1, 20)*100;
yn[0] =random(1, 20)*100; 

translate(width/2, height/2);
scale(1.5);
for (int i=1; i<iterations; i++) {
  tn = 0.4 - (6 /(1+pow(xn[i-1], 2) + pow(yn[i-1], 2)));
  xn[i] = 1 + u * (xn[i-1]* cos(tn) - yn[i-1]*sin(tn));
  yn[i] = u*( xn[i-1]*sin(tn)+yn[i-1]*cos(tn));

  line(xn[i-1], yn[i-1], xn[i], yn[i]);
}
println("done");

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/0a4aa618769560fa35fab92f040a6451c8f5eeca.png)

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [March 22, 2019, 11:12pm UTC](https://discourse.processing.org/t/drawing-ikeda-function/9536/2 "2019-03-22T23:12:23Z")

</div>

i think the main point of this is to have

P starting points ( for inward spirals )  
what must be visible in the canvas.

so your x0,y0 start point creation must reflect that you

- later do a translate to center
- even use a scale(?)

```auto
// IKEDA Function
// https://discourse.processing.org/t/drawing-ikeda-function/9536
// https://en.wikipedia.org/wiki/Ikeda_map#Octave/MATLAB_code_for_point_trajectories

int iterations = 1000; // line segments
int p =100; // starting points
float tn; // angle
float u = 0.918; // ikeda factor
float[] xn = new float[iterations];
float[] yn = new float[iterations];
color cp;

void setup() {
  size(500, 500);
  noLoop();
}

void ikeda() {
  xn[0] = random(-1,1)*width/2; // related to center translate need all 4 quadrants
  yn[0] = random(-1,1)*height/2; 
  cp = color(random(127,200),random(127,200),random(127,200));
  for (int i=1; i<iterations; i++) {
    tn = 0.4 - (6 /(1+pow(xn[i-1], 2) + pow(yn[i-1], 2)));
    xn[i] = 1 + u * (xn[i-1]* cos(tn) - yn[i-1]*sin(tn));
    yn[i] = u*( xn[i-1]*sin(tn)+yn[i-1]*cos(tn));
    stroke(cp);
    line(xn[i-1], yn[i-1], xn[i], yn[i]);
  }
}

void draw() {
  background(200,200,0);
  translate(width/2, height/2);
  for ( int k=0; k<p; k++) ikeda();
}

```

 ![SNAG-0018](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1847dc9b0f02a60ebb8590be97fd27db672f41b8.jpeg)

update:  
the fun starts if you add this:

```auto
void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  u += e*0.02;
  println("ikeda: "+nf(u,1,3));
  redraw();
}

```

for the mouseWheel plus plus version click

> **here: SCROLLITIS**
>
> ```auto
> // IKEDA Function
> // https://discourse.processing.org/t/drawing-ikeda-function/9536
> // https://en.wikipedia.org/wiki/Ikeda_map#Octave/MATLAB_code_for_point_trajectories
> // v0.1 forum
> // v0.2 mouseWheel for u
> // v0.3 too slow for many iterations but need for big u ( > 0.95 ) to reach 0,0
> // but if x[i] < 1 pix ?0.4? break loop / no need to calculate points * iterations times the 0,0 pixel
> // this function has 4 more parameter i want to play with:
> // v0.4.3 mousewheel++ SCROLLITIS
> // wheel ++ [u] ikeda factor u
> // wheel ++ [a][b][c][x] for function parameter
> // wheel ++ [z] zoom / scale(zoom)
> // [r] key only : RESET
> // key [s] save to data/ikeda_+all parameter+_.png
> //
> 
> String info = "mouse click canvas &\npress any key and mousewheel redraw with random startpoints\n"+
> "key [u] and wheel u ( ikada factor )\nkey [a] [b] [c] [x]\nkey [z] zoom\nonly key [r] reset, key [s] save PNG";
> 
> int p =100; // starting points / lines
> int iterations = 1000; // line segments
> float tn; // angle
> float u = 0.9; // ikeda factor start / see mouseWheel
> float tna=0.4, tnb=6, tnc=1, xna=1;
> float zoom = 1; // scale()
> float[] xn = new float[iterations];
> float[] yn = new float[iterations];
> color cp;
> String outfilename = "data/";
> String param;
> String fileext=".png";
> 
> void setup() {
> size(500, 500);
> noLoop();
> println(info);
> }
> 
> void ikeda() {
> xn[0] = random(-1, 1)*width/2; // related to center translate need all 4 quadrants
> yn[0] = random(-1, 1)*height/2; 
> cp = color(random(127, 200), random(127, 200), random(127, 200));
> for (int i=1; i<iterations; i++) {
> // tn = 0.4 - (6 /(1+pow(xn[i-1], 2) + pow(yn[i-1], 2)));
> tn = tna - (tnb /(tnc+pow(xn[i-1], 2) + pow(yn[i-1], 2)));
> // xn[i] = 1 + u * (xn[i-1]* cos(tn) - yn[i-1]*sin(tn));
> xn[i] = xna + u * (xn[i-1]* cos(tn) - yn[i-1]*sin(tn));
> yn[i] = u*( xn[i-1]*sin(tn)+yn[i-1]*cos(tn));
> stroke(cp);
> line(xn[i-1], yn[i-1], xn[i], yn[i]);
> // if ( abs(xn[i]) < 0.4 && abs(yn[i]) < 0.4 ) i = iterations; // smaller 1 pix, break for loop ( but not makes it faster )
> }
> }
> 
> void draw() {
> background(200, 200, 0);
> translate(width/2, height/2);
> scale(zoom);
> for ( int k=0; k<p; k++) ikeda();
> }
> 
> void savePNG(){
> printparam(); // also makes the actual param string about the settings (BAD: too many blanks and dots in a filename )
> String out = outfilename+param+fileext;
> save(out);
> println("save : "+out);
> }
> 
> void mouseWheel(MouseEvent event) { // mousewheel plus plus SCROLLITIS
> float e = event.getCount();
> if ( keyPressed ) {
> if ( key == 'u' ) u += e*0.01;
> if ( key == 'a' ) tna += e*0.01;
> if ( key == 'b' ) tnb += e*0.01;
> if ( key == 'c' ) tnc += e*0.01;
> if ( key == 'x' ) xna += e*0.01;
> if ( key == 'z' ) zoom += e*0.1; // this is not a pure zoom ! it redraws a new random function but same parameter!
> printparam();
> redraw();
> }
> }
> 
> void keyPressed() {
> if ( key == 'r' ) {
> u = 0.9; // ikeda factor
> tna=0.4;
> tnb=6;
> tnc=1;
> xna=1;
> zoom=1;
> println("reset:");
> printparam();
> redraw();
> }
> if ( key == 's' ) savePNG();
> }
> 
> void printparam() { //ikeda [u] 0.90 tna [a] 0.40 tnb [b] 6.00 tnc [c] 1.00 xna [x] 1.00 zoom [z] 1.00
> param = 
> "ikeda [u] "+nf(u, 1, 2)+
> " tna [a] "+nf(tna, 1, 2)+
> " tnb [b] "+nf(tnb, 1, 2)+
> " tnc [c] "+nf(tnc, 1, 2)+
> " xna [x] "+nf(xna, 1, 2)+
> " zoom [z] "+nf(zoom, 1, 2);
> println(param); 
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![mloiz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mloiz/32/11462_2.png) [@mloiz](https://discourse.processing.org/u/mloiz)
#### Post date: [March 25, 2019, 9:58am UTC](https://discourse.processing.org/t/drawing-ikeda-function/9536/3 "2019-03-25T09:58:56Z")

</div>

you are my light @kll !  
thanks
