Beginner to programming arrays

Hi all, this is probably a simple question, but I'm stumped.

I'm trying to create an array of placeholder objects (circles) for a project, I need an array of these circles to have variable spacing in relation to their position vertically or horizontally (scale would be an added bonus) I'm not sue if I'm mixing too much together or just making syntax and logic errors. Can anyone explain what I'm doing wrong here?

float [] loc_x;  // X coordinate for Location of center points.
float [] loc_y;  // Y coordinate for Location of centerpoints.
int x = 85;  // Width
int y = 970;  // Height
int h = 16;  // Width of pattern element.
int v = 19;  // Height of pattern element.
float s  = sqrt(2);  // Scaling Factor
float sx = h*s;  // Spacing within array elements (X).
float sy = v*s;  // Spacing between arrays (Y).


void setup() {
  size(x, y);
  strokeWeight(.25);
  fill(220);

  float [] loc_x = new float[i];
  float [] loc_y = new float[j];
  for (int i = 0; i < width; i++) {
    for (int j = 0; j < height; j++) {
      loc_x[i] = i;
      loc_y[j] = j;
    }
  }
}


void draw() {
  background(255);
  for (int i = 0; i < loc_x.length; i++) {
    for (int j = 0; j 
Tagged:

Answers

  • What does this code do? What does it do that's wrong?

  • edited May 2014 Answer ✓

    there were a few minor errors...

    this version is far from finished.

    in your version in line 17-18 you again defined the two arrays. Leave away float[] here and you are referring to the two global arrays as you wanted.

    Also I introduced k as an index for the arrays. The variables i and j are not suited for that purpose.

    float [] loc_x;  // X coordinate for Location of center points.
    float [] loc_y;  // Y coordinate for Location of centerpoints.
    int maxIndex ; 
    
    int x = 485;  // Width
    int y = 970;  // Height
    
    int h = 16;  // Width of pattern element.
    int v = 19;  // Height of pattern element.
    
    float s  = 1; // sqrt(2);  // Scaling Factor
    float sx = h*s;  // Spacing within array elements (X).
    float sy = v*s;  // Spacing between arrays (Y).
    
    
    void setup() {
      size(x, y);
      strokeWeight(.25);
      fill(220);
    
      int k = 0;
    
      loc_x = new float[2000];
      loc_y = new float[2000];
      for (int i = 0; i < width; i+=sx) {
        for (int j = 0; j < height; j+=sy) {
          loc_x[k] = i + 40 ;
          loc_y[k] = j + 30;
          k++;
        }
      }
      maxIndex=k;
      println (maxIndex);
    }
    
    
    void draw() {
      background(255);
      fill(22);
      for (int i = 0; i < maxIndex; i++) {
        // println(i);
        ellipse(  loc_x[i], loc_y[i], h, v ) ;
      } // for
    } // func 
    //    
    
  • Wow, thanks a ton Chrisir, at least I'm not getting errors now.

    Next question is how do I introduce translations to each loop of the array? Have I set up the appropriate variables to enable this?

  • In line 37&38 I add sx & sy - that gives the spacing.

    But they are too small since s is only 1 atm

  • @Chrisir & @GotoLoop:

    Thanks again you guys, I still don't understand entirely, but am definitely grateful for the help. Where might I find other resources to help me understand array programming better?

    @KevinWorkman:

    This is a manual version of what I was attempting to program:

    1111-02_wallpatterns (half-dark)-01

  • Hm. There is one tut for 2D array - you don't have that yet

    Also one tut for oop

    Also in the wiki | technical faq : read from arrays to class...

  • For patterns like that, there's no need for either arrays nor classes. Just double for () loops! *-:)
    Although I think arrays & classes would make the process easier! O:-)

Sign In or Register to comment.