Another in library series…
Based on discussion in Pentagonal icositetrahedron
Still under construction, posting early version for eyeballs and feedback.
only 2d so far, does R/A from x/y and also stores re-origin-ed x,y (so 0,0 is center)
// Coordinate Library v0.5 alpha by ScruffyNerf
// caches the various coordinate systems,
// so you can convert only once, and then have it precalc-ed on subsequent calls
// first up, 2d Cartesian (x,y) to Polar (r,a)
var coordinates = array(pixelCount)
var calc = true
var coordinatetypes = 4
var _r = 0
var _a = 1
var _x = 2
var _y = 3
// preset all arrays, makes testing easier... (if you don't cache and create arrays every time, memory runs out)
for (i = 0; i < pixelCount; i++) {
coordinates[i] = array(coordinatetypes)
}
function coordinatecache(index,x,y){
x = x - .5
y = y - .5
coordinates[index][_r] = sqrt(x*x + y*y)
coordinates[index][_a] = atan2(y,x)
coordinates[index][_x] = x
coordinates[index][_y] = y
if (index == pixelCount - 1) { // last pixel, we're all cached
calc = false; // if you comment out this line, you can compare cached speed to doing math every time.
}
return
}
export function beforeRender(delta) {
t1 = wave(time(.15))
}
export function render2D(index,x,y) {
if (calc) { coordinatecache(index,x,y)}
r = coordinates[index][_r]
a = coordinates[index][_a]
if (r <= .4 && r >= .3 && (a >= (t1 * PI2) - PI) ){
h = t1 + index/pixelCount
hsv(h, 1, 1)
}
else {
hsv(0,0,0)
}
}
based on my quick test (commenting out the calc = false line)
I go from ~86FPS to 56FPS on a v3 with 256 pixels (16x16)
That’s a 50% difference in speed (30FPS more over 56FPS), just by no longer doing the math functions (Yes, there are some extra things in there, like setting array values, so it’s not all math, but it’s mostly the math, if I comment out all of the rest but the math, it goes to only 64FPS… that’s still more than 20FPS difference)
additional math/speed tweaks:
replace index/pixelCount
with y
and gain another FPS or 2.
replace sqrt calc with with hypot(x,y)
… still only 57 FPS, so slightly faster.
I also removed assigning s and v, and even rearranged logic, to just always set hsv(0,0,0) first before the if(), vs in the else at the end, the else is just slightly faster I think.
Need to add 3d, and maybe other ‘one time’ calculations that are useful…
reference Spherical coordinate system - Wikipedia
I’ll definitely add cylindrical coords (r,a,z) because we have a lot of people with those (wrapped coils, and so on). This will also have a conversion the other way, so potentially you could map whatever was easiest and then convert.