# Function rectangle

## Synopse
Create a binary rectangle image.

- **g = rectangle(s, r, c) **

    - **g**: Image.
    - **s**: Image. [rows cols], output image dimensions.
    - **r**: Double. [rrows ccols], rectangle image dimensions.
    - **c**: Image. [row0 col0], center of the rectangle.

## Description
Creates a binary image with dimensions given by s, rectangle dimensions given by r and center given by c. The pixels inside the rectangle are one and outside zero.


```python
import numpy as np

def rectangle(s, r, c):

    rows,  cols  = s[0], s[1]
    rrows, rcols = r[0], r[1]
    rr0,   cc0   = c[0], c[1]
    rr, cc = np.meshgrid(np.arange(rows), np.arange(cols), indexing='ij')

    min_row, max_row = rr0-rrows//2, rr0+rrows//2
    min_col, max_col = cc0-rcols//2, cc0+rcols//2

    g1 = (min_row <= rr) & (max_row > rr)
    g2 = (min_col <= cc) & (max_col > cc)

    g = g1 & g2
    return g
```

## Examples


```python
testing = (__name__ == "__main__")
if testing:
    ! jupyter nbconvert --to python rectangle.ipynb
    import numpy as np
    import sys,os
    ia898path = os.path.abspath('../../')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia

```

    [NbConvertApp] Converting notebook rectangle.ipynb to python
    [NbConvertApp] Writing 1808 bytes to rectangle.py


### Example 1


```python
if testing:
    F = ia.rectangle([7,9], [3,2], [3,4])
    print(F)
```

    [[False False False False False False False False False]
     [False False False False False False False False False]
     [False False False  True  True False False False False]
     [False False False  True  True False False False False]
     [False False False False False False False False False]
     [False False False False False False False False False]
     [False False False False False False False False False]]


- **Example 2**


```python
if testing:
    F = ia.rectangle([200,300], [90,120], [70,120])
    ia.adshow(ia.normalize(F))
```


<head><style>        table, th, td { border: 0px solid black;        text-align: center;border-collapse: collapse;}</style></head>        <body><table border="0"><td>            <table><tr><td></td></tr>            <tr><td align='center'></td></tr></table></td><tr></tr></table></body>


## Equation

\begin{equation}
  g(x,y)=\begin{cases}
    1, & \text{if } x_\text{min} \leq x < x_\text{max} \text{ and } y_\text{min} \leq y < y_\text{max}.\\
    0, & \text{otherwise}.
  \end{cases}
\end{equation}

## Contributions

Lucas de Vasconcellos Teixeira, 1st semester 2017

e326c423dfcb308271cd1a1c7ff34f78a936adba
ipynb
Jupyter Notebook
src/rectangle.ipynb
ferseiti/ia898
91f49db3a3a5178d3733476ee63536c4c04afc91
MIT
2017-07-12T17:32:44.000Z
2021-08-19T13:30:46.000Z
src/rectangle.ipynb
ferseiti/ia898
91f49db3a3a5178d3733476ee63536c4c04afc91
MIT
2017-06-29T13:34:26.000Z
2017-06-29T13:34:26.000Z
src/rectangle.ipynb
ferseiti/ia898
91f49db3a3a5178d3733476ee63536c4c04afc91
MIT
2017-03-05T17:40:48.000Z
2020-03-09T17:01:20.000Z
Qwen/Qwen-72B
1. YES
2. YES