Placing circle across all the screen orderly

RazRaz
edited February 2017 in Questions about Code

Hi, I am working on a game which has a board of circles (4 circles X 5 circles), like here (Please pay attention just to the circles)

Anybody have a an idea for this? Here is my stating code that I need (adding the new code in the TODO comment):

Main Program

Circle[][] circles;
int cols=4, rows=5;

void setup() {
  size(540, 960);

  circles=new Circle[cols][rows];
  for (int x=0; x<cols; x++) {
    for (int y=0; y<rows; y++) {
      // TODO: init new circle 
      //circles[x][y]=new Circle(some x, some y, r);
      circles[x][y].setColorFill(color(255, 0, 100));
    }
  }
}
void draw() {
  background(50, 50, 75);

  for (int x=0; x<cols; x++) {
    for (int y=0; y<rows; y++) {
      circles[x][y].show();
    }
  }

Circle Class

public class Circle {
  private float x, y, r;
  private color fill;
  
  public Circle(float x, float y, float r) {
    this.x=x;
    this.y=y;
    this.r=r;
  }
  
  public void setColorFill(color fill) {
    this.fill=fill;
  }
  
  public void show() {
    noStroke();
    fill(fill);
    ellipse(x, y, r, r);
  }
}
Tagged:

Answers

  • Select the code. Press ctrl + o to indent your code. Leave a line above and below the code.
    And don't put line breaks inside HTML tags.

  • edited February 2017

    @Raz -- I'm not sure I understand your question.

    You have to initialize the circle before you set its fill color. You already have a commented out line (#11) showing how to initialize the circle. The code gives an error, saying that the initialization line is needed. It is right there -- why don't you add that line?

    Was this template and this suggestion provided by someone else, and do you first need to understand how objects are created with the new keyword to get what the line is doing?

    Or is the issue instead that you understand what new does, but you don't know how to map values from your loop variables (x=0,1,2,3) onto calling circles that are drawn at screen locations (0,100,200,300 pixels)...? For example, x*100.

  • @jeremydouglass I know, This code will not run! I know how to initialize the object, but as you said, I don't know how to map the values of each variable which will be depend by the screen. The problem is exactly what you have describe as the last problem

  • edited February 2017

    Well, start out by trying it manually!:

    circles[x][y]=new Circle(x*100,y*100, 10);
    

    You can change values and rurun several times to see what works, or you can use Tweak mode to change those values live, and see what each one does.

    Once you have values that work for the visual effect you want, consider computing those values on their relationship to the sketch -- for example, the width and height of the screen.

  • edited March 2017

    First you will need a square canvas to draw your circle since you have only one r as your radius. Unless if you want to draw ellipse you will need one more argument for another radius.

    But you can still do this

    Main class:

    ...
      int size = (int)(width / cols);
      for (int x=0; x&lt;cols; x++) {
        for (int y=0; y&lt;rows; y++) {
          // TODO: init new circle 
          circles[x][y]=new Circle(x * size,  y * size, size/2);
          circles[x][y].setColorFill(color(255, 0, 100));
        }
      }
    ...
    

    I used width because it was smaller. So I could compact the circles perfectly. Hope it works.

Sign In or Register to comment.