# Creating variation in grid with random(); & image

**URL:** <https://discourse.processing.org/t/creating-variation-in-grid-with-random-image/40135>\
**Category:** Coding Questions\
**Created:** [December 12, 2022, 6:26pm UTC](https://discourse.processing.org/t/creating-variation-in-grid-with-random-image/40135 "2022-12-12T18:26:44Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [December 15, 2022, 4:31pm UTC](https://discourse.processing.org/t/creating-variation-in-grid-with-random-image/40135/13 "2022-12-15T16:31:31Z")

</div>

> [@asymmetric](#):
>
> Are the following instance variables X and Y?
> 
> ```auto
> int borderGap = 60; // zoom in or out 
> int resolution = 10; // grid cell and row size
> 
> ```

No, these are global variables meaning they are accessible everywhere in the code (inside functions, loops… and more generally any blocks).

```processing
int globalVariable = 0;

void example() {
  int localVariableToTheExampleFunction = 1;
  println(globalVariable); // Accessible here
}

example(); // Prints 0
println(localVariableToTheExampleFunction); // Error: not defined in this scope

```

This is the notion of [scope](https://en.wikipedia.org/wiki/Scope_(computer_science)) in computer science.

> [@asymmetric](#):
>
> `int maxHeight = 70; /// Height brightness `

This is a common practice to put global variables at the top of your program so modifying and reading them is easy. In this case, I am just declaring a variable that controls the maximum height that points can have using the `z` coordinate (pointing from you from the window point of view).

You need to understand how object oriented programming works (using classes with the `class`) keyword. See a previous message I did on the subject:

> [@Problems with my mini game](https://discourse.processing.org/t/problems-with-my-mini-game/21949/2):
>
> Hi, Welcome to the community! slight_smile Before trying to solve your problem, you are facing a well known problem where the structure of your code is actually describing [object oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) : So object oriented programming is a way to structure your code by using objects to model the data that your are manipulating. In this special case, your are creating Balls, and a Ball is described as : a position (x and y coordinates on the screen) a speed (described as a vector, xDir and y…

* * *

To summarize, I am using a `Grid` class that describes the grid you want to display. It has a `points` attribute which is a 2D array of [PVector](https://processing.org/reference/PVector.html) (which is itself a class holding up to three coordinates `x`, `y`, `z`).

This is perfect since we want to handle 3D points organized in a grid (rows and columns).

A class has a constructor, it’s a special “method” with some parameters used to initialize the object (in this case the Grid):

```processing
class Grid {
  Grid(int resolution, int borderGap) {
    // Initialize the grid here (mainly point coordinates)
  }
}

```

Try to read the code and understand what each part does before trying to copy paste code at some places. 😉

---

_[View the full topic](https://discourse.processing.org/t/creating-variation-in-grid-with-random-image/40135)._
