Why my array of a class of cones only draws a strange line?

edited June 2016 in Questions about Code

I have an array of a class that is supposed to draw a cone, but it only draws a line :/ (help please):

Cono[] conos;

void setup(){ size(800,500,P3D); background(115); conos = new Cono [34]; for (int i=0; i<conos.length; i++){ conos[i]=new Cono (); } }

void draw(){ lights(); translate(width / 2, height / 2); fill(255, 255, 255); translate(0, -40, 0); for (int i=0; i<conos.length; i++){ conos[i].dibujame(10, 150, 180, 100); } }

public class Cono { float angle; float angleIncrement; int sides;

Cono() { angle = 0; angleIncrement = TWO_PI / sides; }

void dibujame(float topRadius, float bottomRadius, float tall, float sides) { beginShape(QUAD_STRIP); for (int i = 0; i < sides + 1; ++i) { vertex(topRadiuscos(angle), 0, topRadiussin(angle)); vertex(bottomRadiuscos(angle), tall, bottomRadiussin(angle)); angle += angleIncrement; } endShape(); if (topRadius != 0) { angle = 0; beginShape(TRIANGLE_FAN); // Center point vertex(0, 0, 0); for (int i = 0; i < sides + 1; i++) { vertex(topRadius * cos(angle), 0, topRadius * sin(angle)); angle += angleIncrement; } endShape(); } } }

Answers

  • Cono[] conos;
    
    void setup() { 
      size(800, 500, P3D); 
      background(115); 
    
      conos = new Cono [34]; 
    
      for (int i=0; i<conos.length; i++) { 
        conos[i]=new Cono ();
      }
    }
    
    void draw() { 
      lights(); 
      translate(width / 2, height / 2); 
      fill(255, 255, 255); 
      translate(0, -40, 0); 
      for (int i=0; i<conos.length; i++) { 
        conos[i].dibujame(10, 150, 180, 100);
      }
    }
    
    class Cono { 
      float angle; 
      float angleIncrement; 
      int sides;
    
      Cono() { 
        angle = 0; 
        angleIncrement = TWO_PI / sides;
      }
    
      void dibujame(float topRadius, float bottomRadius, float tall, int sides) { 
    
        angleIncrement = TWO_PI / sides;
    
        beginShape(QUAD_STRIP); 
        for (int i = 0; i < sides + 1; ++i) { 
          vertex(topRadius*cos(angle), 0, topRadius*sin(angle)); 
          vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle)); 
          angle += angleIncrement;
        } 
        endShape(); 
    
        if (topRadius != 0) { 
          angle = 0; 
          beginShape(TRIANGLE_FAN); 
          // Center point
          vertex(0, 0, 0); 
          for (int i = 0; i < sides + 1; i++) { 
            vertex(topRadius * cos(angle), 0, topRadius * sin(angle)); 
            angle += angleIncrement;
          } 
          endShape();
        }
      }
    }
    
  • maybe you find time to read the tutorial on objects

    I mean there are a few misunderstandings here with your class...

    e.g. in the class in line 27 int sides; but this variable gets over shadowed by the var sides in line 34 (the parameter of the same name)

    I had to introduce line 36 because in line 31 sides is 0.

    best would be to put those data in the constructor of the class and define the shape here and in dibujame() / display() just say shape (s, 330, 200); or so

  • Chrisir thanks for formating my code and for your response

  • do the tutorial

Sign In or Register to comment.