Skip to content

Instantly share code, notes, and snippets.

@miura miura/code13_randomwalk.ijm Secret
Created May 16, 2013

Embed
What would you like to do?
//Code 13_assignment
macro "Generate Dot Animation Random Walk (4)" {
// **** initial values ****
sizenum=10; //dot size in pixel
int=255; //dot intensity in 8bit grayscale
frames=200; //frames in stack
w=100; //width of frame
h=100; //height of frame
x_position = (w/2) - (sizenum/2); //starting x position:
y_position= (h/2)-(sizenum/2); //y positon of the oval top-left corner: constant
//**** set colors *****
setForegroundColor(int, int, int);
setBackgroundColor(0, 0, 0);
//**** ask speed *****
speed=getNumber("Speed [pix/frame]?",10)
//**** prepare stack ****
stackname="dotanimation"+speed;
xspeed = speed;
yspeed = speed;
newImage(stackname, "8-bit Black", w, h, frames);
//**** drawing oval in the stack ****
for(i=0; i<frames; i++) {
setSlice(i+1);
direction = randsteps(); // user-defined function. See below.
if (direction == "right")
x_position += xspeed;
else if (direction == "left")
x_position -= xspeed;
else if (direction == "down")
y_position += yspeed;
else // direction == up
y_position -= yspeed;
if (x_position > (w-sizenum))
x_position = w-sizenum; //if dot goes out to right, then step back.
else if (x_position < 0)
x_position = sizenum; // if dot goes out to left side
if (y_position > (h-sizenum) )
y_position = h - sizenum; //if dot goes out downwards
else if(y_position < 0)
y_position = sizenum; //if dot goes out upwards
makeOval(x_position, y_position, sizenum, sizenum);
run("Fill", "slice");
}
run("Select None");
}
function randsteps(){
direction = "";
r = random();
if (r < 0.25)
direction = "right";
else if (r < 0.5)
direction = "up";
else if (r < 0.75)
direction = "left";
else
direction= "down";
return direction;
}
@miura

This comment has been minimized.

Copy link
Owner Author

miura commented May 16, 2013

XY movement macro is now modified to plot random walk dot. Changes are the following:

  • line 29: a randome direction for the next step is chosen using the user-defined function randsteps(). This function is from line 55 to 67. A test macro for this macro could be found in https://gist.github.com/cmci/5592101
  • line 30-37, depending on the chosen direction, an increment of movement in one of directin is made.
  • line 39-46, conditions for the bouncing are separated to four cases instead of two cases, as switching the direction does not ensure that the bouncing back is towards image as random choice is made for every step. Instead, these boundary conditions simpy place the dot back to the image edge.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.