### Xor's Raymarcher

This is my first time touching TurtleToy and even JavaScript, but it's been fun to play around with it. Let me know if anything needs improvement. JS seems to be quite forgiving.

```/*
"Xor's Raymarcher"

This is my first time touching TurtleToy and even JavaScript, but it's been fun to play around with it.
Let me know if anything needs improvement. JS seems to be quite forgiving.
*/

let col = -1; //min=-1, max=1, step=2
let res = 100;//min=50, max=200, step=50
let pos = 0;  //min=0, max=20, step=1

Canvas.setpenopacity(col);
const vec = new Turtle();

//Based on reinder's work:
const sub3=(a,b)=>[a-b,a-b,a-b];
const mul3=(a,b)=>[a*b,a*b,a*b];
const dot3=(a,b)=>a*b+a*b+a*b;
const len3=(a)=>Math.sqrt(dot3(a,a));
const nor3=(a)=>mul3(a,1/len3(a));

//SDF function.
function dist(p)
{
//Rotate the position coordinates.
p = [p*.8+p*.6,
p,
p*.8-p*.6];
//Repeat cube coordinates.
c = [p-Math.floor(p/8)*8-4,
p-Math.floor(p/8)*8-4,
p-Math.floor(p/8)*8-4];
//Calculate cube length vector.
c = [Math.max(Math.abs(c)-1,0),
Math.max(Math.abs(c)-1,0),
Math.max(Math.abs(c)-1,0)];

return Math.min(len3(c)-1.,-p+6);
}
//Raymarching function.
function raymarch(p,d)
{
//Only 20 steps!
for(i = 0;i<20;i++)
{
s = dist(p);
if (s<.01) return p;
p = [p+d*s,p+d*s,p+d*s];
}
return p;
}
function walk(i)
{
//Calculate pixel coordinates.
f = Math.floor(i/res);
d = Math.ceil(i/2)*4-i*2-1;
x = (i-f*res)/(res-1)*200-100;
y = f/(res-1)*200-100;

//Calculate camera.
p = [0,-1,pos];
v = nor3([x,y,100]);
r = raymarch(p,v);

//Calculate vector direction.
nx = dist([r+v,r+v,r-v])-
dist([r-v,r+v,r+v]);
ny = dist([r+v,r+v,r-v])-
dist([r+v,r-v,r+v]);
n = 20/len3(sub3(r,p));

//Output vector.
vec.jmp(x,y);
vec.goto(x+ny*n,y-nx*n);
return i < res*res;
}```