We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › renegade random positions
Page Index Toggle Pages: 1
renegade random positions (Read 776 times)
renegade random positions
Feb 21st, 2009, 5:34am
 
Hello,

I am attempting to create a program that inputs text one character at a time, then transforms each character to a new random size, color, and position.

I am having difficulties constraining the characters to the edge of the screen.

to determine the end positions, taking into consideration the character's max size and center alignment, I am using:

endX[0] = random(maxSize/2, width-maxSize/2);
endY[0] = random(maxSize, height);

This works nothing like I thought it might. I try to change the lower parameter and the higher parameter seems to move as well, and vice versa. Also, a few characters like to escape the screen completely, never to return.

What can I do to constrain the characters to the exact edges of the screen?

Also if anyone has any tips to speed this program up, they would be greatly appreciated as well.


Full code and demonstration at:
http://www.sarawentworth.com/processingHelp/

Thanks!
Re: renegade random positions
Reply #1 - Feb 21st, 2009, 1:11pm
 
i haven't been able to test that because i get out of memory errors (and i'm on a 4G pc...) and nothing other than a white screen and an exception

but looking at the code i have some friendly criticism

this is crying out to be a class rather than 24 5000 element arrays:

Code:

int arrayLength = 5000;
float[] charTimer = new float[arrayLength];
int[] Size = new int[arrayLength];
int[] beginSize = new int[arrayLength];
int[] endSize = new int[arrayLength];
int[] R = new int[arrayLength];
int[] G = new int[arrayLength];
int[] B = new int[arrayLength];
int[] A = new int[arrayLength];
int[] beginR = new int[arrayLength];
int[] beginG = new int[arrayLength];
int[] beginB = new int[arrayLength];
int[] endR = new int[arrayLength];
int[] endG = new int[arrayLength];
int[] endB = new int[arrayLength];
char[] charArray = new char[arrayLength];
float[] beginX = new float[arrayLength];
float[] beginY = new float[arrayLength];
float[] endX = new float[arrayLength];
float[] endY = new float[arrayLength];
float[] distX = new float[arrayLength];
float[] distY = new float[arrayLength];
float[] x = new float[arrayLength];
float[] y = new float[arrayLength];
float[] pct = new float[arrayLength];


this next bit is terrible. when you've got 30+ copies of the same code with only 1 character difference between each one you know you're doing it wrong. just set the array equal to the value of key

from the documentation:
"The keyPressed() function is called once every time a key is pressed. The key that was pressed is stored in the key variable."

Code:

void keyPressed() {
switch(key) {
case 'A':
updateArray();
updateInit();
charTimer[0] = millis();
charArray[0] = 'A';
beginX[0] = xPos;
beginY[0] = yPos;
xPos += xInc;
break;
case 'B':
updateArray();
updateInit();
charTimer[0] = millis();
charArray[0] = 'B';
beginX[0] = xPos;
beginY[0] = yPos;
xPos += xInc;
break;
...


rippling 20 arrays of 4999 elements each might be why it's a bit slow... use a circular buffer instead

Code:

void updateArray() {
for(int j=(arrayLength-1); j>0; j--){
Size[j] = Size[j-1];
beginSize[j] = beginSize[j-1];
endSize[j] = endSize[j-1];
...
}
Re: renegade random positions
Reply #2 - Feb 22nd, 2009, 12:05am
 
Thanks koogs.

If it isn't obvious already I'm a bit of a newbie to programming in general. I will begin working on the class. However, I'm not entirely familiar with circular buffers.

This explains it fairly well i think:
http://www.vias.org/javacourse/chap16_04.html

However, I was wondering if there might be a good example somewhere on the processing site?
Re: renegade random positions
Reply #3 - Feb 22nd, 2009, 10:30am
 
saratonin wrote on Feb 22nd, 2009, 12:05am:
I'm not entirely familiar with circular buffers. [...]
However, I was wondering if there might be a good example somewhere on the processing site

Perhaps in Can someone explain my own code to me
Re: renegade random positions
Reply #4 - Feb 24th, 2009, 12:42am
 
> to determine the end positions, taking into consideration
> the character's max size and center alignment, I am
> using:    

> endX = random(maxSize / 2, width - maxSize / 2);
> endY = random(maxSize, height);

are you sure these are centre aligned? (ok, they are centre bottom aligned)

wouldn't the endY be somewhere between (height - maxSize) and height? similarly endX would be between (width - maxsize) and width.

unfortunately if you do this all the characters end up on top of each other in the bottom right hand corner. perhaps you actually want them along the bottom OR along the right but not both. in which case you have to treat the two edges differently:
Code:

if (random(10) < 5) {
// random position on right hand edge
endX = width;
endY = random(0, height);
} else {
// random position on bottom edge
endX = random(0, width);
endY = height;
}


(use textAlign(RIGHT) and the bottom right of the letter is the origin. which makes it easier to define the endX and endY as that's also bottom and right)

i think the printing of the current state of the 5000 character array every frame was dragging it down too.

have also found out what was causing it to run out of memory - the maxSize for the font. reducing it to 100 solves the problem.

and you can replace *the whole of* your keyPressed with this:

Code:

void keyPressed() {
updateArray();
updateInit();
charTimer[0] = millis();
charArray[0] = key;
beginX[0] = xPos;
beginY[0] = yPos;
xPos += xInc;
}
Re: renegade random positions
Reply #5 - Feb 24th, 2009, 12:42am
 
(actually, that doesn't take descenders into account, but is otherwise ok)
Re: renegade random positions
Reply #6 - Feb 24th, 2009, 3:12am
 
Thanks guys, you're awesome.

Yes, I know lowering the maxSize and shortening the array will speed it up. I am, however, very interested in this circular buffer concept... because it seems like a good thing to know, and I kinda like the way it looks with a wider variation of sizes.

I have the letters center-aligned because in instances in which they are not done growing when they hit their final destination, they look strange expanding from the bottom left. This wouldn't be an issue if I could somehow set them to grow according to the time it takes to get to the final position... so that by the time they stop they won't grow any more. I may still try to do that.

I don't want them to all line up on the sides... I just want them to all be 100% on the screen... so that if I'm projecting it on a wall there are no obvious edges where the letters cut off. Even when it is left aligned, random(maxSize, height), and random(0, width-maxSize) do not work as I thought they might. I can probably play with the numbers til something works... but I'm trying to find the logic behind what's going on.

I'm still trying to catch the renegades... an issue only apparent in the first characters of each line (which is probably a clue). They just exit to the left and never return. I'm going to clean up the code and see if it's still an issue.

I uploaded the updates so far (no class yet):
http://www.sarawentworth.com/ProcessingHelp2/
Re: renegade random positions
Reply #7 - Feb 24th, 2009, 10:41pm
 
> I have the letters center-aligned because in instances in which they are not done growing when they hit their final destination, they look strange expanding from the bottom left.

ah, ok, i didn't see this because i turned the max size down to 50 to get over my memory problem (which i can probably fix with jvm parameters but...) and they'd all stopped growing before they stopped moving.

in that case then i'd also look at using textAlign(CENTER, CENTER) so that the middle of the letter is in the centre and not middle of the baseline (i found it useful to draw a small ellipse at the x, y position of the letter).

your rightmost border then becomes width - (maxSize / 2) and the lower border height - (maxSize / 2), ie half a character away from the edge.

i also rewrote the thing to use circular buffers (not sure i got that right but then i never got anywhere near filling them so it didn't matter) and used a class for the letters, which made everything simpler. what you have is basically a particle system and it's a good idea to get that down and reusable because it crops up again and again. i normally use an ArrayList (actually i normally extend ArrayList...) but i'm not sure that's ideal.
Re: renegade random positions
Reply #8 - Feb 25th, 2009, 12:26am
 
> I don't want them to all line up on the sides... I just want them to all be 100% on the screen...

ok, then you need them center / center aligned and they shouldn't be allow to end up closer than half a character away from any edge. so hwidth to (width - hwidth) and hheight to (height - hheight) (where hheight and hwidth are half the character size. something tells me they will be different and can be calculated. or just use half maxSize as a rough guess)

can't see your example, just a white square. i guess maxSize is too large and browser java plugin dies due to lack of memory.
Page Index Toggle Pages: 1