Placement of variables

I was following a course book on processing which wanted me to recreate an image: it wanted me to center a circle in each quarter of a square. It then wanted me to replace all the hard-coded numbers with variables. When I did so before the code, my image became four circles packed into the top left quarter of the square. But when i put it in void draw, it did it fine.

Why did this happen?

int ellipseSize = 50;
int bckgrndClr = 255;
int strokeClr = 0;
int fillClr = 190;
void setup(){

void draw(){
//these are the variables which the question are about
  float quartHeight = (height/4);
  float halfHeight = (height/2);
  float quartWidth = (width/4);
  float halfWidth = (width/2);

  ellipse(quartHeight, quartWidth,ellipseSize,ellipseSize);
  ellipse(halfHeight+quartHeight, halfWidth+quartWidth,ellipseSize,ellipseSize);
  ellipse(quartHeight, halfHeight+quartWidth,ellipseSize,ellipseSize);
  ellipse(halfHeight+quartHeight, quartWidth,ellipseSize,ellipseSize);

At the global scope - outside of any function - you shouldn’t assign variables values based on the values stored in width and height. These values are not properly set up until after the call to size() happens in setup()!

You should declare your values globally, and then assign them a value in setup() (after the call to size()):

int half_width;

void setup(){
  size( 600, 400 );
  half_width = width / 2; // 300;

// ...
1 Like

This is a common issue for ppl starting with processing. I am making a mental note… it should be added here


1 Like