meanings of codes

edited January 2016 in General Discussion

hi, i'm new to processing and i want to know the meanings of the different codes, can somebady please explain me the meanings of the different functions? thanks a lot

int N = 0;
int E = 1;
int S = 2;
int O = 3;
int dir = S;
int minL = 10;
int dWeight = 50;
int dStroke = 4;


float posX, posY;
float posXcross, posYcross;
float angleCount = 10;   
float angle = getRandomAngle(dir);
float stepSize = 5;


void setup(){
  size(800,800);
  colorMode(HSB, 360, 100, 100, 100);
  smooth();
  background(360);

  posX = int(random(0, width));
  posY = 5;
  posXcross = posX;
  posYcross = posY;
}

void draw(){
  for (int i=0; i<=posX; i++) {
        strokeWeight(1);
        point(posX,posY); 
    }
   // posX = posX + cos(...... ; 
    posX += cos(radians(angle)) * stepSize;
    posY += sin(radians(angle)) * stepSize;

    boolean reachedBorder = false;

    if (posY <= 5) {
      dir = S;
    } 
    else if (posX >= width-5) {
      dir = O;
    }
    else if (posY >= height-5) {
      dir = N;
    }
    else if (posX <= 5) {
      dir = E;
    }

    int x = (int) posX;
    int y = (int) posY;
    if (get(x, y) != color(360) || reachedBorder) {
      angle = getRandomAngle(dir);
      float distance = dist(posX, posY, posXcross, posYcross);
      if (distance >= minL) {
        strokeWeight(distance/dWeight);

        line(posX, posY, posXcross, posYcross);
      }
      posXcross = posX;
      posYcross = posY;
    }
  }

float getRandomAngle(int theDirection) {
float a = (floor(random(-angleCount, angleCount)*7));

  if (theDirection == N) return (a - 160);
  if (theDirection == E) return (a);
  if (theDirection == S) return (a + 130);
  if (theDirection == O) return (a + 180);

  return 0;
}

Answers

  • edited January 2016

    You will some very useful tutorials on Processing here: https://processing.org/tutorials/ They cover basic to advanced topics. You'll also find the Processing language reference here: https://processing.org/reference/ which covers the commands available in Processing.

    Having someone "explain" it to you, especially if you don't have a grasp on the basics of the language is pretty useless. I'd suggest you spend a little time on the tutorials and language commands and see how much YOU can work out first.

    Cheers.

  • I have deleted the duplicate discussion. Do not post the same question more than once.

    Edit the original post so that your code appears formatted. You can learn how to do this here.

    How much of the code do you understand? Try adding your own comments. If you are not sure what the code does try changing some of the values used and see what the effect is.

  • ok thank you. i have been studying it for 4 months and particularly i can't afford the meaning of :

     posX += cos(radians(angle)) * stepSize; 
     posY += sin(radians(angle)) * stepSize;
    
    boolean reachedBorder = false;
    
    
    
    
    int x = (int) posX;
        int y = (int) posY;
        if (get(x, y) != color(360) || reachedBorder) {
          angle = getRandomAngle(dir);
          float distance = dist(posX, posY, posXcross, posYcross);
    

    can you help me please?

  • I'm not one to give explicit explanations, but I will help you in the general direction so you will learn a trick or two. :) In a nutshell, 2 variables check to see if they are either on either a while or black pixel, or if the edge of the display has been met. If either condition is true, a new angle and distance is calculated and the line plotting takes off in another direction until the next time either a black line or display edge is found. If you're using Processing 3, then could run this in the debugger (set breakpoints at the beginning and the end of the function), and watch the variables change as you step through the sketch. Very useful for understanding how a program, or a function, loop, etc works. I hope this will prove helpful. Cheers.

  • so when it says :" get(x,y) !=(360) || reachedBorder " it means that if a line reaches a point with that color for examples then it calculated it like the boolean reachedBorder and so it calculate a new angle of drawing the next line like it has rached the edge of the sketch, am i right? :)

  • edited January 2016

    Yes. Well done! "get(x,y) !=(360) || reachedBorder" means "check the color of the pixel at location x,y. If the color of the pixel is NOT white OR we have reached the display border, then calculate a new angle and plot in the new direction. If you examine the background() function in void setup() you'll see that is where we get the white color to test against.

  • so the " || " stands for " or" am i right? while " != " means that the previous values are not the color, for examples 360, that i chose for the background and so every other color that the line will encounter in his path will count as a "border" and it allows to change the direction. did i have understand?

  • Answer ✓

    Yes, the || makes a boolean comparison of "or" while != is the assignment comparison of "not equal to". The two comparisons are different. So, in that program statement, if x, y is over ANY other color than white, OR x,y succeeds in the reachBorder boolean test and becomes "TRUE", then the loop falls through and creates a new random angle and plots in a new direction. If you go up to the background(360) statement and change 360 to any other value and run the program, see what happens. :)

  • ok thank you so much :)

  • You are most welcome. Good luck with you studies!

Sign In or Register to comment.