Changing values?

Hi is anybody clever enough to explain to me why this program creates changing values , I realize that it resets the arrays with each draw loop , But why are they different ?

please format code with </> button * homework policy * asking questions

// TUBULAR FORM USING REFORMED ARRAYS ****** NIGEL S WOOLARD

int r = 100;
int r2 = 200;

int np = 12;

int cl = 100;
float angle=radians(360/np);
float anglestep = radians(360/np); // ALSO TRIED WITH PI VALUES

floatPX1 = new float [np];
floatPY1 = new float[np];
floatPZ1 = new float[np];

floatPX2 = new float[np];
floatPY2 = new float[np];
floatPZ2 = new float[np];

void setup() {

size(600, 600, P3D);
}

void draw() {

BACKGROUND();

CREATETUBE();

DRAWTUBE();

}

void CREATETUBE() {

noStroke();

beginShape(TRIANGLE_STRIP);

for (int i=0; i<np; i++) {

float px1 = sin(angle/np)*r;

float py1 = cos(angle/np)*r;

float pz1 = 0;

PX1[i] = px1;
PY1[i] = py1;
PZ1[i] = pz1;

//vertex (px1, py1, pz1);

float px2 = px1;

float py2 = py1;

float pz2  = pz1;



px2 = sin(angle/np)*r2;

py2 = cos(angle/np)*r2;

pz2 = 0+cl;

PX2[i] = px2;
PY2[i] = py2;
PZ2[i] = pz2;

// vertex (px2, py2, pz2);

angle = angle + anglestep;

}

endShape();
}

//****************************************************************************

void DRAWTUBE(){

translate(300,300,0);
beginShape();
for (int i=0;i<np;i++){

    stroke(#FF0000);
    vertex (PX1[i],PY1[i],PZ1[i]);
    stroke(#FFEA00);
    vertex (PX2[i],PY2[i],PZ2[i]);
  }
  
  endShape();}

//*****************************************************************************

void BACKGROUND(){

fill(#FF8000);

}

in my opinion this might be it

I rewrote your Sketch a bit

I think one issue was that you had float px1 = sin(angle/np)*r; here.
I think this should be float px1 = cos(angle)*r; (without “/np”)

I also set angle to 0 in draw()

Chrisir


// TUBULAR FORM USING REFORMED ARRAYS ****** NIGEL S WOOLARD

final int r = 100;
final int r2 = 200;

final int np = 90;

final int cl = 100;
float angle=0; // radians(360/np);
final float anglestep = radians(360/np); // ALSO TRIED WITH PI VALUES

float[]PX1 = new float[np];
float[]PY1 = new float[np];
float[]PZ1 = new float[np];

float[]PX2 = new float[np];
float[]PY2 = new float[np];
float[]PZ2 = new float[np];

void setup() {
  size(600, 600, P3D);
}

void draw() {
  BACKGROUND();

  angle=0; 
  CREATETUBE();
  DRAWTUBE();
}

//--------------------------------------------------------------

void CREATETUBE() {
  for (int i=0; i<np; i++) {

    PX1[i] = cos(angle)*r;
    PY1[i] = sin(angle)*r;
    PZ1[i] = 0;

    PX2[i] = cos(angle)*r2;
    PY2[i] = sin(angle)*r2;
    PZ2[i] = cl;

    angle += anglestep;
  }
}

//****************************************************************************

void DRAWTUBE() {
  translate(300, 300, 0);
  beginShape();
  for (int i=0; i<np; i++) {
    stroke(#FF0000);
    vertex (PX1[i], PY1[i], PZ1[i]);
    stroke(#FFEA00);
    vertex (PX2[i], PY2[i], PZ2[i]);
  }
  endShape();
}

//*****************************************************************************

void BACKGROUND() {
  fill(#FF8000);
}
//

Thanks Chris that’s great !

1 Like