Code from page

edited April 2014 in Hello Processing

Where can i find code from this page >>> http://hello.processing.org/ <<< Im interesting in this code very much.

«13

Answers

  • or someone know how connect 2 points by line like on that page

  • edited April 2014
    int x,y,z,w ;
    void setup()
    {
     size(640, 360); 
    }
    void draw()
    { 
    x= mouseX;
    y= mouseY;
    if (mousePressed==true) 
    {
      fill(255,0,0);
    rect(mouseX,mouseY,5,5);
    point(pmouseX,pmouseY);
    z=pmouseX;
    w=pmouseY;
    }
    line(z,w,mouseX,mouseY );
    }
    

    look that, i want only 1 line behind two points, but in my code there are a lot of lines from each point. Can you check my code?

  • when you use background you clear the canvas

    click mouse then

    int x, y, z, w ;
    void setup()
    {
      size(640, 360);
      background(111);
    }
    void draw()
    { 
      background(111);
      x= mouseX;
      y= mouseY;
      if (mousePressed==true) 
      {
        fill(255, 0, 0);
        rect(mouseX, mouseY, 5, 5);
        point(pmouseX, pmouseY);
        z=pmouseX;
        w=pmouseY;
      }
      line(z, w, mouseX, mouseY );
    }
    
  • if (mousePressed==true)

    also ok

    if (mousePressed)

  • But after drawing line i want to save it on the board.I want to draw a square with lines

  • and if it possible connect points like on that page http://hello.processing.org if they are near

  • here

    int x, y, z, w ;
    void setup()
    {
      size(640, 360);
      background(111);
    }
    void draw()
    { 
      //  background(111);
      x= mouseX;
      y= mouseY;
      if (mousePressed==true) 
      {
        fill(255, 0, 0);
        rect(mouseX, mouseY, 5, 5);
        point(pmouseX, pmouseY);
    
        if (z>0&&w>0)
          line(z, w, mouseX, mouseY );
        z=mouseX;
        w=mouseY;
      }
    }
    
  • edited April 2014

    you wrote

    and if it possible connect points like on that page http://hello.processing.org if they are near

    to achieve this: you have to store the points in an ArrayList and make a check which points are near and then connect

    maybe look for it in www.openprocessing.org

    http://www.openprocessing.org/sketch/8168

  • thank you very much for help, i study processing only few days so you save my time :D

  • how to draw line from point which i chose????

  • edited April 2014

    when you click the mouse and click again elsewhere you have a line.

    That's the point you chose: where you click.

  • edited April 2014

    with this new version if you hold a key on the keyBoard, you don't draw lines

    thus you can draw e.g. two independent rectangles using the mouse

    int x, y, z, w ;
    void setup()
    {
      size(640, 360);
      background(111);
    }
    void draw()
    { 
      //  background(111);
      x= mouseX;
      y= mouseY;
      if (mousePressed==true) 
      {
        fill(255, 0, 0);
        rect(mouseX, mouseY, 5, 5);
        point(pmouseX, pmouseY);
    
        if (z>0&&w>0&&!keyPressed)
          line(z, w, mouseX, mouseY );
        z=mouseX;
        w=mouseY;
      }
    }
    
  • another one question, how can i delet line or point or another one figure.Can you help me with this ?

  • edited April 2014

    when you hold q on the keyboard you delete canvas with the mouse (press mousebutton)

    int x, y, z, w ;
    void setup()
    {
      size(640, 360);
      background(111);
    }
    void draw()
    { 
      //  background(111);
    
      stroke(0);
    
      x= mouseX;
      y= mouseY;
      if (mousePressed) 
      {
        fill(255, 0, 0);
        rect(mouseX, mouseY, 5, 5);
        point(pmouseX, pmouseY);
    
        if (z>0 && w>0 && !keyPressed) {
          line(z, w, mouseX, mouseY );
        } // if 
    
        // delete 
        if (keyPressed && key=='q') {
          fill(111);
          noStroke();
          rect(mouseX-25, mouseY-25, 50, 50);
        } // if 
        else {  
          z=mouseX;
          w=mouseY;
        } // else
      } // if
    } // func 
    // 
    
  • and the last question)) how can i count how many pixels in the line (i want to know Cables length)

  • edited April 2014

    use dist() and add all dists of one bus

    see reference for dist()

  • i dont know how to do it where should i use dist()?

  • here ...

    with dist() you give two points and get the distance between them

    line as well gets two points as parameters

    int x, y, z, w ;
    float distTotal;
    
    
    void setup()
    {
      size(640, 360);
      background(111);
    }
    void draw()
    { 
      //  background(111);
    
      stroke(0);
    
      x= mouseX;
      y= mouseY;
      if (mousePressed) 
      {
        fill(255, 0, 0);
        rect(mouseX, mouseY, 5, 5);
        point(pmouseX, pmouseY);
    
        if (z>0 && w>0 && !keyPressed) {
          line(z, w, mouseX, mouseY );
          distTotal = distTotal + dist( z, w, mouseX, mouseY )  ;
          println (distTotal);
        } // if 
    
        // delete 
        if (keyPressed && key=='q') {
          fill(111);
          noStroke();
          rect(mouseX-25, mouseY-25, 50, 50);
        } // if 
        else {  
          z=mouseX;
          w=mouseY;
        } // else
      } // if
    } // func 
    // 
    
  • how can i print "distTotal" on monitor?

  • textFont(verdana, 20);
    fill(0);
    text("distTotal",10,25);
    

    i tried to do it, but it print only word "distTotal"

  • Without the " "

  • textFont(verdana, 20);
    fill(0);
    text(distTotal,10,25);
    
  • edited April 2014

    here... line 28 following ...

    int x, y, z, w ;
    float distTotal;
    
    
    void setup()
    {
      size(640, 360);
      background(111);
    }
    void draw()
    { 
      //  background(111);
    
      stroke(0);
    
      x= mouseX;
      y= mouseY;
      if (mousePressed) 
      {
        fill(255, 0, 0);
        rect(mouseX, mouseY, 5, 5);
        point(pmouseX, pmouseY);
    
        if (z>0 && w>0 && !keyPressed) {
          line(z, w, mouseX, mouseY );
          distTotal = distTotal + dist( z, w, mouseX, mouseY )  ;
          println (distTotal);
          // delete old text
          fill(111);
          noStroke();
          rect (0, 0, 100, 20);
          // bring in new text
          fill(255);
          text (int(distTotal), 12, 12);
        } // if 
    
        // delete 
        if (keyPressed && key=='q') {
          fill(111);
          noStroke();
          rect(mouseX-25, mouseY-25, 50, 50);
        } // if 
        else {  
          z=mouseX;
          w=mouseY;
        } // else
      } // if
    } // func 
    // 
    
  • please, if you can check this code.I want to draw ellipse\rect if i click on icon, but when i pressed button on my mouse i draw a line on ellipse\rect boolean [] button1; boolean [] button2; int i, n,u; void setup() { background(250);

      size(1250,690);
    
      button1 = new boolean[1];
        button2 = new boolean[1];
    
    }
    void draw()
    {
    
     if(button1[i]) 
       {  
    if (mousePressed == true) 
             {
               if ( mouseY>120)
               {
              strokeWeight(4);
              rect(mouseX,mouseY,56,48);
             }
             }
             fill(255,0,0);
       }
     else 
          {
    fill(0,255,0);
          }
          strokeWeight(3);
    rect(330,26,56,48);
     if(button2[u]) 
       {  
    if (mousePressed == true) 
             {
               if ( mouseY>120)
               {
              strokeWeight(4);
              ellipse(mouseX,mouseY,56,48);
             }
             }
             fill(255,0,0);
       }
     else 
          {
    fill(0,255,0);
          }
          strokeWeight(3);
    rect(430,26,56,48);
    }
    void mouseClicked(){
        if(dist(360,38,mouseX, mouseY)<=32)
        {
          button1[n] = !button1[n];
        }    
            if(dist(460,58,mouseX, mouseY)<=32){
          button2[n] = !button2[n];
            }  
      }
    
  • here

    boolean [] button1; 
    boolean [] button2; 
    int i=0, n=0, u=0; 
    
    
    void setup() { 
      background(250);  
      size(1250, 690);
    
      button1 = new boolean[1];
      button2 = new boolean[1];
    }
    
    void draw()
    {
    
      if (button1[i])
      { 
        if (mousePressed == true)
        {
          if ( mouseY>120)
          {
            strokeWeight(4);
            rect(mouseX, mouseY, 56, 48);
          }
        }
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
    
      strokeWeight(3);
      rect(330, 26, 56, 48);
      if (button2[u])
      { 
        if (mousePressed == true)
        {
          if ( mouseY>120)
          {
            strokeWeight(4);
            ellipse(mouseX, mouseY, 56, 48);
          }
        }
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
      strokeWeight(3);
      ellipse(450, 50, 56, 48);
    }
    void mouseClicked() {
      if (dist(360, 38, mouseX, mouseY)<=32)
      {
        button1[n] = !button1[n];
      }   
      if (dist(460, 58, mouseX, mouseY)<=32) {
        button2[n] = !button2[n];
      }
    }
    //
    
  • no i think that you didnt understand me, i want draw 1 ellipse\rect when i mousePressed but know i draw a lot of ellipse\rect ( on 1 line )

  • here

    boolean [] button1; 
    boolean [] button2; 
    int i=0, n=0, u=0; 
    
    
    void setup() { 
      background(250);  
      size(1250, 690);
    
      button1 = new boolean[1];
      button2 = new boolean[1];
    }
    
    void draw()
    {
      if (button1[i])
      { 
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
    
      strokeWeight(3);
      rect(330, 26, 56, 48);
      if (button2[u])
      { 
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
      strokeWeight(3);
      ellipse(450, 50, 56, 48);
    }
    
    void mouseClicked() {
      if (dist(360, 38, mouseX, mouseY)<=32)
      {
        button1[n] = !button1[n];
      }   
      else if (dist(460, 58, mouseX, mouseY)<=32) {
        button2[n] = !button2[n];
      }
    
    
      {
        if (button1[i])
        { 
          if ( true)
          {
            if ( mouseY>120)
            {
              strokeWeight(4);
              rect(mouseX, mouseY, 56, 48);
            }
          }
        }
    
    
        if (button2[u])
        { 
          if ( true)
          {
            if ( mouseY>120)
            {
              strokeWeight(4);
              ellipse(mouseX, mouseY, 56, 48);
            }
          }
        }
      }
    }
    //
    
  • thx u for help, but look if you pressed rect you draw rect, when you pressed circle you dwar circle but when i pressed rect and them pressed circle draw both rect+circle i tried to fix this but i dont know how, when i pressed circler (ICON) and then rect(icon) i want turn off circle and drawing rect

  • edited April 2014

    Here, I have commented the modifications

    boolean [] button1; 
    boolean [] button2; 
    int i=0, n=0, u=0; 
    
    
    void setup() { 
      background(250);  
      size(1250, 690);
    
      button1 = new boolean[1];
      button2 = new boolean[1];
    }
    
    void draw()
    {
      if (button1[i])
      { 
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
    
      strokeWeight(3);
      rect(330, 26, 56, 48);
      if (button2[u])
      { 
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
      strokeWeight(3);
      ellipse(450, 50, 56, 48);
    }
    
    void mousePressed() {
      if (dist(360, 38, mouseX, mouseY)<=32)
      {
        button1[n] = !button1[n];
        button2[n] = false; // add these two line that's all
      }   
      else if (dist(460, 58, mouseX, mouseY)<=32) {
        button2[n] = !button2[n];
        button1[n] = false;  // add these two line that's all
      }
    
    
      {
        if (button1[i])
        { 
          if ( true)
          {
            if ( mouseY>120)
            {
              strokeWeight(4);
              rect(mouseX, mouseY, 56, 48);
            }
          }
        }
    
    
        if (button2[u])
        { 
          if ( true)
          {
            if ( mouseY>120)
            {
              strokeWeight(4);
              ellipse(mouseX, mouseY, 56, 48);
            }
          }
        }
      }
    }
    
  • if(button14[xx]) 
       {  
         if ( true)
               {
    if ( keyPressed  ) {
    background(250);
      for (int k=20; k < 1250; k += 20) 
    {
    strokeWeight(0);
    line(0 + k , 100 , 0 + k,1250);
    line(0 , 100 + k , 1250 ,100 + k);
    }
    }
               }
    

    im for perseverance but look at this code, here when i pressed key i use background(250); and delete all at window but when i againe draw line

    if(button10[x]) 
       {  
    if (mouseY > 110)
    {if ( true)
               {
      if (mousePressed==true) 
      {
        fill(255, 0, 0);
        rect(mouseX, mouseY, 1, 1);
        point(pmouseX, pmouseY);
     strokeWeight(8);
        if (zz>0&&zz>0&&!keyPressed)
          line(zz, zzz, mouseX, mouseY );
          distTotal2=distTotal2 + (dist( zz, zzz, mouseX, mouseY )/100)  ;
        zz=mouseX;
        zzz=mouseY;
      }
               }
    

    i connect with last point which i drew defore background(250);

  • edited April 2014

    when using background also say

    zz=-1;
    

    this is checked by the if-clause in line 12; no line is made when zz is ==-1

  • edited April 2014

    thx

  • edited April 2014

    yeah

    just use zz=-1;

    don't use z=-1;

    Also:

    after the if-line (line 12) please insert { and

    insert } before zz=mouseX; (line 15)

  • edited April 2014

    but i have 2 questions and thats all) if (mousePressed==true) { fill(255, 0, 0); rect(mouseX, mouseY, 1, 1); point(pmouseX, pmouseY); strokeWeight(8); if (zz>0&&zz>0&&!keyPressed) line(zz, zzz, mouseX, mouseY ); distTotal2=distTotal2 + (dist( zz, zzz, mouseX, mouseY )/100) ; zz=mouseX; zzz=mouseY; } when i use this code i draw and count line but first points with cordination (35;35) give me first dist 35 + another point (8;8) will be 35 + distance before 2 point i dont want count first point i tried do it with for(); but it didnt work

  • edited April 2014

    How to post code

    when you post your code:

    • in the editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before and 1 empty line after it

    • mark the code (without the 2 empty lines)

    • press C in the small command bar.

  • Also:

    after the if-line (line 12) please insert { and

    insert } before zz=mouseX; (line 15)

  • im sorry but i dont understand (

  • insert an opening bracket {

    and a closing bracket

    so that the excuted part of the if clause is not one line but two lines

    if (zz>0&&zz>0&&!keyPressed)
       {
          line(zz, zzz, mouseX, mouseY );
          distTotal2=distTotal2 + (dist( zz, zzz, mouseX, mouseY )/100)  ;
      }
        zz=mouseX;
    
  • And the main problem which i cant solve

          if (zz>0&&zz>0&&!keyPressed)
            {
              line(zz, zzz, mouseX, mouseY );
              distTotal2=distTotal2 + (dist( zz, zzz, mouseX, mouseY )/50) ;
            }
            zz=mouseX;
            zzz=mouseY;
          }
    

    here i have code which build some figure, and now i want to count square if this figure i tried to multiply 1 side on other side (but it didnt wokr) and i think can be other figure circle or triangle

  • is english your 1st language?

    please use more empty lines, like I do

    I don't understand "now i want to count square if this figure"

  • ok, can you show your entire code please?

    Do you want that the program can tell you whether you just draw a square, a triangle or a circle?

  • I know russian ukrainian and polish languages and i started lerniang elglish 1 year ago.I want to draw rect\circle or something else and count area of ​​the figure

  • No offense

    Do you want to measure the size of the area enclosed by the rect?

    How many pixels there are in it?

  • look i draw some figure ( circle, rect, triangle) and count are of this figure which i drew.

  • please post your entire code...

  • here... based on an old version...

    boolean [] button1; 
    boolean [] button2; 
    int i=0, n=0, u=0; 
    
    int countCircles, countRects;
    
    void setup() { 
      background(250);  
      size(1250, 690);
    
      button1 = new boolean[1];
      button2 = new boolean[1];
    }
    
    void draw()
    {
      fill(255);
      noStroke();
      rect(0, 0, 180, 32);
      fill(0);
      text ( countCircles + " circles and " + countRects +  " rects.", 14, 14);
    
      stroke(0);
    
    
      if (button1[i])
      { 
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
    
      strokeWeight(3);
      rect(330, 26, 56, 48);
      if (button2[u])
      { 
        fill(255, 0, 0);
      }
      else
      {
        fill(0, 255, 0);
      }
      strokeWeight(3);
      ellipse(450, 50, 56, 48);
    
      //
    } // end of draw() 
    
    void mousePressed() {
      if (dist(360, 38, mouseX, mouseY)<=32)
      {
        button1[n] = !button1[n];
        button2[n] = false; // add these two line that's all
      }   
      else if (dist(460, 58, mouseX, mouseY)<=32) {
        button2[n] = !button2[n];
        button1[n] = false;  // add these two line that's all
      }
    
    
      {
        if (button1[i])
        { 
          if ( true)
          {
            if ( mouseY>120)
            {
              strokeWeight(4);
              rect(mouseX, mouseY, 56, 48);
              countRects++;
            }
          }
        }
    
    
        if (button2[u])
        { 
          if ( true)
          {
            if ( mouseY>120)
            {
              strokeWeight(4);
              ellipse(mouseX, mouseY, 56, 48);
              countCircles++;
            }
          }
        }
      }
    }
    
    //
    
  • if (zz>0&&zz>0&&!keyPressed)
      {
        line(zz, zzz, mouseX, mouseY );
        distTotal2=distTotal2 + (dist( zz, zzz, mouseX, mouseY )/50) ;
      }
      zz=mouseX;
      zzz=mouseY;
    }
    

    nono look this code you can draw some line, with this line you can build some figure i want to draw rect or triangle with this lines and count area of figure

  • post your entire code

Sign In or Register to comment.