Reputation: 217
I am matching each word in a given phrase with any corresponding words found in a provided sample text. I am matching the words from my given phrase with the sample text by drawing a bezier curve at each instance. My issue is the following: after I place the sample Text into an array, I don't know of a good way to draw the text again without getting weird spacing between each word. I am currently drawing each word from the array, but it's not proving to be very effect. Perhaps my entire approach is incorrect...any suggestions are greatly appreciated. This is all new to me. Thanks!
String sampleText = "this is where my sample text goes";
String phrasePart1 = "this";
String phrasePart2 = "is";
String phrasePart3 = "text";
float x = 30;
float y = 70;
size(1000, 800);
background(#FFFFFF);
smooth();
String[] wordSet = split(sampleText, " ");
fill(0);
text(phrasePart1, width/2-30, 30);
text(phrasePart2, width/2+10, 30);
text(phrasePart3, width/2+30, 30);
for (int i = 0; i < wordSet.length; i++) {
textSize(6);
text(wordSet[i], x, y);
if (wordSet[i].equals(phrasePart1)) {
noFill();
stroke(153);
smooth();
bezier(width/2-35, 27, 30, 30, 40, random(30, 200), x+5, y-9);
}
if (wordSet[i].equals(phrasePart2)) {
noFill();
stroke(153);
smooth();
bezier(width/2+13, 33, 670, 30, 680, random(30, 200), x+5, y-9);
}
if (wordSet[i].equals(phrasePart3)) {
noFill();
stroke(153);
smooth();
bezier(width/2+50, 27, 670, 30, 680, random(30, 200), x+5, y-9);
}
x = x + wordSet[i].length()*3+4;
if (x > width-50) {
x = 30;
y = y + 12;
}
}
Upvotes: 1
Views: 644
Reputation: 2800
Try replacing
x = x + wordSet[i].length()*3+4;
at the bottom of your for loop with textWidth
x = x + textWidth(wordSet[i] + " " );
Upvotes: 2