Omnimaga

Calculator Community => TI Calculators => General Calculator Help => Topic started by: meishe91 on October 19, 2011, 08:51:08 pm

Title: Random Space Filling
Post by: meishe91 on October 19, 2011, 08:51:08 pm
Does anyone have a good algorithm for filling in a box randomly with pixels until it is completely filled? I couldn't think of a good way of doing this. The only way I can think of doing it is by randomly generating an X- and Y-coordinate, checking if it's on, and if it isn't then turn it on. The only issue with this is that the bigger the box gets and the more pixels you fill the longer it can take to find a pixel that is turned off. The point of it is to seem random but it doesn't have to be necessarily. Any help would be greatly appreciated :)

Note:
I tried searching on Google but couldn't think of good keywords to find something like this. All I kept coming up with is space filling algorithms for like filling shapes into.

Edit:
The purpose for this is to materialize a title from nothing into the final results fast but supposedly random.
Title: Re: Random Space Filling
Post by: thepenguin77 on October 19, 2011, 10:04:52 pm
I know one easy way to do it, though I'm not sure how good it would look. I really have no way to describe it other than in steps:


1. First, you need a counter, I will make it 8-bit and call it _C, the counter starts at 00.
2. Clear your buffer

OuterLoop:
3. Set a pointer to the beginning of the picture buffer

InnerLoop:
4. Take a random 8-bit number, AND it with _C, then OR it with the current data in the picture buffer and store it
5. Rotate _C by one bit (left or right doesn't matter, just keep it consistent)
6. Increase to the next byte in the picture buffer
7. Goto InnerLoop until you are at the end of the buffer

8. Increase _C by 1 (the real _C, not whatever kind of rotated crap you ended up with after the inner loop)
9. Goto OuterLoop as long as _C is not equal to 256 (0)

10. Goto OuterLoop a few more times with _C = 255 just to make sure any stragglers get filled in


That aught to do it. The only thing that will break it is if your random routine isn't truely random. ;D You might also get interesting results if you randomly rotated _C rather than just rotating it by 1.

Edit:
    The second AND is an OR
Title: Re: Random Space Filling
Post by: meishe91 on October 19, 2011, 10:08:39 pm
Thanks. But any chance that could be translated into more layman's terms? I don't know much Assembly so I'm getting lost in the ANDing and rotating and such things. Thanks though :)
Title: Re: Random Space Filling
Post by: ztrumpet on October 19, 2011, 10:22:54 pm
Meishe, are you looking for Axe or TI Basic advice?
Title: Re: Random Space Filling
Post by: meishe91 on October 19, 2011, 10:25:17 pm
Either, but preferably TI-BASIC since I know it a lot better. Even just a general algorithm without any code would suffice. Eventually I have to port it to C++ but since we don't really have a general programming help section I thought this would be a good place since I can just port it later on my own.
Title: Re: Random Space Filling
Post by: Builderboy on October 19, 2011, 10:27:55 pm
You could always do the brute force way and create an array of all the points of the rectangle and then mix them up
Title: Re: Random Space Filling
Post by: meishe91 on October 19, 2011, 10:30:18 pm
I actually just thought about doing that.

Edit:
Thanks guys. I ended up just generating an array with all the points in it and then shuffled those points around and then displayed it. It seems to work pretty well :) Thanks for everyone who helped.
Title: Re: Random Space Filling
Post by: thepenguin77 on October 19, 2011, 11:18:23 pm
I was going to port my example over to C++, but I realized that if you are writing it on the computer, my technique won't work. I specially optimized mine for the calculator and it relies on the fact that you are storing your picture in monochrome. Most likely you'll have 24-bit pixels so for mine to work correctly would take a lot of work.

I'd go with the mixing up idea.
Title: Re: Random Space Filling
Post by: AngelFish on October 20, 2011, 04:19:25 pm
Here's my quick demonstration of an incredibly simple "random" space filling method. I wrote it in Axe, although it's easy enough to translate to Assembly.

Code: [Select]
$0904->A
Repeat getkey(15)     //Loop until user presses Clear
{A} or {A and 511+L6}->{A and 511+L6}
A+1->A
Dispgraph
End

0x0904 is just a location in memory on my calc that has "random" values that are ORed with the contents of the screen. It takes almost no additional memory to implement. The ANDing A with 511 is equivalent to taking A mod 512, which is an arbitrarily chosen constant to prevent A from overflowing the screen and ORing RAM with random values...

All it needs is the appropriate bitmasking to get it to work within a box onscreen.