# KITT without arrays

**URL:** <https://forum.electromage.com/t/kitt-without-arrays/1219>\
**Category:** Patterns and Code\
**Created:** [May 6, 2021, 3:48am UTC](https://forum.electromage.com/t/kitt-without-arrays/1219 "2021-05-06T03:48:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://forum.electromage.com/user_avatar/forum.electromage.com/jeff/32/3473_2.png) [@jeff](https://forum.electromage.com/u/jeff)\
**Post date:** [May 6, 2021, 3:48am UTC](https://forum.electromage.com/t/kitt-without-arrays/1219/1 "2021-05-06T03:48:00Z")

</div>

@zranger1 got a proof of concept for this going in [this thread](https://forum.electromage.com/t/how-would-you-make-a-2d-kitt/1153/11).

He added:

> [@How would you make a 2D KITT?](https://forum.electromage.com/t/how-would-you-make-a-2d-kitt/1153/11):
>
> The only thing it doesn’t do (yet) is the brightness blend when switching directions, but I think that could be done by running two simultaneous waves going in opposite directions…

I took a shot at this today and got something workable. Sorry I’m lazy and didn’t add nice UI controls 😉

![ezgif.com-gif-maker](https://forum.electromage.com/uploads/default/original/1X/4d783e402edf7e76cccd7b5882affd37eafee00d.gif)

```javascript
var tailPct = .6 // length of the tail in 0..1
var t1, shift = 1 - tailPct

export function beforeRender() { t1 = time(.05) }

function pulse(x) {
  var halfsaw = triangle(x - t1) * square(x - t1, .5)
  return max(0, (halfsaw - shift) / tailPct)
}

export function render(index) {
  pct = index / pixelCount / 2
  v = pulse(pct) + pulse(-pct)
  hsv(0, 1, v * v * v)
}

```

Interestingly, original KITT is about 1.6x faster.

---

<div class="post-metadata">

**Author:** ![Scruffynerf](https://forum.electromage.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@Scruffynerf](https://forum.electromage.com/u/Scruffynerf)\
**Post date:** [May 6, 2021, 4:16am UTC](https://forum.electromage.com/t/kitt-without-arrays/1219/2 "2021-05-06T04:16:50Z")

</div>

I feel like golfing this could end with a super minimal KITT.

And now I’m curious to see a mapped arrayless KITT too. But the array is otherwise useful (it’s a cheap framebuffer approach, which can be handy for all sorts of stuff) so lots of ways to do the same thing.

---

<div class="post-metadata">

**Author:** ![Scruffynerf](https://forum.electromage.com/letter_avatar_proxy/v4/letter/s/34f0e0/32.png) [@Scruffynerf](https://forum.electromage.com/u/Scruffynerf)\
**Post date:** [May 6, 2021, 12:01pm UTC](https://forum.electromage.com/t/kitt-without-arrays/1219/3 "2021-05-06T12:01:28Z")

</div>

Ok, golfed mapped arrayless version (with sliders and exports for exploration):

```
export var tailPct = .6 // length of the tail in 0..1
export var speed = .05
export var pct1
export var pct2

export function sliderTail (v) {
  tailPct = .6 + v/3;
}

export function sliderSpeed (v) {
  speed = v/10;
}

export function render2D(index,x,y) {
  pct1 = y/2 - time(speed)
  pct2 = -y/2 - time(speed)
  v = max (0,( tailPct - 1 + triangle(pct1) * square(pct1,.5) ) / tailPct )
    + max (0,( tailPct - 1 + triangle(pct2) * square(pct2,.5) ) / tailPct )
  hsv(0, 1, v * v * v)
}

```

that’s running at 69+ FPS on my v3 with a 16x16 matrix

to compare:  
Original KITT runs at 79 FPS  
[Mapped Array KITT](https://forum.electromage.com/t/how-would-you-make-a-2d-kitt/1153/13) runs at 80 FPS

So the array approach is _slightly_ faster… but… even changing it from `v*v*v` to `v*v` adds a frame or 2 back. But still, that’s only a 15% difference in speed. You’ve entirely eliminated any arrays, pure math/waves, so it’s a bit slower but uses MUCH less memory.

---

<div class="post-metadata">

**Author:** ![wizard](https://forum.electromage.com/user_avatar/forum.electromage.com/wizard/32/4_2.png) [@wizard](https://forum.electromage.com/u/wizard)\
**Post date:** [May 6, 2021, 7:29pm UTC](https://forum.electromage.com/t/kitt-without-arrays/1219/4 "2021-05-06T19:29:32Z")

</div>

Very nice! No arrays, just math!
