We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am trying to write a code that will create a series of X along the sketch area. These X should form a grid where I can control the the vertical and horizontal gutter between them. Also, I would like to understand how can I compile the whole X code (stroke, strokeWeight, line) into one word such as xDraw.
Thank you in advance.
Here's the code so far:
int xSize;
int xWeight = 5;
int xNum = 10;
int xSpace = 50;
void setup() {
size(500, 500);
for (int i=0; i< xNum; i++) {
strokeCap(SQUARE);
strokeWeight(xWeight);
line(20, 20, 40, 40);
line(20, 40, 40, 20);
if (i == i) {
strokeCap(SQUARE);
strokeWeight(xWeight);
line(20+xSpace, 20, 40+xSpace, 40);
line(20+xSpace, 40, 40+xSpace, 20);
}
}
}
Answers
You can define your own function.
As an aside, I'm not sure what you intended with your line 16, but
if (i == i)
will always evaluate as true, becausei
is always equal to itself.Hope that helps.
you can make a class letter X
and have a list of them
(you could also use text() to write an 'X' )
@velvetkevorkian Thanks dude. I am new to code and I don't practice enough to understand everything perfectly. On the line 16 I was trying to come with a solution to move the symbol along the xpos axis. Let's say that i2 xpos is the same as i1 xpos, then i2 will be moved to a new value. i understand the logic but I don't know how to translate in code. By the way, can you explain me the line below? Why are you using float for those int?
void xDraw(float xWeight, float xNum, float xSpace)
@Chrisir Thanks for the code dude. This is really going along the lines I want. please help me understand the code. Could you help me understand this two lines? I am struggling to understand the logic behind them.
ArrayList<LetterX> lettersOfX = new ArrayList();
for (LetterX currentLetterX : lettersOfX)
I have tweaked your code a bit but I still having problems achieving my goal. I want to create a grid format using the X controlling the number of row and column in relation to the sketch size. I am not sure I am having the right logic though. Below I am trying to create a new line of X below the first one. It doesn't work and I am not sure this is the most practical solution. How can I solve this?
Thanks again.
Here's the code:
Actually, since the number of entries is a fixed 10 -
int xNum = 10;
, there would be no need to bother using an ArrayList!A regular array would be enough -
LetterX[] lettersOfX = new LetterX[xNum];
http://processing.org/reference/Array.html
http://processing.org/reference/ArrayList.html
That is called enhanced or for each loop!
It iterates over each element of lettersOfX, extracting them to currentLetterX variable on each iteration.
this gives you a grid:
ArrayList
ArrayList is a special list.
in the list there are items.
you can easily add an item using add (line 19)
Each item is an object LetterX - it has an position and an strokeWeight xWeight. It could also have a color. It is a package with properties (like position etc.) and functions (like display()).
also you can go through all it's elements by the for-loop (line 26) as gotoloop explained
grid
the way we achieve the grid is a double for-loop: for each column i we loop through all the rows j
we then use i and j to define a position of each X
this
says
invoke a new ArrayList of the name lettersOfX (what you need later in your code) and the items in it are of type LetterX
LetterX you defined yourself as a class
it's a list of items of LetterX
when we say
(line 16) we have a single item we can work with it.
We can then bring it into the list (line 19) using add()
similar in line 27 we get one item from the list and display it using display().
@Chrisir, @GoToLoop Thank you for your help. I am understanding better the logic behind the code. Now with the grid in place I am trying to create an Array with 3 colours that will be assigned randomly to the stroke(). I tried a few things with the code but no success. What I am doing wrong?
Thanks again.
Here's the latest code:
here
your lines 11 to 14 I had to rewrite
since the color of the X is also a property of X, the idea is to put the color into the class (line 46 / 52) and define it at the beginning like the other properties (line 26)
you might want to read the tutorial on classes / OOP:
https://forum.processing.org/tutorials/objects
or read the first 6 tutorials :
https://forum.processing.org/tutorials
Greetings, Chrisir ;-)
your Colours randomizer
is in my line 26
Here's my own tweak-ish take on it: :bz
@Chrisir, Thanks for the patience buddy. Your explanations are really useful. I will check the links and see if I can understand better classes / OOP.
@GoToLoop, Thanks dude, I really like the way everything seems more dynamic now. It will take me while to understand what you did there but I really appreciate the help.
A little variation of the theme. Instead of defining a custom LetterX class, this 1 relies on Processing's own built-in 1 called PVector!
PVector already got 3
float
fields: x, y, z.Fields x & y will take the role of the coordinate pair.
While z gonna store the ink!
Then, the former LetterX's display() method becomes sketch's display(PVector p) top-class function! :D
The following program totally does not work, but couldn't a simple grid with 'X's have been accomplished by using embedded
for
loops? Something like this:Again, this program totally does not do what I wanted it to do lol. However, couldn't something like this be done building on our basic knowledge of Processing that we have acquired thus far without jumping months ahead of our own experience?
yes.
@ Bilvad :
you wrote :
here
you wrote
your initial question was quote "a series of functions into one variable?" unquote. That's what a class is in a way.
I think you are intelligent enough to figure out the code we gave you, right?
So just make it part of your own experience...
;-)