random lines

edited October 2013 in How To...

i'm trying to build a system which starts begins with an ellipse in the centre of the page, and from that a number of lines grow in different directions. once each line is complete, an ellipse would form and the thing would grow large. i have attempted this a few times and quit in a rage. if anyone could show me how to loop lines growing in randomly set directions, so i can learn what is going on, that would be awesome.

i apologise for my lack of capitalisation, my keyboard is broken...

cheers.

Answers

  • edited October 2013

    You can change screen origin coordinates from (0,0) to center of screen w/ -> translate(width>>1, height>>1);. 8-X

  • for random directions just pick a random angle between 0 and TWO_PI and then the direction is just length * cos(angle) in the x, length * sin(angle) in the y.

  • _vk_vk
    edited October 2013

    Something like this: [code edited] thanks koogs

    ArrayList<GrowingLine> gl = new ArrayList<GrowingLine>();
    void setup() {
      size(400, 400);
      gl.add(new GrowingLine(width/2, height/2));
      stroke(150);
    }
    
    void draw() {
      background(255);
      for (GrowingLine line:gl) {
        line.display();
      }
      if (frameCount % 100 == 0)
        gl.add(new GrowingLine(width/2, height/2));
    }
    
    class GrowingLine {
    
      float x, y, a, leng, l, x2, y2;
    
      GrowingLine(int _x, int _y) {
        x =_x;
        y =_y;
        a = random(TWO_PI);
        leng = random(20, min(width/2, height/2));
        l = 0;
      }
    
      void display() {
        if (l < leng) {
          l++;
        }else{
          ellipse(x2, y2, 8, 8);    
        }
    
        x2 = x + cos(a)*l;
        y2 = y + sin(a)*l;
    
        line(x, y, x2, y2);
      }
    }
    
  • logical error in line 37?

  • _vk_vk
    edited October 2013

    Ops, corrected. thanks. As the width and height were the same, I missed it.

  • You posted in Questions about Code, but didn't show any code!

    Please, read the Category descriptions. Thanks. (Moved.)

  • edited October 2013 Answer ✓

    not reading the replys so far

    this stores the winning possibilities as String

    and takes this apart and uses it as index for the field

    it counts the fields per row and if it's == 3, he won

    boolean HatEinSpielerGewonnen (String Spieler1) {
      // using: String[] Reihen = 
      // { "012", "345", "678", "036", "147", "258", "048", "246" };
      boolean Buffer = false;
      int Position;
      int count;
      for (int i = 0; i < Reihen.length; i = i+1) {
        count=0;
        for (int j = 0; j < Reihen[i].length(); j = j+1) {
          Position = integerFromChar( Reihen[i].charAt(j) );
          if (Spielfeld[Position].equals(Spieler1)) {
            count++;
          }
        } 
        if (count == 3) {
          Buffer=true;
          break;
        }
      }
      return (Buffer);
    }
    
    
    int integerFromChar(char MyChar) {
      int Buffer = 0;
      switch (MyChar) {
      case '0':
        Buffer = 0;
        break;
      case '1':
        Buffer = 1;
        break;
      case '2':
        Buffer = 2;
        break;
      case '3':
        Buffer = 3;
        break;  
      case '4':
        Buffer = 4;
        break;  
      case '5':
        Buffer = 5;
        break;  
      case '6':
        Buffer = 6;
        break;  
      case '7':
        Buffer = 7;
        break;  
      case '8':
        Buffer = 8;
        break;  
      case '9':
        Buffer = 9;
        break;  
      default:
        //
        break;
      } // switch
    
      return ( Buffer ) ;
    } // function
    

    from http://www.openprocessing.org/sketch/29501

  • int integerFromChar(char c) {
      return c - '0';
    }
    
  • edited October 2013

    A much shorter and efficient alternative to snippet integerFromChar(): 3:-O

    static final int integerFromChar(char c) {
      return constrain(c - '0', 0, 9);
    }
    

    P.S.: Oops. After refreshing, seen koogs' got same solution already! :O)

Sign In or Register to comment.