Lines between Rects (returning values)
Hello,
I try to keep the questions until I am really stuck!
I have a sketch I am doing of what I want to be rectangles generated by a list read in by strings (eventually an arrayList so I can add to it). The rects and strings are in a class called node with an array to iterate, and I have code to word wrap the strings and adjust the height of the rects.
Now I am trying to find out the best way to make the heights and width of the rects available and get the x and y positions of the rects (a class called Node) so I can draw lines linking them.
I have had thoughts to makes the lines part of the Node class and iterate them with i, but without much luck, also looked at PVector but could not really get my head around returning the values needed and using them with the line draw in the right place (in draw, or class?).
Also, I am aware that maybe I don't have the rect Node movement code in the best place for this, as I can never seem to access one value or the other in draw even if I do a return value, be it the rect position for lines, or height and width for bouncing it off walls and other rects.
I've looked at a lot of code and reference material, but could really use an experienced eye to look and give me a steer. PVector? ArrayList? i and j? Return x and y values in some spot? I tried to return them in class and access in draw with no luck.
So I am looking for the most sensible way to draw lines between these nodes and the most logical place to put the movement code.
Many thanks if you could help. - the // line is the pseudocode I would like to work, and I would also like height/width to be accessable while still using the height generating code in the class! line 75 on is credited to a processing user and is the height code.
PS. I have looked at this;
https://forum.processing.org/topic/circles-with-lines-connecting-farthest-neighbor
But again, the same issue of making rects from a string list and generating heights from that confuses it.
- color r = int (random (50,200));
- color g = int (random (50,200));
- color b = int (random (50,200));
- color a = int (100);
- PFont font;
- int fontSize = 24;
- Node[] myNode = new Node[3];
- String[] newString = {"test0","teeeeeeeeeeeeeeeesttttttttttttttt1", "test2"};
- void setup(){
- size(1200,800);
- smooth();
- font = createFont("Calibri", fontSize);
- textFont(font,fontSize);
- for (int i = 0; i < myNode.length; i++) {
- myNode[i] = new Node (newString[i],320,8);
- }
- }
- void draw(){
- background (100);
- for (int i = 0; i < myNode.length;i++) {
- myNode[i].display();
- //line (myNode[i].posX,myNode[i].posY,myNode[i+1].posX,myNode[i+1].posY,);
- }
- }
- class Node {
- String nodeString = "";
- int specificWidth;
- int lineSpacing;
- //boolean dir = false;
- float posX= (float)random(200,600);
- float posY= (float)random(200,600);
- float dirX=1;
- float dirY=1;
- float spdX= (float) random(-1,1);
- float spdY= (float) random(-1,1);
- Node(String nodeString_,int specificWidth_,int lineSpacing_) {
- nodeString= nodeString_;
- specificWidth = specificWidth_;
- lineSpacing = lineSpacing_;
- float posX;
- float posY;
- float dirX;
- float dirY;
- float spdX;
- float spdY;
- }
- void display() {
- smooth();
- rectMode(CORNER);
- fill (r,g,b,a);
- rect(posX,posY,specificWidth,calculateTextHeight(nodeString, specificWidth, fontSize, lineSpacing));
- fill(255);
- text (nodeString,posX+(specificWidth/25),posY,specificWidth,1000);
- posX = posX+spdX*dirX;
- posY = posY+spdY*dirY;
- if (posX <= 0){
- dirX*=-1;
- }
- if (posX >= width-specificWidth){
- dirX*=-1;
- }
- if (posY <= 0){
- dirY*=-1;
- }
- if (posY >= height- calculateTextHeight(nodeString, specificWidth, fontSize, lineSpacing)){
- dirY*=-1;
- }
- }
- //code reference - http://processing.org/discourse/beta/num_1195937999.html
- //steven
- int calculateTextHeight(String nodeString, int specificWidth, int fontSize, int lineSpacing) {
- String[] wordsArray;
- String tempString = "";
- int numLines = 0;
- float textHeight;
- wordsArray = split(nodeString, " ");
- for (int i=0; i < wordsArray.length; i++) {
- if (textWidth(tempString + wordsArray[i]) < specificWidth) {
- tempString += wordsArray[i] + " ";
- }
- else {
- tempString = wordsArray[i] + " ";
- numLines++;
- }
- }
- numLines++;
- textHeight = numLines * (textDescent() + textAscent() + lineSpacing);
- return(round(textHeight));
- }
- }