Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Randomizing Squares
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
supaphreek




PostPosted: Mon May 10, 2010 7:31 pm   Post subject: Randomizing Squares

What is it you are trying to achieve?
I am trying to create random squares across the screen. (all the created squares have to have the same area... be the same size)


What is the problem you are having?
I can get it to create random rectangles, but not squares because if i use randint and drawbox, it wont always have the same pixel length for all sides.


Describe what you have tried to solve this problem
Not much, i can create many other boxes by inputting the values manually and loading them, but it isnt randomized.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
I have no relevant code at the moment. Sorry about that

Please specify what version of Turing you are using
I beleive its the latest, 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Mon May 10, 2010 7:50 pm   Post subject: Re: Randomizing Squares

Variables dude. Use them wisely. You say you want something of the same for multiple uses think variables. I'm guessing you've got something like this:
code:

for i : some range
    DrawBox(Random, Random, Random, Random, colour)
end for


So you need to remember the dimension of all the squares:

size = 10

Then you say you need to draw the squares randomly across the screen. To normally draw a rectangle/square you use DrawBox which has the following parameters:

DrawBox(x1, y1, x2, y2, colour)

Each point is relative to the bottom left. So say we wanted to draw a single point (essentially a really small square) using drawbox we'd use:

DrawBox (x, y, x, y, colour)

Notice both points have to be the same. To randomly draw this, we'd have to set x and y to random numbers.

x = Random
y = Random
DrawBox (x, y, x, y, colour)


Notice how we don't do it inside the call to DrawBox? But wait, don't forget that size thing to scale it. So we add the same distance to both x,y of the second coordinates.

x = Random
y = Random
DrawBox (x, y, x+size, y+size, colour)
supaphreek




PostPosted: Mon May 10, 2010 8:03 pm   Post subject: RE:Randomizing Squares

Im sorry, but im a bit confused :S if i added 10 to the value of the x and y, how would that make it a perfect square? :S
DemonWasp




PostPosted: Mon May 10, 2010 8:11 pm   Post subject: RE:Randomizing Squares

If all the boxes have to be the same area / size, then you know that size, or can choose it. Let's call the size of the box SIZE (a variable or const).

Now we just randomize the bottom left corner's coordinates to be (x, y) in the range (0, 0) to (maxx-SIZE, maxy-SIZE) (because we want the box to stay on the screen).

Drawing a box with that lower-left corner and dimensions SIZE by SIZE is left as an exercise to you. This should be relatively simple (hint: addition).
supaphreek




PostPosted: Tue May 11, 2010 6:19 am   Post subject: RE:Randomizing Squares

Yeah! i get the difference now! Thank you both! got it working Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: