# how to draw rectangles from this list

**URL:** <https://community.wolfram.com/t/how-to-draw-rectangles-from-this-list/2554>\
**Category:** Questions\
**Tags:** fun-and-play, graphics-and-visualization\
**Created:** [July 5, 2014, 12:34am UTC](https://community.wolfram.com/t/how-to-draw-rectangles-from-this-list/2554 "2014-07-05T00:34:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![22padrinos](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/22padrinos/32/29645_2.png) [@22padrinos](https://community.wolfram.com/u/22padrinos)\
**Post date:** [July 5, 2014, 12:34am UTC](https://community.wolfram.com/t/how-to-draw-rectangles-from-this-list/2554/1 "2014-07-05T00:34:59Z")

</div>

Hello community, I am writing to ask for your help to do the following

I have this list

```
pts= {{9, 1}, {9, 1}, {7, 2}, {8, 2}, {7, 3}, {7, 3}, {7, 4}, {8,
   4}, {9, 5}, {6, 5}, {6, 6}, {5, 3}, {5, 6}, {5, 6}, {6, 7}, {5, 
  7}, {5, 7}, {5, 8}, {4, 2}, {4, 4}, {4, 5}, {4, 8}, {3, 6}, {3, 
  7}, {3, 8}, {3, 8}};

```

and would like to draw rectangles of width equal to 1 and large the first element of each ordered pair, so

 ![5890Pantallazo.png](https://global.discourse-cdn.com/wolfram/original/2X/4/43304c93867c6f90ed931d0f3089cb48c39562fa.png)

as will be able to see each rectangle has 19 large by 1 wide, must accommodate only the rectangles that have in common the second element of each ordered pair  
I hope you can help me, I am currently suffering too much about this, Greetings and happy Friday.

---

<div class="post-metadata">

**Author:** ![dreiss](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dreiss/32/29094_2.png) [@dreiss](https://community.wolfram.com/u/dreiss)\
**Post date:** [July 5, 2014, 1:54am UTC](https://community.wolfram.com/t/how-to-draw-rectangles-from-this-list/2554/2 "2014-07-05T01:54:15Z")

</div>

Here is a start

```
In[1]:= points = {{9, 1}, {9, 1}, {7, 2}, {8, 2}, {7, 3}, {7, 3}, {7, 
    4}, {8, 4}, {9, 5}, {6, 5}, {6, 6}, {5, 3}, {5, 6}, {5, 6}, {6, 
    7}, {5, 7}, {5, 7}, {5, 8}, {4, 2}, {4, 4}, {4, 5}, {4, 8}, {3, 
    6}, {3, 7}, {3, 8}, {3, 8}};

In[2]:= pointLists = GatherBy[pts, Last]

Out[2]= {{{9, 1}, {9, 1}}, {{7, 2}, {8, 2}, {4, 2}}, {{7, 3}, {7, 
   3}, {5, 3}}, {{7, 4}, {8, 4}, {4, 4}}, {{9, 5}, {6, 5}, {4, 
   5}}, {{6, 6}, {5, 6}, {5, 6}, {3, 6}}, {{6, 7}, {5, 7}, {5, 7}, {3,
    7}}, {{5, 8}, {4, 8}, {3, 8}, {3, 8}}}

In[3]:= Graphics[
 MapIndexed[{EdgeForm[Black], White, 
    Rectangle[{0, 2 First[#2]}, {19, 2 First[#2] + 1}], Pink, 
    Function[z, 
      Rectangle[{z[[1]], 2 First[#2]}, {z[[2]], 2 First[#2] + 1}]] /@ 
     Partition[Join[{0}, Accumulate[#1[[All, 1]]]], 2, 1]} &,
  pointLists
  ]
 ]

```

Which gives this picture

 ![blocks.jpg](https://global.discourse-cdn.com/wolfram/original/2X/8/89312aa8d676e4e41a91b6892231994bc88e0f32.jpeg)

Adding the labels and so on would take a bit more work… 😉

So, how did I get to this pice of code (which I admit is a bit unreadable…). I solved individual component problems one by one and tested them as I went along. Then as each seemed to work, I incorporated them into the function that I was building up. To make the code clearer, I could break it up into individual pieces which each individually do part of the computation and then put them all into a Module with appropriately named local variables. But to paraphrase Fermat: I do not have time to write it all in the margin. My apologies! I have a busy week but was caught by the challenge of your problem!

---

<div class="post-metadata">

**Author:** ![22padrinos](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/22padrinos/32/29645_2.png) [@22padrinos](https://community.wolfram.com/u/22padrinos)\
**Post date:** [July 5, 2014, 2:18am UTC](https://community.wolfram.com/t/how-to-draw-rectangles-from-this-list/2554/3 "2014-07-05T02:18:11Z")

</div>

David Reiss infinite thanks for helping me with this problem, I have no words to thank you for your invaluable help, thank you for sharing your knowledge, that’s just what I need
