Improving sketch used for kinect detection and audio playback. (smt and beads libraries)

edited July 2016 in Kinect

Hey, so this links to a previous post—I have been building this code, and finally have it in a spot where it is usable, but KNOW people out there can help me improve it, whether through speeding it up, cutting it down, making it more efficient or more obvious what is going on.

Here's an outline of the project. A kinect and projector are mounted above the ground pointing down. A screensaver will play until someone enters the sensors space, at which point, they will trigger sounds by entering zones on the sketch. As more people enter, the zones adapt a little to make it more interesting.

Any input anyone has on improving this and making it more manageable would be hugely helpful.

//Set for 6' height from ground 15'6" distance

import vialab.SMT.*;
import beads.*;

//    Grid Line Variables 
int numCols = 8;
int numRows = 7;

//      ZONES

//    Array of Zones
int zonesNum = 28;
int soundsNum = 29;
Zone[] z = new Zone[zonesNum];
Zone[] z2 = new Zone[zonesNum];
Zone[] z3 = new Zone[zonesNum];
Custom custom;
ArrayList<Custom> list;
//    Zone Grid Variables 
int x, y, w, h;
int[] colX = new int[numCols];
int[] rowY = new int[numRows];
int[] colW = new int[numCols];
int[] rowH = new int[numRows];
int[] colX2 = new int[numCols];
int[] rowY2 = new int[numRows];
int[] colW2 = new int[numCols];
int[] rowH2 = new int[numRows];
int[] colX3 = new int[numCols];
int[] rowY3 = new int[numRows];
int[] colW3 = new int[numCols];
int[] rowH3 = new int[numRows];
//    Zone Booleans
boolean [] isTouched = new boolean[zonesNum];
boolean [] canPlay = new boolean[zonesNum];
boolean [] play = new boolean[zonesNum];


//     ZONES AUDIO

//    Array of Sounds
AudioContext ac;
//    Audio Files
int numSamples = 0;
int sampleWith = 0;
String [] sourceFile;
Gain [] gain;
Glide [] gainAmt;
SamplePlayer [] samplePlayer;
//    Playback Variables
int [] playDelay = new int[zonesNum];
//int playDelay = 0;
int waitTime = 10;
int count = 0;

//      DISPLAY

//    Setup SMT Display INFO
boolean window_fullscreen = false;
int window_width = 1200;
int window_height = 800;
int window_halfWidth;
int window_halfHeight;
int fps_limit = 60;
//    Defines for dialing in
boolean staticState = false;

//    Screensaver Variables
float topStrokeX, botStrokeX, gridX, gridY;
int alpha;
int r;
int unitSize = 50;
PVector[] unit = new PVector[56];
int xUnit = int (width/numCols);
int yUnit = int (height/numRows);
//    To Define if people are in space
int people;
boolean peopleIn = false;
//    Playhead for screensaver
int playX;
int playY;
int grow;
//    Controlling Playhead Speed 
float rate = 5;
int passCount=1;

//    Removes frame from window
//void init()
//{
//  frame.removeNotify();
//  frame.setUndecorated(true);
//  frame.addNotify();
//  super.init();
//}

void setup() {

  //  //    Sets Window to Location
  //  frame.setLocation(0, 0);

  smtSetup();
  screenSaverSetup();
  audioSetup();

  functionGrids();
  dialInGrid();
  function1();
  function2();


  ac.start();
}

void draw() { 
  background(0);
  fill(30);

  peopleOn();
  staticStates ();

  if (staticState) {
    textDisplay();
  }
  delay();



  //Switch off SMT cursors
  //  SMT.setTouchDraw(TouchDraw.NONE);
}

//        Private class loads variables for each zone from functions tab depending on situation (num of people in space)
private class Custom extends Zone {

  public Custom(String name, int x, int y, int width, int height, String audio) {
    super(name, x, y, width, height, audio);
    this.setCaptureTouches( false);
    this.name = name;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.name = audio;
  }

  //  Zone Draw method
  public void draw() {
    int number = this.name.length();
    int numTouches = getNumTouches();

    stroke(255);
    strokeWeight(2);

    //    Creates different color fill if touch occurs in zone
    if (numTouches==0) {
      fill( #F0E15E);
      rect( 0, 0, this.getWidth(), this.getHeight());
    }
    if (numTouches>0) {
      fill( #5AC16A);
      rect( 0, 0, this.getWidth(), this.getHeight());
    }

    //      Takes length of zone name and parses numeric value (i) to stop audio track relatied to zone once touch ends.
    if ( number == 6 ) {
      String element = this.name;
      int x = Character.getNumericValue(element.charAt(5));
      int audio = x;
      if (numTouches==0) {
        isTouched[audio] = false;
      }
    }
    if ( number == 7 ) {
      String element = this.name;
      int x = Character.getNumericValue(element.charAt(5));
      //      System.out.println("x=" + x);
      int y = Character.getNumericValue(element.charAt(6));
      //      System.out.println("y=" + y );
      int audio = (x*10)+y;
      if (numTouches==0) {
        isTouched[audio] = false;
      }
    }
  }

  //  Zone Touch method
  public void touch() {

    int number = this.name.length();

    //      Takes length of zone name and parses numeric value (i) to play audio track relatied to zone.
    if ( number == 6 ) {
      String element = this.name;
      int x = Character.getNumericValue(element.charAt(5));
      int audio = x;

      if (playDelay[audio] >= waitTime) {
        samplePlayer[audio].start();
        samplePlayer[audio].setToLoopStart();
        isTouched[audio] = true;
        playDelay[audio]=0;
      }
    }
    if ( number == 7 ) {
      String element = this.name;
      int x = Character.getNumericValue(element.charAt(5));
      int y = Character.getNumericValue(element.charAt(6));
      int audio = (x*10)+y;

      if (playDelay[audio] >= waitTime) {
        samplePlayer[audio].start();
        samplePlayer[audio].setToLoopStart();
        isTouched[audio] = true;
        playDelay[audio]=0;
      }
    }
  }
  public void press( Touch touch) {
  }
}

void audioSetup() {

  //Audio Setup
  ac = new AudioContext(); 
  File folder = new File(sketchPath("") + "samples");
  File [] fileList = folder.listFiles();
  for (int i = 0; i < fileList.length; i ++) {
    if (fileList[i].isFile()) {
      if (fileList[i].getName().endsWith(".mp3")) {
        numSamples ++;
      }
    }
  }
  if (numSamples <= 0) {
    println("No samples found in " + sketchPath("") + "samples/");
    println("Exiting...");
    exit();
  }
  sampleWith = 20;
  sourceFile = new String[numSamples];
  int count = 0;
  for (int i = 0; i < fileList.length; i ++) {
    if (fileList[i].isFile()) {
      if (fileList[i].getName().endsWith(".mp3")) {
        sourceFile[count] = fileList[i].getName();
        count ++;
      }
    }
  }
  gain = new Gain[numSamples];
  gainAmt  = new Glide[numSamples];
  samplePlayer = new SamplePlayer[numSamples];
  try {
    for (count = 0; count < numSamples; count ++) {
      println("loading" + sketchPath("") + "samples/" + sourceFile[count]);
      samplePlayer[count] = new SamplePlayer(ac, new Sample(sketchPath("") + "samples/" + sourceFile[count]));
      samplePlayer[count].setKillOnEnd(false);
      gainAmt[count] = new Glide(ac, 0.0);
      gain[count] = new Gain(ac, 2, 0.2);
      gain[count].addInput(samplePlayer[count]);
      ac.out.addInput(gain[count]);
    }
  }
  catch (Exception e) {
    println("Error loading samples");
    e.printStackTrace();
    exit();
  }
}

void delay () {
  for (int i=0; i< zonesNum; i++) {
    if (!isTouched[i]) {
      playDelay[i]++;
    }
  }
}

void functionGrids() {

  // for dial in grid.

  for (int i=0; i<numCols; i++) {
    colW[0] = colW[7] = (int)(width*0.03); 
    colW[1] = colW[6] = (int)(width*0.07); 
    colW[2] = colW[5] = (int)(width*0.10); 
    colW[3] = colW[4] = (int)(width*0.3);
  }
  for (int j=0; j<numCols; j++) {
    colX[0] = 0;
    colX[1] = colW[0];
    colX[2] = colW[0]+colW[1];
    colX[3] = colW[0]+colW[1]+colW[2];
    colX[4] = colW[0]+colW[1]+colW[2]+colW[3];
    colX[5] = colW[0]+colW[1]+colW[2]+colW[3]+colW[4];
    colX[6] = colW[0]+colW[1]+colW[2]+colW[3]+colW[4]+colW[5];
    colX[7] = colW[0]+colW[1]+colW[2]+colW[3]+colW[4]+colW[5]+colW[6];
  }

  for (int i=0; i<numRows; i++) {
    rowH[0] = rowH[6] = (int)(height*0.05);
    rowH[1] = rowH[5] = (int)(height*0.10);
    rowH[2] = rowH[4] = (int)(height*0.15);
    rowH[3] = (int)(height*0.4);
  }
  for (int j=0; j<numRows; j++) {
    rowY[0] = 0;
    rowY[1] = rowH[0];
    rowY[2] = rowH[0]+rowH[1];
    rowY[3] = rowH[0]+rowH[1]+rowH[2];
    rowY[4] = rowH[0]+rowH[1]+rowH[2]+rowH[3];
    rowY[5] = rowH[0]+rowH[1]+rowH[2]+rowH[3]+rowH[4];
    rowY[6] = rowH[0]+rowH[1]+rowH[2]+rowH[3]+rowH[4]+rowH[5];
  }

  //  for fuction1 grid

  for (int i=0; i<numCols; i++) {
    colW2[i] = height/numCols;
  }
  for (int j=0; j<numCols; j++) {
    colX2[0] = (width-height)/2;
    colX2[1] = colX2[0]+colW2[0];
    colX2[2] = colX2[0]+colW2[0]+colW2[1];
    colX2[3] = colX2[0]+colW2[0]+colW2[1]+colW2[2];
    colX2[4] = colX2[0]+colW2[0]+colW2[1]+colW2[2]+colW2[3];
    colX2[5] = colX2[0]+colW2[0]+colW2[1]+colW2[2]+colW2[3]+colW2[4];
    colX2[6] = colX2[0]+colW2[0]+colW2[1]+colW2[2]+colW2[3]+colW2[4]+colW2[5];
    colX2[7] = colX2[0]+colW2[0]+colW2[1]+colW2[2]+colW2[3]+colW2[4]+colW2[5]+colW2[6];
  }

  for (int i=0; i<numRows; i++) {
    rowH2[i] = height/numCols;
  }
  for (int j=0; j<numRows; j++) {
    rowY2[0] = rowH2[0]/2;
    rowY2[1] = rowH2[0]/2+rowH2[0];
    rowY2[2] = rowH2[0]/2+rowH2[0]+rowH2[1];
    rowY2[3] = rowH2[0]/2+rowH2[0]+rowH2[1]+rowH2[2];
    rowY2[4] = rowH2[0]/2+rowH2[0]+rowH2[1]+rowH2[2]+rowH2[3];
    rowY2[5] = rowH2[0]/2+rowH2[0]+rowH2[1]+rowH2[2]+rowH2[3]+rowH2[4];
    rowY2[6] = rowH2[0]/2+rowH2[0]+rowH2[1]+rowH2[2]+rowH2[3]+rowH2[4]+rowH2[5];
  }

  //  for function2 grid.

  for (int i=0; i<numCols; i++) {
    colW3[i] = height/numCols;
  }
  for (int j=0; j<numCols; j++) {
    //    colX2[j] =+ colX2[j];
    colX3[0] = (width-height)/2;
    colX3[1] = colX3[0]+colW3[0];
    colX3[2] = colX3[0]+colW3[0]+colW3[1];
    colX3[3] = colX3[0]+colW3[0]+colW3[1]+colW3[2];
    colX3[4] = colX3[0]+colW3[0]+colW3[1]+colW3[2]+colW3[3];
    colX3[5] = colX3[0]+colW3[0]+colW3[1]+colW3[2]+colW3[3]+colW3[4];
    colX3[6] = colX3[0]+colW3[0]+colW3[1]+colW3[2]+colW3[3]+colW3[4]+colW3[5];
    colX3[7] = colX3[0]+colW3[0]+colW3[1]+colW3[2]+colW3[3]+colW3[4]+colW3[5]+colW3[6];
  }
  for (int i=0; i<numRows; i++) {
    rowH3[i] = height/numCols;
  }
  for (int j=0; j<numRows; j++) {
    rowY3[0] = rowH3[0]/2;
    rowY3[1] = rowH3[0]/2+rowH3[0];
    rowY3[2] = rowH3[0]/2+rowH3[0]+rowH3[1];
    rowY3[3] = rowH3[0]/2+rowH3[0]+rowH3[1]+rowH3[2];
    rowY3[4] = rowH3[0]/2+rowH3[0]+rowH3[1]+rowH3[2]+rowH3[3];
    rowY3[5] = rowH3[0]/2+rowH3[0]+rowH3[1]+rowH3[2]+rowH3[3]+rowH3[4];
    rowY3[6] = rowH3[0]/2+rowH3[0]+rowH3[1]+rowH3[2]+rowH3[3]+rowH3[4]+rowH3[5];
  }
}

void dialInGrid() {

  for (int i=0; i< zonesNum; i++) {
    for (int j = 0; j < numRows; j++) {
      if ((i>=0)&&(i<4)) {
        y = rowY[0];
        h = rowH[0];
      } 
      if ((i==4)||(i==5)||(i==6)||(i==7)) {
        y = rowY[1];
        h = rowH[1];
      }
      if ((i>=8)&&(i<=11)) {
        y = rowY[2];
        h = rowH[2];
      }
      if ((i>=12)&&(i<=15)) {
        y = rowY[3];
        h = rowH[3];
      }
      if ((i>=16)&&(i<=19)) {
        y = rowY[4];
        h = rowH[4];
      }
      if ((i>=20)&&(i<=23)) {
        y = rowY[5];
        h = rowH[5];
      }
      if ((i>=24)&&(i<=27)) {
        y = rowY[6];
        h = rowH[6];
      }
    }
    for (int k = 0; k < numCols; k++) {

      if ((i==0)||(i==8)||(i==16)||(i==24)) {
        x = colX[0];
        w = colW[0];
      }
      if ((i==1)||(i==9)||(i==17)||(i==25)) {
        x = colX[2];
        w = colW[2];
      }
      if ((i==2)||(i==10)||(i==18)||(i==26)) {
        x = colX[4];
        w = colW[4];
      }
      if ((i==3)||(i==11)||(i==19)||(i==27)) {
        x = colX[6];
        w = colW[6];
      }
      if ((i==4)||(i==12)||(i==20)) {
        x = colX[1];
        w = colW[1];
      }
      if ((i==5)||(i==13)||(i==21)) {
        x = colX[3];
        w = colW[3];
      }
      if ((i==6)||(i==14)||(i==22)) {
        x = colX[5];
        w = colW[5];
      }
      if ((i==7)||(i==15)||(i==23)) {
        x = colX[7];
        w = colW[7];
      }
    }
    z[i] = new Custom("TouchZone"+i, x, y, w, h, "audio"+i);

    SMT.add(z[i]);
  }
}

void function1() {

  for (int i=0; i< zonesNum; i++) {
    for (int j = 0; j < numRows; j++) {
      if ((i>=0)&&(i<8)) {
        y = rowY2[2];
        h = rowH2[0];
      }
      if ((i>=8)&&(i<16)) {
        y = rowY2[4];
        h = rowH2[0];
      }
      if (i>=16) {
        y = -200;
        h = 0;
      }
    }
    for (int k = 0; k < numCols; k++) {
      if ((i==0)||(i==8)) {
        x = colX2[0];
        w = colW2[k];
      }
      if ((i==1)||(i==9)) {
        x = colX2[1];
        w = colW2[k];
      }
      if ((i==2)||(i==10)) {
        x = colX2[2];
        w = colW2[k];
      }
      if ((i==3)||(i==11)) {
        x = colX2[3];
        w = colW2[k];
      }
      if ((i==4)||(i==12)) {
        x = colX2[4];
        w = colW2[k];
      }
      if ((i==5)||(i==13)) {
        x = colX2[5];
        w = colW2[k];
      }
      if ((i==6)||(i==14)) {
        x = colX2[6];
        w = colW2[k];
      }
      if ((i==7)||(i==15)) {
        x = colX2[7];
        w = colW2[k];
      }
      if (i>15) {
        x = -200;
        w = 0;
      }
    }

    z2[i] = new Custom("TouchZone"+i, x, y, w, h, "audio"+i);

    SMT.add(z2[i]);
  }
}

void function2() {

  for (int i=0; i< zonesNum; i++) {
    for (int j = 0; j < numRows; j++) {
      if ((i>=0)&&(i<4)) {
        y = rowY3[0];
      }
      if ((i>=4)&&(i<8)) {
        y = rowY3[1];
      }
      if ((i>=8)&&(i<12)) {
        y = rowY3[2];
      }
      if ((i>=12)&&(i<16)) {
        y = rowY3[3];
      }
      if ((i>=16)&&(i<20)) {
        y = rowY3[4];
      }
      if ((i>=20)&&(i<24)) {
        y = rowY3[5];
      }
      if ((i>=24)&&(i<28)) {
        y = rowY3[6];
      }
    }
    for (int k = 0; k < numCols; k++) {
      if ((i==0)||(i==8)||(i==16)||(i==24)) {
        x = colX3[0];
      }
      if ((i==1)||(i==9)||(i==17)||(i==25)) {
        x = colX3[1];
      }
      if ((i==2)||(i==10)||(i==18)||(i==26)) {
        x = colX3[2];
      }
      if ((i==3)||(i==11)||(i==19)||(i==27)) {
        x = colX3[3];
      }
      if ((i==4)||(i==12)||(i==20)) {
        x = colX3[4];
      }
      if ((i==5)||(i==13)||(i==21)) {
        x = colX3[5];
      }
      if ((i==6)||(i==14)||(i==22)) {
        x = colX3[6];
      }
      if ((i==7)||(i==15)||(i==23)) {
        x = colX3[7];
      }
      w = rowH3[0];
      h = rowH3[0];
    }

    z3[i] = new Custom("TouchZone"+i, x, y, w, h, "audio"+i);

    SMT.add(z3[i]);
  }
}


//      plays tuning up audio
void keyReleased() {
  if (key == 'a') {
    samplePlayer[28].start();
    samplePlayer[28].setToLoopStart();
  } else {
    background(0);
  }
}

//      sets sketch in degbugging mode
void staticStates () {
  if (keyPressed) {

    //    to get to dial-in
    if (key == 'm') {
      staticState=true;
      peopleIn = true;

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

        z[i].setTouchable(true);
        z[i].setVisible( true );
        SMT.add(z[i]);
        SMT.remove(z2[i]);
        SMT.remove(z3[i]);
      }
    }
    if (key == 'n') {
      staticState=true;
      peopleIn = true;

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

        z2[i].setTouchable(true);
        z2[i].setVisible( true );
        SMT.add(z2[i]);
        SMT.remove(z3[i]);
        SMT.remove(z[i]);
      }
    }
    if (key == 'b') {
      staticState=true;
      peopleIn = true;

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

        z3[i].setTouchable(true);
        z3[i].setVisible( true );
        SMT.add(z3[i]);
        SMT.remove(z[i]);
        SMT.remove(z2[i]);
      }
    }
    if (key == 'p') {
      staticState=false;
      for (int i=0; i< zonesNum; i++) {
        SMT.add(z2[i]);
        SMT.add(z3[i]);
      }
    }
  }
}

more to follow in separate post...

Tagged:

Answers

  • void peopleOn () {
      if (!staticState) {
    
        people = SMT.getTouchCount();
    
        if (people==0) {
          peopleIn=false;
    
          for (int i=0; i< zonesNum; i++) {
            z[i].setVisible( false );
            z2[i].setVisible( false );
            z3[i].setVisible( false );
          }
        } else if ((people>0)&&(people<=3)) {
          peopleIn = true;
          for (int i=0; i< zonesNum; i++) {
            SMT.putZoneOnTop(z2[i]);
            z2[i].setTouchable(true);
            z2[i].setVisible( true );
            z3[i].setTouchable(false);
            z3[i].setVisible( false );
            z[i].setTouchable(false);
            z[i].setVisible( false );
          }
        } else if (people>3) {
          peopleIn = true;
          for (int i=0; i< zonesNum; i++) {
    
            SMT.putZoneOnTop(z3[i]);
            z3[i].setTouchable(true);
            z3[i].setVisible( true );
            z2[i].setTouchable(false);
            z2[i].setVisible( false );
            z[i].setTouchable(false);
            z[i].setVisible( false );
          }
        }
        if (!peopleIn) {
          screenSaver();
        }
      }
    }
    
    void textDisplay () {
      fill(255);
      for (int i=0; i< zonesNum; i++) {
        text("Play Delay "+i +playDelay[i], width-100, i*20);
      }
      text("People: "+people, width-100, height-40);
    }
    
    void screenSaverSetup() {
      for (int i=0; i<56; i++) {
        unit[i] = new PVector();
        if (i<numCols) {
          unit[i].x = i*unitSize;
        } else {
          unit[i].x = (i%numCols)*unitSize;
        }
        if (i<numRows) {
          unit[i].y = 0;
        } else {
          unit[i].y = (i%numRows)*unitSize;
        }
      }
      r = int(random(56));
      gridX=unit[r].x;
      gridY=unit[r].y;
      topStrokeX=gridX;
      botStrokeX=gridX+unitSize;
    }
    
    void screenSaver () {
      stroke(255);
      float gridW = gridX+unitSize;
      float gridH = gridY+unitSize;
    
      if ((topStrokeX < gridW) && (botStrokeX > gridX)) {
        line(gridX, gridY, topStrokeX, gridY);
        line(botStrokeX, gridH, gridW, gridH);    
        topStrokeX++;
        botStrokeX--;
      } 
      if ((topStrokeX >= gridW) && (botStrokeX <= gridX)) {
        fill(255, alpha);
        rect(gridX, gridY, unitSize, unitSize);
        alpha+=5;
        if (alpha >=400) { //GOES HIGHER THAN 255 to HOLD FULL ALPHA FOR A FEW SECONDS
          alpha=0;
          r = int(random(56));
          gridX=unit[r].x;
          gridY=unit[r].y;
          topStrokeX=gridX;
          botStrokeX=gridX+unitSize;
        }
      }
    }
    
    void smtSetup() {
    
      //Display setup
      if ( window_fullscreen) {
        window_width = displayWidth;
        window_height = displayHeight;
      }
      window_halfWidth = window_width / 2;
      window_halfHeight = window_height / 2;
      //processing window setup
      frameRate(fps_limit);
    //  size( window_width, window_height, SMT.RENDERER);
    
      size( 400, 350, SMT.RENDERER);
    
      //  size( 1000, 700, SMT.RENDERER);
      SMT.init( this, TouchSource.AUTOMATIC);
    
      //Aspect Ratio Variables
      numCols = 8;
      numRows = 7;
    
      list = new ArrayList<Custom>();
    
    }
    
  • p.s. I know my coding is somewhat primitive, but I wanted to post to help future people and also learn, so if you can help, that would be great.

  • but KNOW people out there can help me improve it

    Unfortunately, your use of the smt library will limit the responses. Very few people will have that installed and working and without being able to run the code the rest of us will just be guessing.

  • Answer ✓

    there is some obvious flab though. this in function2, line 517 onwards

    for (int j = 0; j < numRows; j++) {
      if ((i>=0)&&(i<4)) {
        y = rowY3[0];
      }
      if ((i>=4)&&(i<8)) {
        y = rowY3[1];
      }
      if ((i>=8)&&(i<12)) {
        y = rowY3[2];
      }
      if ((i>=12)&&(i<16)) {
        y = rowY3[3];
      }
      if ((i>=16)&&(i<20)) {
        y = rowY3[4];
      }
      if ((i>=20)&&(i<24)) {
        y = rowY3[5];
      }
      if ((i>=24)&&(i<28)) {
        y = rowY3[6];
      }
    }
    

    the loop doesn't use j so it doesn't need to be a loop.

    also, because of the regularity of the numbers, the whole thing can be reduced to

    y = rowY2[i / 4];
    
  • Answer ✓

    the loop following that one, line 539 onwards, also doesn't use k, so doesn't need to be a loop.

    x = colX3[i % 8]; // modulo operator...
    

    there are others...

  • Hey koogs—thats great, I need to learn some of those tricks to make everything a little more readable and easy, thanks.

    Its a shame about SMT. Are there any other sites you know of which might be more helpful to post at?

  • Here's a questions for you, this should be pretty simple right:

        for (int j=0; j<numRows; j++) {
            rowY[0] = 0;
            rowY[1] = rowH[0];
            rowY[2] = rowH[0]+rowH[1];
            rowY[3] = rowH[0]+rowH[1]+rowH[2];
            rowY[4] = rowH[0]+rowH[1]+rowH[2]+rowH[3];
            rowY[5] = rowH[0]+rowH[1]+rowH[2]+rowH[3]+rowH[4];
            rowY[6] = rowH[0]+rowH[1]+rowH[2]+rowH[3]+rowH[4]+rowH[5];
          }
    

    Can't this just be something like...

          for (int j=0; j<numRows; j++) {
            rowY[0] = 0;
            if (j<0) {
              rowY[j] += (rowH[0]+rowH[j-1]);
            }
          }
    

    Why doesn't this work in the same/similar way—or is there a way of achieving the same thing in the first block in a more efficient manner?

  • well, in that first loop you aren't using j at all. so you are calculating rowY[0] to rowY[7] as exactly the same thing. and you're doing that numRows times. which is pointless.

    if you remove the loop, leaving

    rowY[0] = 0;
    rowY[1] = rowH[0];
    rowY[2] = rowH[0]+rowH[1];
    rowY[3] = rowH[0]+rowH[1]+rowH[2];
    rowY[4] = rowH[0]+rowH[1]+rowH[2]+rowH[3];
    rowY[5] = rowH[0]+rowH[1]+rowH[2]+rowH[3]+rowH[4];
    rowY[6] = rowH[0]+rowH[1]+rowH[2]+rowH[3]+rowH[4]+rowH[5];
    

    then rowY[2] is rowY[1] + rowH[1], rowY[3] = rowY[2] + rowH[2]...

    rowY[0] = 0;
    rowY[1] = rowH[1];
    for (int i = 2 ; i <= 6 ; i++) {
      rowY[i] = rowY[i - 1] + rowH[i - 1];
    }
    
  • Genius!—or common sense! Thank you Koogs, I'm slowly learning and understanding more.

Sign In or Register to comment.