# teaching machines

## Cratewalk

December 26, 2017 by . Filed under madeup, public.

Random walks have fascinated me the past few months. As someone who plans weeks and months ahead of time, I suppose I’m compensating a little. They let me take a walk on the wild side. Doo doo doo doo doo doo doo doo doo.

But pure wildness isn’t very interesting—at least not for long. It needs constraints. Last time, I constrained wildness to complete a circuit. This week, I wondered how I might constrain wildness to the faces of a box. I tried using turtle geometry initially, but precision issues led to some drift. This approach uses integers and absolute coordinates throughout:



-- Madeup has a builtin cross product, but it
-- produces floats. I want to stay in Integer
-- Land.
to icross u, v
{
u * v - u * v,
u * v - u * v,
u * v - u * v
}
end

to detectAxis direction
for i to 3
if direction[i] != 0
return i
end
end
end

forward = {0, 1, 0}
axis = detectAxis forward
right = {1, 0, 0}
.rgb = {1, 0.6, 0}

moveto p, p, p
repeat 60

-- Randomly pick a length to travel.
distanceToTravel = random 6, 8

-- Travel as far we can along each face of
-- the box.
while distanceToTravel > 0

-- How much distance do we have left on
-- the current face?
if forward[axis] > 0
else
end

-- If none, let's pitch down. Our new
-- forward direction is the cross
-- product of the old forward and right
-- vectors.
if distanceToEdge == 0
forward = icross forward, right
distanceToEdge = boxDiameter
axis = detectAxis forward
end

actualDistance = min distanceToEdge, distanceToTravel
p[axis] = p[axis] + actualDistance * forward[axis]
moveto p, p, p
distanceToTravel = distanceToTravel - actualDistance
end

-- Let's randomly turn sometimes.
i = random 0, 4

-- Turn right.
if i == 0
tmp = forward
forward = right
right = -tmp
axis = detectAxis forward
-- Turn left.
else if i == 1
tmp = forward
forward = -right
right = tmp
axis = detectAxis forward
end
end

dowel maxBend:10

var mupDiv = jQuery('#mup_cratewalk');
mupDiv.closest('pre').replaceWith(mupDiv);
document.getElementById('mup_form_cratewalk').submit();

-- Madeup has a builtin cross product, but it
-- produces floats. I want to stay in Integer
-- Land.
to icross u, v
{
u * v - u * v,
u * v - u * v,
u * v - u * v
}
end

to detectAxis direction
for i to 3
if direction[i] != 0
return i
end
end
end

forward = {0, 1, 0}
axis = detectAxis forward
right = {1, 0, 0}
.rgb = {1, 0.6, 0}

moveto p, p, p
repeat 60

-- Randomly pick a length to travel.
distanceToTravel = random 6, 8

-- Travel as far we can along each face of
-- the box.
while distanceToTravel > 0

-- How much distance do we have left on
-- the current face?
if forward[axis] > 0
else
end

-- If none, let's pitch down. Our new
-- forward direction is the cross
-- product of the old forward and right
-- vectors.
if distanceToEdge == 0
forward = icross forward, right
distanceToEdge = boxDiameter
axis = detectAxis forward
end

actualDistance = min distanceToEdge, distanceToTravel
p[axis] = p[axis] + actualDistance * forward[axis]
moveto p, p, p
distanceToTravel = distanceToTravel - actualDistance
end

-- Let's randomly turn sometimes.
i = random 0, 4

-- Turn right.
if i == 0
tmp = forward
forward = right
right = -tmp
axis = detectAxis forward
-- Turn left.
else if i == 1
tmp = forward
forward = -right
right = tmp
axis = detectAxis forward
end
end

dowel maxBend:10