# Creating a grid of dice representing an image

**URL:** <https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364>\
**Category:** Coding Questions\
**Created:** [August 15, 2022, 4:41pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364 "2022-08-15T16:41:22Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 8:40pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/41 "2022-10-31T20:40:34Z")

</div>

> [@asymmetric](#):
>
> ```auto
> int dots = s;
> 
> ```

This belongs in the show function, no?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 8:44pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/42 "2022-10-31T20:44:08Z")

</div>

I removed the following variable from the constructor…

> [@asymmetric](#):
>
> ` int dots = s;`

I’m now recieving the same error message for the same variable in the show function now.

```auto
void show(float myX, float myY, float dots )
  {
    fill(255,255,255);
    dots = s;
    rect(myX, myY, 50, 50);
    fill(0, 0, 0);

```

> [@asymmetric](#):
>
> “s cannot be resolved to a variable”

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 8:53pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/43 "2022-10-31T20:53:02Z")

</div>

> [@asymmetric](#):
>
> `void show(float myX, float myY, float dots )`

void show(float myX, float myY, int s )

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:11pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/44 "2022-10-31T21:11:32Z")

</div>

```auto
      die.show(x * tileW, y * tileH, s); //Class

```

“The method show(float,float,int) in the type image\_rasterization. Die is not applicable for the arguments (float,float,float).”

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:13pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/45 "2022-10-31T21:13:53Z")

</div>

- List item

The function “show()” expects parametesrs like "show(float,float,int)  
The value of the local variable “myX” is not used.  
The value of the local variable “myY” is not used.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 9:14pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/46 "2022-10-31T21:14:05Z")

</div>

> [@asymmetric](#):
>
> ` float s = map(b, 0, 255, 0, 7);`

Do you still have a line like this in draw()?

What about

int s = int(map(…

And then like above?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:15pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/47 "2022-10-31T21:15:23Z")

</div>

I am still receiving the same errors.

```auto
float s = int(map(b, 0, 255, 0, 7)); // 1-6 dice sides
      Die die = new Die(x*tileW, y*tileH );
      
      float sc = tileW/50; //50 is size of die face
      //println(sc, 1/sc);

      push();
      scale(sc, sc);
      //int s = int(random(0, 7)); // This will give 0 to 6! Use print() to see this before and after int()
      die.show(x * tileW, y * tileH, s); //Class
      pop();

```

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:17pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/48 "2022-10-31T21:17:21Z")

</div>

It worked but the compiler still has the following errros

The value of the local variable “myX” is not used.  
The value of the local variable “myY” is not used.

```auto
float s = int(map(b, 0, 255, 0, 7)); // 1-6 dice sides
      Die die = new Die(x*tileW, y*tileH );
      
      float sc = tileW/50; //50 is size of die face
      //println(sc, 1/sc);

      push();
      scale(sc, sc);
      //int s = int(random(0, 7)); // This will give 0 to 6! Use print() to see this before and after int()
      die.show(x * tileW, y * tileH, int (s)); //Class
      pop();
      }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 9:19pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/49 "2022-10-31T21:19:05Z")

</div>

> [@asymmetric](#):
>
> The value of the local variable “myX” is not used.  
> The value of the local variable “myY” is not used.

Just a warning. Ignore it.

Does it look cool?

Do you realize that you only analyze the 80 x 80 pixels at the top of your image?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:20pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/50 "2022-10-31T21:20:21Z")

</div>

![Screen Shot 2022-10-31 at 2.17.59 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/e/fec159be2e5130042368cdc215037ce48cebe984.png)

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:21pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/51 "2022-10-31T21:21:05Z")

</div>

> Blockquote

Yes, lol! How do I change this?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 9:22pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/52 "2022-10-31T21:22:31Z")

</div>

Doesn’t look good…

check the numbers…

As I said: 80x80…

Maybe resize your image to 80x80 too?

But the dices should fill the entire window

Now you can work with it!

Really try to understand the code and improve it!!

Congratulations!!!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 9:23pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/53 "2022-10-31T21:23:48Z")

</div>

The for loop has the numbers that are used to analyze the image ; using get. Change the for loop.

Really try to understand everything

Guessing won’t help you in the long run

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:28pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/54 "2022-10-31T21:28:13Z")

</div>

> The for loop has the numbers that are used to analyze the image ; using get. Change the for loop.

How should I change the for loop?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:29pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/55 "2022-10-31T21:29:36Z")

</div>

![Screen Shot 2022-10-31 at 2.29.12 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/6/767f606693392077aa15c7227e385b621ef19576.png)  
LOL

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 9:35pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/56 "2022-10-31T21:35:27Z")

</div>

Do you understand the principle what we are doing?

The image has a size set by resize in setup.

The for loop loops over each pixel, it therefore must have same upper boundary as width and height of image.

Then we enlarge the image using dice. Here  
we say +tileW and \* tileH. The resulting grid of dices should not exceed your window / screen?

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:38pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/57 "2022-10-31T21:38:55Z")

</div>

The dice are now fitting within the window screen, but the dice do not reflect the pixel value.

 ![Screen Shot 2022-10-31 at 2.37.46 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/9/19901c790137c1afa50c63b11a59d0a084316449.png)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 9:44pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/58 "2022-10-31T21:44:21Z")

</div>

Post your entire code every time

Do the 2 for loops cover the entire image now??

Why does the map statement not result in 1…6 but in 0…7???

But good progress here! We see something!!!

---

<div class="post-metadata">

**Author:** ![asymmetric](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/asymmetric/32/16577_2.png) [@asymmetric](https://discourse.processing.org/u/asymmetric)\
**Post date:** [October 31, 2022, 9:44pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/59 "2022-10-31T21:44:53Z")

</div>

```auto
PImage img;

void setup(){
  size(800,800);
  img = loadImage("IMG_7741.jpeg");
  img.resize(800,800);
}

void draw(){
  background(#f1f1f1);
  //noSrtoke();
  
  float tilesX = 15;
  float tilesY = tilesX;
  
  float tileW = width / tilesX;
  float tileH = height/ tilesY;
 
  for (int x = 0; x < tilesX; x++){
    for (int y = 0; y < tilesY; y++){
     
      int px = int(x);
      int py = int(y);
      color c = img.get(px,py);
      
      fill(c);
      
      float b = brightness(c);

      float s = int(map(b, 0, 255, 0, 7)); // 1-6 dice sides
      Die die = new Die(x*tileW, y*tileH );
      
      float sc = tileW/50; //50 is size of die face
      push();
      //scale(sc, sc);
      die.show(x * tileW, y * tileH, int (s)); //Class
      pop();
      }
   }
}
class Die
{
  //variable declarations here
  int dots;
  float myX;
  float myY;

  Die(float x, float y) //constructor
  {
    float myX = x;
    float myY= y;
  }

  void show(float myX, float myY, int s )
  {
    fill(255,255,255);
    rect(myX, myY, 50, 50);
    fill(0, 0, 0);
    dots = s;

    if (dots == 1)
    {
      ellipse(myX+25, myY+25, 10, 10);
    } else if (dots == 2)
    {
      ellipse(myX+10, myY+40, 10, 10); 
      ellipse(myX+40, myY+10, 10, 10);
    } else if (dots == 3)
    { 
      ellipse(myX+10, myY+40, 10, 10); 
      ellipse(myX+25, myY+25, 10, 10); 
      ellipse(myX+40, myY+10, 10, 10);
    } else if (dots == 4)
    { 
      ellipse(myX+10, myY+40, 10, 10); 
      ellipse(myX+10, myY+10, 10, 10); 
      ellipse(myX+40, myY+40, 10, 10); 
      ellipse(myX+40, myY+10, 10, 10);
    } else if (dots == 5)
    { 
      ellipse(myX+10, myY+40, 10, 10); 
      ellipse(myX+10, myY+10, 10, 10); 
      ellipse(myX+40, myY+40, 10, 10); 
      ellipse(myX+40, myY+10, 10, 10); 
      ellipse(myX+25, myY+25, 10, 10);
    } else if (dots == 6)
    { 
      ellipse(myX+10, myY+40, 10, 10); 
      ellipse(myX+10, myY+25, 10, 10); 
      ellipse(myX+10, myY+10, 10, 10); 
      ellipse(myX+40, myY+10, 10, 10); 
      ellipse(myX+40, myY+25, 10, 10); 
      ellipse(myX+40, myY+40, 10, 10);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2022, 9:46pm UTC](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364/60 "2022-10-31T21:46:32Z")

</div>

???

The image is 800 and the for loop goes up to 15…  
Bad

Probably you also want smaller dices!!!

[Previous page](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364.md?page=2)

[Next page](https://discourse.processing.org/t/creating-a-grid-of-dice-representing-an-image/38364.md?page=4)
