We are about to switch to a new forum software. Until then we have removed the registration on this forum.
i'm trying to recreate this poster to understand how to visualize long texts in processing. For the moment that is what i wrote:
import processing.pdf.*;
PFont f;
String txt [];
String myData [];
ArrayList romeopos = new ArrayList();
ArrayList julietpos = new ArrayList();
void setup() {
size(600, 800, P2D);
background(255);
f = createFont("Garamond", 9);
txt=loadStrings("test.txt");
myData = new String[txt.length];
myData = txt[0].split(" ");
}
void draw() {
noLoop();
fill(50);
textFont(f);
for (int i=0; i<myData.length; i++) {
if ( myData[i].toLowerCase().contains("romeo"))
romeopos.add(i);
if ( myData[i].toLowerCase().contains("juliet"))
julietpos.add(i);
}
// text(txt[0], 10, 10, width-20, height-10);
// println (myData);
}
i don't know how to continue, could you help me? the .txt is Romeo and Juliet written in one line.
Answers
i think you'd need to save the x and y position of the start of each Romeo and each Juliet. which means printing the text letter by letter and keeping track yourself. using text() won't do it - there's no way of mapping from position within string onto screen position, especially with a proportionally spaced font.
this can put out a text and gives positions
here the word "is" is marked
I prefer koogs solution because you don't have to worry about punctuation marks because they are attached to the words in your array. I would suggest that you work word by word rather than letter by letter.
Thank you all for your quick response, for the moment i was following Chrisir response since i haven't really understood the other, i'm just at the beginning with learning processing. after i get in a for loop:
romeopos/ julietpos being arraylists, how can i get the "coordinates" out of the for() ? is it possibile to append an array to an array? or i'm focusing on the wrong way?
arraylists of what though? what are you storing in them?
imo they need to be arraylists of PVectors so you can
to save both x and y of the word
The picture below was done using text from The Scarlet Pimpernel by Baroness Emma Orczy. It displays the first 2168 words from the book and links all occurrences of the word "scarlet" (7) and links them.
The sketch to do this was based on the algorithm I posted earlier and is just 52 lines of code. I can post the code here but I understand if you want to create the code yourself, after all that is the best way of learning to program. The decision is yours. :)
could you? i think i'm missing some big steps
If you want any of the code explained then just ask.
why
.contains()
?because the split is on the space character so might have "Scarlet."
it's just that it's at the start of the string so why not startsWith()? and using contains() could be dangerous if you were looking for something that could have a prefix - 'brought' and 'thought' would return an erroneous true if looking for 'ought' for instance. (you're safe with Romeo, Juliet or Scarlet though, i guess)
tbh, i wouldn't've bothered with word-wrapping - people will be looking at the lines and not the right-hand edge. i might like to highlight the words in the relevant colour and join their middles rather than their starts though.
Thank you, i'm understanding more about arrays and pvector, btw what exactly is deltaY?
deltaY is the value you need to add to y position to get it onto the next row. he's calculated it based on the font height on line 2. (delta being the greek letter often used in maths circles to mean 'difference')
All these are very easy to do with simple modifications to my code but as this is not my project I will leave that for the OP to figure out :)