Created
August 20, 2014 01:03
-
-
Save willpatera/a648a18fccdbb4a6927c to your computer and use it in GitHub Desktop.
Python Random Walk 3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import numpy as np | |
import scipy.spatial.distance as spd | |
import OpenGL.GL as gl | |
from glumpy import figure, Trackball | |
if __name__ == '__main__': | |
fig = figure(size=(800,800)) | |
trackball = Trackball(65, 135, 1., 20.0) | |
@fig.event | |
def on_mouse_drag(x, y, dx, dy, button): | |
trackball.drag_to(x,y,dx,dy) | |
fig.redraw() | |
@fig.event | |
def on_draw(): | |
fig.clear() | |
trackball.push() | |
# gl.glPoint(0.0,0.0,0.0) | |
gl.glEnable(gl.GL_POINT_SMOOTH) | |
gl.glPointSize(10) | |
gl.glBegin(gl.GL_POINTS) | |
gl.glColor4f(0.0, 0.0, 0.0, 0.5) | |
gl.glVertex((0.0,0.0,0.0)) | |
gl.glColor4f(1.0, 0.0, 0.0, 0.5) | |
gl.glVertex(walker[-1]) | |
gl.glEnd() | |
gl.glColor4f(0.0, 0.0, 0.0, 1.0) | |
gl.glBegin(gl.GL_LINE_STRIP) #GL_LINES | |
for pt in walker: | |
gl.glVertex( pt ) | |
gl.glEnd() | |
trackball.pop() | |
step_pos = 1.0 | |
step_neg = 1.0 | |
bnds = 5.0 | |
@fig.timer(1.0) | |
def timer(dt): | |
global start_point | |
global g_counter | |
# new_point = [p + np.random.randint(-1.0, +2.0) for p in start_point] | |
new_point = [p + np.random.uniform(-step_neg, +step_pos) for p in start_point] | |
origin = [0.0, 0.0, 0.0] | |
dist = spd.euclidean(origin, new_point) | |
# check the boundary! | |
# to-do: check for self intersection | |
if dist < bnds: | |
walker.append(new_point) | |
start_point = new_point | |
# x,y,w,h = fig.x, fig.y, fig.width, fig.height | |
# data = gl.glReadPixels (x,y,w, h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE) | |
# from PIL import Image | |
# image = Image.fromstring('RGBA', (w,h), data) | |
# image = image.transpose(Image.FLIP_TOP_BOTTOM) | |
# image.save ("data/%04d.png" %(g_counter)) | |
g_counter += 1 | |
fig.redraw() | |
g_counter = 0 | |
start_point = [0.0,0.0,0.0] | |
walker = [] | |
walker.append(start_point) | |
fig.window.set_title("Random Walk: -%s:+%s within %s distance" %(step_neg, step_pos, bnds)) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment