以下代码的目标是获取给定文本,并根据每个单词的长度将文本分成几类。我已经实现了这个基本功能,因为当前代码成功地将所有单字母单词放入一列,将所有双字母单词放入一列等等。但是,为了使每一列中的每个单词都与前一个单词相同,我必须为每个if
语句分配一个唯一的y变量。但是,这是有问题的,因为我想跟踪每个y
位置,以便绘制连接单词的连续顺序的线,就像它们在示例文本中一样。也许有一个更好的整体方法 - 或者也许有一种方法可以在连续的单词之间画线而不创建一个全局的“y”值....
String text = "this is will only have a two word form as I want to see if this can work";
String[] words = split(text, " ");
size(200, 200);
background(#FFFFFF);
int a = 30;
int b = 30;
int c = 30;
int d = 30;
int x = 10;
for (int i = 0; i < words.length; i++) {
textSize(10);
fill(0);
if (words[i].length() == 1) {
text(words[i], x, a);
a = a + 13;
}
if (words[i].length() == 2) {
text(words[i], x+30, b);
b = b +13;
}
if (words[i].length() == 3) {
text(words[i], x+60, c);
c = c +13;
}
if (words[i].length() == 4) {
text(words[i], x+90, d);
d = d +13;
}
// I want to draw a line during each iteration in order to connect the sequential order of the words
//line(x, y, px, py)
}
答案 0 :(得分:1)
引人入胜的问题!我想我已经理解了你想做什么,但是接近这个有点吓人。在尝试编写草图来解决这个问题时,我发现很难计算出字符串的y值。 x值可以通过字符串的长度计算而没有任何问题,但y值并不那么容易,因为可以有任意数量的字符串具有该长度。
所以我要说我会使用HashMap,其中y值作为键,字符串作为值,但后来我意识到它们不一定是唯一的。看起来你是初学者,所以我不想压倒你,但我想到的解决方案是使用类。
我会创建一个类并创建一个该对象的数组来存储带有x,y,索引和值的字符串。
这是我的尝试:
final String text = "this is will only have a two word form as I want to see if this can work";
PFont font; //this is the font I'm gonna use to draw with later
Word[] words; //this is where I'll store my sentence later
//this is a basic CLASS. I can create instances of it later and store things inside it
class Word
{
String value;
int index;
float x, y;
Word(String value, int index)
{
this.value = value;
this.index = index;
//we're not gonna set x and y in here, we'll calculate that later
}
int length() { return value.length(); }
}
//this is where I'll give all my variables values
void setup()
{
String[] sentence = split(text, " ");
words = new Word[sentence.length];
//initially all the words are in order, so we'll store these i values as indices
for(int i=0;i<sentence.length;i++)
words[i] = new Word(sentence[i], i);
size(300, 300);
font = createFont("arial",18);
//sort the list by word length
sortList();
//find the x and y coordinates of each word in the list
findCoordinates();
}
void sortList()
{
//here I sort the list of words using a basic bubble sort
boolean flag = true;
while(flag)
{
flag = false;
for(int i=0;i<words.length-1;i++)
{
if(words[i].length() > words[i+1].length())
{
//swap the two
Word word = words[i];
words[i] = words[i+1];
words[i+1] = word;
flag = true;
}
}
}
}
void findCoordinates()
{
float y = 50;
for(int i=0;i<words.length;i++)
{
//I noticed that there was a relationship between the length of words
//and their x position, so I wrote this formula for it
float x = 30+40*(words[i].length()-1);
//I used the global y like you did. If the length of the current word
//is different than the previous one, then reset the y
if(i!=0 && words[i].length() != words[i-1].length())
y = 50;
//since we now know the x and y of the word, save it!
words[i].x = x;
words[i].y = y;
//and we'll increment y since the next word will be below this one
y += 18;
}
}
//this helps me later when I draw the lines, so I can do it in their original order
Word getWordAt(int index)
{
//goes through the list and finds the one with specified index
for(int i=0;i<words.length;i++)
if(words[i].index==index)
return words[i];
return null;
}
//this is where I'll draw the lists on the screen
void draw()
{
background(255);
textAlign(CENTER,CENTER);
textFont(font);
fill(0);
//let's print the words to the screen now
for(int i=0;i<words.length;i++)
{
text(words[i].value, words[i].x, words[i].y);
}
//and over here, let's draw the lines!
for(int i=0;i<words.length-1;i++)
{
line(getWordAt(i).x, getWordAt(i).y, getWordAt(i+1).x, getWordAt(i+1).y);
}
}
这是一个很大而有趣的问题,老实说有点吓人,但这就是我组织它的方式!实际上并没有更好的方法。我有另一个草图,我没有上课就做了,但它有点乱。
我希望这是有道理的,你从中学到了一些东西。快乐的节目!
答案 1 :(得分:0)
我会根据长度将所有单词添加到List
,而不是立即绘制它们(如果您还希望拥有动态数量的列,则列出列表)。
然后,您可以根据列表中的索引计算y位置,而不必为变量中的每个循环存储y位置。然后你可以画出你的线条。
列表有点像一个数组但是可变长度。因此,您可以为每个字长制作一个列表(一个用于一个字母的单词,一个用于带有两个字的单词等),这些代表您的列,或者您将创建一个包含所有这些“collumn”列表的列表,以便您可以动态添加它们。
Documentation on List can be found here或者谷歌java list
将会有数以千计的有用链接处理列表。
在这些列表中,您的单词将按顺序存储,并为其分配索引(如数组中的索引)。现在你把每个单词都设为13px高,这样列表中每个单词的y位置都是13 * index
。将整个句子添加到列表后,您将遍历列表并从一个单词到下一个单词绘制线条。我希望这就够了。