# Converting JS (DTwitter) code into Processing

**URL:** https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083
**Category:** Coding Questions
**Created:** [December 8, 2022, 11:48pm UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083 "2022-12-08T23:48:28Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![funnyrectangles](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@funnyrectangles](https://discourse.processing.org/u/funnyrectangles)
#### Post date: [December 8, 2022, 11:48pm UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/1 "2022-12-08T23:48:28Z")

</div>

Hi,

is it possible to convert this JS (Dtwitter) code into regular Processing code?

```auto
function u(t) {
T([0,0,c.width|=T=(A,i=0,z,p=S(t++)/3+.5)=>i++>12?x.strokeRect(...A):T(A=[...A],i,z=A[2+i%2]*=p,T(A,i),A[i%=2]+=z,A[2+i]=z/p-z),1080])
}

```

where

```
  u(t) is called 60 times per second.
  t: elapsed time in seconds.
  c: A 1920x1080 canvas.
  x: A 2D context for that canvas.
  S: Math.sin
  C: Math.cos
  T: Math.tan
  R: Generates rgba-strings, ex.: R(255, 255, 255, 0.5)

```

this is the original:  
[https://www.dwitter.net/d/23448](https://www.dwitter.net/d/23448)

Thanks a lot!!

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 9, 2022, 1:07am UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/2 "2022-12-09T01:07:04Z")

</div>

> [@funnyrectangles](#):
>
> Is it possible to convert this JS (Dtwitter) code into regular Processing code?

It should be possible. But that’d be a daunting task to pull out on Java/Processing syntax! 😱

To help you get started I’ve indented & commented the code, so it’s slightly more readable: 👓

```auto
function u(t) {
    T([
        0, // 1st elem
        0, // 2nd elem
        c.width |= // 3rd elem
            T = (A, i = 0, z, p = S(t++)/3 + .5) => // 4-param lambda
                i++ > 12 ?
                    x.strokeRect(...A) :
                    T(
                        A = [...A], // 1st param
                        i, // 2nd param
                        z = A[2 + i%2] *= p, // 3rd param
                        T(A, i), // 4th param (void)
                        A[i %= 2] += z, // ignored param
                        A[2 + i] = z/p - z // ignored param
                    ),
        1080 // 4th elem
    ])
}

```

---

<div class="post-metadata">

### Author: ![funnyrectangles](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@funnyrectangles](https://discourse.processing.org/u/funnyrectangles)
#### Post date: [December 9, 2022, 2:43pm UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/3 "2022-12-09T14:43:40Z")

</div>

> [@GoToLoop](#):
>
> It should be possible. But that’d be a daunting task to pull out on Java/Processing syntax! 😱

You say so because of the lambda and three dots `...` operators (rest parameters / spread operators)?

Would it be possible then to have this code running in p5.js somehow?

---

<div class="post-metadata">

### Author: ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)
#### Post date: [December 9, 2022, 3:05pm UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/4 "2022-12-09T15:05:23Z")

</div>

You can use the `...` ([varargs](https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html)) in Java (But only in the function definition, not when you call it). Also Processing 4 (Java 17) provides Lambda.

---

<div class="post-metadata">

### Author: ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)
#### Post date: [December 9, 2022, 5:05pm UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/5 "2022-12-09T17:05:55Z")

</div>

So I’ve done this now and it definitely looks like something but not like the original sketch. So I must have made a mistake somewhere. If I can find it, I’ll send a new version.

```auto
void setup() {
  size(500, 500);
}

void draw() {
  background(255);
  
  noFill();
  stroke(0);
  strokeWeight(1);
  u(millis() / 1000f);
}

void T(float t, float[] A) {
  T(t, A, 0);
}
void T(float t, float[] A, int i) {
  t += 1;
  T(t, A, i, 0, sin(t)/3 + 0.5);
}
void T(float t, float[] A, int i, float z, float p) {
  i++;
  if(i > 12) {
    rect(A[0], A[1], A[2], A[3]);
  }else{
    float[] param1 = new float[A.length];
    arrayCopy(A, param1);
    
    int param2 = i;
    
    A[2 + i%2] *= p;
    z = A[2 + i%2];
    float param3 = z;
    
    T(t, A, i);
    
    i %= 2;
    A[i] += z;
    
    A[2 + i] = z/p - z;
    
    T(t,
      param1,
      param2,
      param3,
      0
    );
  }
}
void u(float t) {
    T(t, new float[]{
        0, // 1st elem
        0, // 2nd elem
        width, // 3th elem
        height // 4th elem
    });
}

```

---

<div class="post-metadata">

### Author: ![funnyrectangles](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@funnyrectangles](https://discourse.processing.org/u/funnyrectangles)
#### Post date: [December 9, 2022, 7:29pm UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/6 "2022-12-09T19:29:57Z")

</div>

Thanks a lot!!! Now I can get into it better!

Some people there said it is based on binary trees. By that, can I expect that this recursion is supposed to produce only 0 or 2 child?

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 10, 2022, 7:34am UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/7 "2022-12-10T07:34:46Z")

</div>

Finally I’ve hit the correct recipe I guess: 🧑‍🍳

```auto
/**
 * Subdivision Waves [Java Mode] (v1.0.9)
 * mod by GoToLoop (2022/Dec/10)
 *
 * https://Dwitter.net/d/23448
 *
 * https://Discourse.Processing.org/t/
 * converting-js-dtwitter-code-into-processing/40083/7
 */

static final int ITERS = 12;
static final float STEP = .025;

float t;

void setup() {
  size(700, 400);
  background(-1);
}

void draw() {
  t = frameCount * STEP;
  T(0, 0, width, height);
}

void mousePressed() {
  looping ^= true;
}

void keyPressed() {
  mousePressed();
}

@SafeVarargs final void T(final float... A) {
  assert A.length >= 4: "Requires 4 values";
  T(A, 0);
}

void T(float[] A, int i) {
  ++t;

  if (i++ > ITERS) {
    rect(A[0], A[1], A[2], A[3]);
    return;
  }

  A = A.clone();

  final int
    ii = i % 2, 
    idx = ii + 2;

  final float
    p = sin(t)/3 + .5, 
    z = A[idx] *= p;

  T(A, i);

  A[ii] += z;
  A[idx] = z/p - z;

  T(A, i);
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 10, 2022, 10:14am UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/8 "2022-12-10T10:14:04Z")

</div>

Converted it to Python Mode: 🐍

> **[Put Python and ProcessingJS Anywhere on the Web](https://trinket.io/processing/fac1ae0453)**
>
> Python in the browser. No installation required.

https://trinket.io/embed/python/fac1ae0453?toggleCode=true&runOption=run

---

<div class="post-metadata">

### Author: ![funnyrectangles](https://avatars.discourse-cdn.com/v4/letter/f/d78d45/32.png) [@funnyrectangles](https://discourse.processing.org/u/funnyrectangles)
#### Post date: [December 11, 2022, 4:43pm UTC](https://discourse.processing.org/t/converting-js-dtwitter-code-into-processing/40083/9 "2022-12-11T16:43:34Z")

</div>

Thanks a lot, this is great!!!
