OpenGL-Random Walk Program

I'm writing a program for my C++ class. Here's the description:

Random Walk
Random walk simulates how a molecule moves in a liquid or how a drunk person stumbles.
We want to find out how long it takes a molecule (drunk) to return to the point of departure.

Draw a point in the middle of the window. This is the starting point. The drunk person now moves randomly into one of the four direction by some fixed distance, say, 2 units. Then plot a point for the new position. Run the program until the drunk person returns to the original position. (You may have to abort the program if it takes too long.) Then print on the screen the number of moves needed.

I've got an origin point and one random point drawn, but I'm having trouble looping/iterating it. I'm very new to C++ and any help would be appreciated. Can someone help me out? Here is what I'm working with:


#include "stdafx.h"
#include <cmath>
#include <cstdlib> //Due to a bug in Microsoft Visual C++ this has to come before including glut.h
#include <glut.h>
#include <ctime>

using namespace std;

void myinit()
{
glClearColor(1.0, 1.0, 1.0, 1.0); // white background
glColor3f(1.0, 0.0, 0.0); // draw in red

// set up viewing
// 500 x 500 window with origin in the center

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-250.0,250.0,-250.0,250.0);
//glMatrixMode(GL_MODELVIEW);

srand(static_cast<unsigned int>(time(0)));
}


void markPosition(void)
{

glClear(GL_COLOR_BUFFER_BIT); //clear the window



GLfloat currentPos[2] = {0,0};
GLfloat nextPos[2] = {0,0};


//making random variables
GLfloat x=rand()%3;
GLfloat y=rand()%3;
//establish the coordinates of nextPos
//based on the random variables
//keeping a step size of ten units:
if (x==0)
{
nextPos[0] = currentPos[0]; //x=0, x coordinate doesn't move
}
else
{
if (x==1)
{
nextPos[0] = currentPos[0] + 10; //x=1 step right
}
else
{
nextPos[0] = currentPos[0] - 10; //x=2 step left
}
}

if (y==0) //similarly for y coordinate
{
nextPos[1] = currentPos[1];
}
else
{
if (y==1)
{
nextPos[1] = currentPos[0] + 10;
}
else
{
nextPos[1] = currentPos[0] - 10;
}
}

glPointSize(4);
glBegin(GL_POINTS);
glVertex2f(currentPos[0], currentPos[1]);
glVertex2f(nextPos[0], nextPos[1]);


/*now it needs a move function?
GLfloat temp=currentPos;
currentPos=nextPos;
nextPos=temp;
move(currentPos, nextPos);

Then finish off the for loop
convert to string, print string?
*/



glEnd();
glFlush();
}

int main(int argc, char** argv)
{

// Standard GLUT initialization

glutInit(&argc,argv); //initialiazes OpenGL
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // display mode
glutInitWindowSize(500,500); // 500 x 500 pixel window
glutInitWindowPosition(150,150); // places window near left upper corner
glutCreateWindow("Random Motion"); // window title

// register callbacks
glutDisplayFunc(markPosition);
//glutReshapeFunc(changeSize);


myinit(); // set attributes

//Enter the GLUT processing loop

glutMainLoop();

return 0;
}

Thanks!

Dr. Minimair is going to fail you.
Last edited on
lol nah jk. its Dan
Topic archived. No new replies allowed.