# 2d rainbow patterns?

**URL:** <https://forum.electromage.com/t/2d-rainbow-patterns/922>\
**Category:** Patterns and Code\
**Created:** [January 22, 2021, 6:49am UTC](https://forum.electromage.com/t/2d-rainbow-patterns/922 "2021-01-22T06:49:45Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![zranger1](https://forum.electromage.com/user_avatar/forum.electromage.com/zranger1/32/301_2.png) [@zranger1](https://forum.electromage.com/u/zranger1)\
**Post date:** [January 30, 2021, 9:41pm UTC](https://forum.electromage.com/t/2d-rainbow-patterns/922/20 "2021-01-30T21:41:07Z")

</div>

It’s there… Check capitalization and spelling, then check to be sure that all the pairs of braces in the functions match up. If you can’t get yours to work, compare it to:

```auto
// Radial rainbow for 2D displays
// initial circle/rainbow code by @zranger1
// modified for sound by @scruffynerf

export var speed = 0.5
export var direction = 1
export var size = 100
export var spin = 1
export var spincycle = 0
export var spincyclearray = 0

export var frequencyData
export var angle

// UI sliders

// higher is faster
export function sliderSpeed(v) {
  speed = 1-v;  
}

// left = inward, right = outward
export function sliderDirection(v) {
  direction = (v < 0.5) ? 1 : -1;  
}

// sizing
export function sliderSize(v) {
  size = 200*v
}

// spin
export function sliderSpin(v) {
  spin = v
}

// pythagorean distance from center of display. Pixelblaze
// provides normalized x,y coords, so center is always going
// to be (0.5,0.5) regardless of real world display dimensions
function getRadius(x, y) {
  x -= 0.5; y -= 0.5;
  return sqrt(x*x + y*y)*2;
}

// pythagorean angle from center of display. Pixelblaze
// provides normalized x,y coords, so center is always going
// to be (0.5,0.5) regardless of real world display dimensions
function getAngle(x, y) {
  x -= 0.5; y -= 0.5;    
  angle = floor(((atan2(y,x)+PI)/4)*32)%32
  angle = (angle+spincyclearray)%32;
  return angle;
}

// generate a timer - a sawtooth wave that we can
// use to animate color -- the direction flag makes
// it positive or negative, depending on the UI slider setting
// spin slider controls the spin, currently only one direction
export function beforeRender(delta) {
  t1 = direction * time(0.08 * speed);
  spincycle = (spincycle + spin)%32
  spincyclearray = floor(spincycle);
}

// use radius+angle and timer to color every pixel, if it's loud enough
export function render2D(index, x, y) {
  radius = getRadius(x, y);
  bri = (radius <= frequencyData[getAngle(x,y)]*size) ? 1 : 0.3
  hsv(t1+radius, 1, bri);
}

```

---

_[View the full topic](https://forum.electromage.com/t/2d-rainbow-patterns/922)._
