Jump to content

  • Log In with Google      Sign In   
  • Create Account


Randomly Generating Lines...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 intenseza   Members   -  Reputation: 119

Like
0Likes
Like

Posted 06 March 2013 - 01:34 PM

Hey all.

 

Why won't this code work? I'm trying to randomly draw lines...

 

 

public void GenerateRandomObjects()
        {
 
 
            for (int i = 1; i <= 10; i++)
            {
                Gl.glBegin(Gl.GL_LINES);
 
                Random r = new Random();
 
                int X1 = r.Next(800);
                int X2 = r.Next(800);
                int Y1 = r.Next(800);
                int Y2 = r.Next(800);
 
                Gl.glVertex2i(X1, Y1);
                Gl.glVertex2i(X2, Y2);
 
                Gl.glEnd();
 
            }
 
            Gl.glFlush();
 
        }
 
 
It produces 1 line :/

Edited by intenseza, 06 March 2013 - 01:40 PM.


Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 3938

Like
1Likes
Like

Posted 06 March 2013 - 01:41 PM

I'm trying to randomly draw lines...
It produces 1 line :/

It's actually drawing the same line 10 times. This is because you are resetting the RNG after every iteration. See the System.Random constructor. The default constructor sets the seed to the current system time in seconds. Because it takes less than a second to draw those lines, you are getting the same seed, and consequently the same sequence of random numbers.

You'll need to move r before the loop.

WW91J3ZlIGdvdCBhIHNlY3JldCBib251cyBwb2ludCE=


#3 intenseza   Members   -  Reputation: 119

Like
0Likes
Like

Posted 06 March 2013 - 01:44 PM

You Sir are a lifesaver. I would have spent hours trying to figure it out. Bless you!



#4 intenseza   Members   -  Reputation: 119

Like
0Likes
Like

Posted 06 March 2013 - 03:02 PM

Sorry to worry again. I'm implementing the cohen clipping algorithm and in order to do this, I need to be able to show the parts of the line that are outside the viewport in a different colour. Firstly, how is it possible to show objects that aren't within the viewport?



#5 Tim Cooper   Members   -  Reputation: 359

Like
0Likes
Like

Posted 07 March 2013 - 04:08 AM

My guess is define your own "viewport" that is smaller than the screen size. Then you just need to find the intersections of each line with your viewport rectangle and draw the segments of each line in the required colour based on the start point, intersection point(s) and end point.

 

Don't forget to draw the rectangle to show your viewport.


Tim Cooper - software developer, project manager and aspiring iPhone app developer.
 

Creative Shadows Ltd - My company website

 

iTunes Link - Doodlemate - Create animated gif's from your iPhone.

iTunes Link - Aeolus / Aeolus Lite - Land balloons in a simple, fun and slightly addictive game.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS