Loading...
Logo
Processing Forum
Hi,
I want to set some code I found to make random stars, but I want to change it so I can control # of spikes and radius with number boxes,

but I havent understand how to accomplish this,
here is the code, Thank you:
 
Copy code
  1. // analizando programa star,
    // donde llama a las classes?

    Star star; //
    import controlP5.*;

    ControlP5 controlP5;

    Textlabel myTextlabelA;
    Textlabel myTextlabelB;

    int a = 8;

    public int numberboxValue = 10;

    void setup() {
      size(800,600);
      //frameRate(30);
      controlP5 = new ControlP5(this);
      controlP5.setControlFont(new ControlFont(createFont("Georgia",20), 20));

      myTextlabelA = controlP5.addTextlabel("label","ISHTAR",10,250);
      myTextlabelA.setColorValue(0xffcccccc);
      myTextlabelB = controlP5.addTextlabel("label","IX",20,-250);
      //myTextlabelB = new Textlabel(this,"a single textlabel big stuff.",20,20,400,200);

      controlP5.addNumberbox("numberboxValue",0,20,50,80,14);

      smooth();
      star = new Star();

    }



    void draw() {
      //background(0);
      //myTextlabelB.draw(this);
      fadeToBlack();
      star.draw();
    }

    // EMPIEXA kkkkkkkkkkkkkkkkkkkk

     

    void fadeToBlack(){
      // fade away
      fill(0,0,0, 30);
      noStroke();
      translate(0,0); //centrar??
      rect(0,0,width,height);
    }
     
     
    // ================================================================ separador para las clases?
     
     
    class Star //class 1 notar la mayuscula,
    {
      // define variables, propiedades
      float phase = 0;
      float r1=0, r2=0;
      int spikes=0;
      Point[] points;
       
      Star(){ //constructor, mismo nombre que la clase, no inizialisa pues no hay variables?
        setProps(); //metodo?? cual funcion??
      }
       
      public void draw(){ //porque con nombre draw??
         
        translate(width/2, height/2);
        noStroke();
        //strokeWeight(.5);
        //stroke(255,255,255);
        //noFill();
        fill(255,255,255, 50);
         
       // r1 = 200;
       // r2 = 60;
       // r1 = height * cos(phase);
        //r2 = height/5 * cos(phase);
        phase += .1;
         //r1 = ((width/2) - mouseX) / 10;
         calcPoly();
         render();
         if(frameCount % 60 == 0) setProps();
      }
       
      private void setProps(){
        //spikes = int(random(174))+6;//getNumSpikes();
        spikes = getto();
        r1 = random(200)+50;
        r2 = random(150);
        int total = int(spikes*2);
        points = new Point[ total ];
      }
       
      private void calcPoly(){
         Point p;
         float i=0, n = points.length;
         float a = 360/n;
         float cosa, sina;
          
         for(i=0;i<n;i++){
           points[int(i)] = new Point(0,0);
            p = points[int(i)];
            cosa = cos((a*i) * PI/180);
            sina = sin((a*i) * PI/180);
            if(i%2==0){
               p.x = r1 * cosa;
               p.y = r1 * sina;
            }else{
               p.x = r2 * cosa;
               p.y = r2 * sina;
            }
             
         }
      }
       
      private void render(){
         int i=0, n=points.length;
         Point p1, p2;
          
         pushMatrix();
         beginShape(POLYGON);
        // rotateY( ry );
          
         for(i=0; i<n; i++){
           if(i>0){
             p1 = points[i-1];
             p2 = points[i];
             //line( p1.x, p1.y, p2.x, p2.y );
             vertex( p1.x, p1.y );
             vertex( p2.x, p2.y );
             if(i==n-1) {
               p1 = points[0];
               //line( p1.x, p1.y, p2.x, p2.y );
             }// end if
           }// end if
         }// end for
          
         endShape();
         popMatrix();
      }
    }
     
    class Point { //class point
       public float x, y; // propiedades
       Point(float x, float y){ //constructor
         this.x = x; // inializacion??
         this.y = y;
       }
    }

    int getto()
    {
    a = numberboxValue;
    return a;
    }

Replies(3)

you didnt add any slider yet. did you check out the slider example that comes with controlP5 ?

http://www.sojamo.de/libraries/controlP5/examples/ControlP5slider/ControlP5slider.pde
hi,
thank you
I just edited
is a number box
in the code is inserted, but not working yet, thank you
i tweaked the source a bit and added 2 sliders, also see the link cedric provided. i added some comments and hope they help to understand the changes. 


Copy code
  1. import controlP5.*;

  2. Star star;
  3. ControlP5 controlP5;

  4. // number of spikes, will change when the value of the numberbox changes
  5. public int numberboxValue=10; 

  6. // radius of the star, changes according to the value of slider radius
  7. public float radius = 100; 

  8. // spikiness of a star, percental to the radius. 
  9. // changes according to the value of slider spikiness
  10. public float spikiness = 0.5; 

  11. void setup(){
  12.   size(800,600);
  13.   controlP5=new ControlP5(this);
  14.   // notice that the name of the numberbox and the name of the 
  15.   // variable numberboxValue correspond (same goes for the 2 sliders below)
  16.   // controlP5 will now automatically change your variables when the
  17.   // value of a controller changes.
  18.   controlP5.addNumberbox("numberboxValue",numberboxValue,20,50,80,14);
  19.   controlP5.addSlider("radius",0,400, radius,20,100,200,10);
  20.   controlP5.addSlider("spikiness",0,1, spikiness,20,120,200,10);
  21.   smooth();
  22.   noStroke();
  23.   star=new Star();
  24. }

  25. void draw(){
  26.   // adding pushMatrix to prevent an offset of the 
  27.   // matrix when drawing controlP5 elements 
  28.   // + add popMatrix at the end of draw()
  29.   pushMatrix(); 

  30.   fadeToBlack();
  31.   star.draw();

  32.   popMatrix();
  33. }

  34. void fadeToBlack(){
  35.   fill(0,0,0,30);
  36.   rect(0,0,width,height);
  37. }


  38. class Star {
  39.   float phase=0;
  40.   float r1=0,r2=0;
  41.   int spikes=0;
  42.   Point[] points;

  43.   Star() {
  44.     setProps();
  45.   }

  46.   void draw(){
  47.     pushMatrix(); // adding pushMatrix to keep the matrix in order
  48.     translate(width/2, height/2);
  49.     noStroke();
  50.     fill(255,255,255,50);
  51.     phase+=.1;
  52.     calcPoly();
  53.     render();
  54.     setProps();
  55.     popMatrix();
  56.   }

  57.   void setProps(){
  58.     spikes=getto();

  59.     // apply the radius and spikiness to the star
  60.     r1=radius - (spikiness*radius);
  61.     r2=radius;
  62.     int total=int(spikes*2);
  63.     // in case total is negative, cut the value off at 0
  64.     total = max(0,total); 
  65.     points=new Point[total];
  66.   }

  67.   void calcPoly(){
  68.     Point p;
  69.     float i=0,n=points.length;
  70.     float a=360/n;
  71.     float cosa,sina;
  72.     for(i=0;i<n;i++){
  73.       points[int(i)]=new Point(0,0);
  74.       p=points[int(i)];
  75.       cosa=cos((a*i)*PI/180);
  76.       sina=sin((a*i)*PI/180);
  77.       if(i%2==0){
  78.         p.x=r1*cosa;
  79.         p.y=r1*sina;
  80.       }
  81.       else{
  82.         p.x=r2*cosa;
  83.         p.y=r2*sina;
  84.       }
  85.     }
  86.   }

  87.   void render(){
  88.     int i=0,n=points.length;
  89.     Point p1,p2;
  90.     pushMatrix();
  91.     beginShape(POLYGON);
  92.     for(i=0;i<n;i++){
  93.       if(i>0){
  94.         p1=points[i-1];
  95.         p2=points[i];
  96.         vertex(p1.x,p1.y);
  97.         vertex(p2.x,p2.y);
  98.         if(i==n-1){
  99.           p1=points[0];
  100.         }//endif
  101.       }//endif
  102.     }//endfor
  103.     endShape();
  104.     popMatrix();
  105.   }
  106. }

  107. class Point{
  108.   float x,y;
  109.   Point(float theX,float theY){
  110.     x=theX;
  111.     y=theY;
  112.   }
  113. }

  114. int getto() {
  115.   return numberboxValue;
  116. }

andreas schlegel, www.sojamo.de