I am trying to write a text randomizer. The idea is to put individual letters of ‘ATOMS’ into the grid. There is a specific grid that I have created and I want Individual letters of the word ‘ATOMS’ to sit in random places on the grid on mouse cl. (Image attached) without being repeated.
I have tried putting them in an array but somehow it doesn’t seem to work.
hello,
i don’t think that your problem is the array.
it looks like you are writing the ‘A’ into every field of the grid
i = 0, j = 0 -> text(letters[0], i * stepX, j * stepY); will become text(“A”, 0,0);
0 1 -> A, 120,120
0 2 -> A 120, 140
and so on…
then you put all the other letters always into the same place
if you want to place the letters randomly, shouldn’t you use a random value ?
like:
for (var i = 0; i < columns; i++) {
text (letter[I], random(5)*stepX, random(5)*stepY);
}
(maybe using int() to get correct integer values - im not good in p5js)
the next step would be to prevent letters from being put into a place that already has one
another thing probably to have the word still readable…
or maybe you want them horizontally OR vertically arranged.
so you could randomly put A only in the first column or row, T in the next and so on…
Thank you!! I am new to P5js as well. I have tried your code and tweaked it a bit. it seems to work, but how to get them sit in the specified grid? and randomly generate the positions.
Right now I have tried to make them displace horizontally.
1st I think you do not need to call textSize() in draw(), im used to processing and there using fill() etc. is usually thought to cost relatively much cpu.
2nd stepX is the width divided by the number of fields. but you want to put the “T” in the middle of the axis. so the first step should be 1/2 step, adding stepX fully for the rest of the columns.
so saying:
stepX = width/columns;
...
var thisStepX = stepX/2 + i*stepX;
var thisStepY = stepY/2 + i*stepY;
text(letters[i], thisStepX, thisStepY);
should put the letters diagonally.
now you can replace I or j by a random number -> every letter can be everywhere
or only make j random -> each column gets a letter at a random place from the rows
I think you will not need two nested loops then: looping through the columns you place 1 letter in each row randomly with random(rows)
also, by putting noFill() inside the end of the inner (j) loop, you will not see more than the first letter you should put a fill(0) in front of the text
Thank you so much. This seems to solve the problem of positioning. I have simplified the grid a bit to get a better understanding.
Just need to figure out the randomization bit.
That is great!
I have tried this. it is somewhat working but still not there. , but i want just the three word to appear at a time. Here is the link to the code.
Link: [https://editor.p5js.org/Rohit/sketches/SBo4u0VYF]
for (var i = 0; i < columns; i++) {
//for loop to iterate rows
var thisStepX = stepX / 2 + i * stepX;
var thisStepY = stepY / 2 + i * stepY;
//defining the random attributes to the letters
var rand = round(random(letters.length -1));
var TheCollective = letters[rand];
textAlign(CENTER, CENTER);
//text(letters[i], thisStepX, thisStepY);
//first row
text(TheCollective, thisStepX, thisStepY);
text(TheCollective, thisStepX + stepX, thisStepY);
text(TheCollective, thisStepX + 2 * stepX, thisStepY);
//Second row
text(TheCollective, thisStepX, thisStepY + stepY);
text(TheCollective, thisStepX + stepX, thisStepY + stepY);
text(TheCollective, thisStepX + 2 * stepX, thisStepY + stepY);
//Third row
text(TheCollective, thisStepX, thisStepY + 2 * stepY);
text(TheCollective, thisStepX + stepX, thisStepY + 2 * stepY);
text(TheCollective, thisStepX + 2 * stepX, thisStepY + 2 * stepY);
for (var j = 0; j < rows; j++) {
noFill();
//Grid
//noStroke();
noFill();
rect(i * stepX, j * stepY, stepX, stepY);
}
}
I think @glv 's idea of shuffling is also interesting. but maybe it will give you less control of recognizing the word. I also notice that you still use two nested loops and I do not think that you need it, since your idea of iterating over each column will give a more readable result than pure randomness…
you should walk through your code step by step to see what happens in each loop.
i think: step 1: walk through each row from 0 to n (5 in your example.)
that would be the loop you need -> for (int i = 0; i < n; I++);
0 = first row, 1 = second, …, n = last row
step 2: create a random number between 0 to n (5 in your example) find a position for the nth letter in the nth row ->
if you are at i = 0 in your single loop, it will put the first letter into the first row
text (letter[i], stepX/2 + i * stepX, stepY/2 + i * stepY);
-> this would put each character into one field further than the next, creating a diagonal word
var r = random(n);
text (letter[i], stepX/2 + r * stepX, stepY/2 + i * stepY);
-> this will put each letter (because i is growing by 1 each cycle)
into the next row (because of i * stepY), but into a random position in that column (because of r * stepX)
if you switch r and i it would switch rows and columns text (letter[i], stepX/2 + i * stepX, stepY/2 + r * stepY);
if you use r both times it would be completely random (but two characters could land in the same spot)