# Martin Thoma

## How to check if a point is inside a rectangle

September 7th, 2012

A rectangle

I’ve just found this interesting question on StackExchange:

If you have a rectangle ABCD and point P. Is P inside ABCD?

## The idea

The idea how to solve this problem is simply beautiful.

If the point is in the rectangle, it divides it into four rectangles:

Divided rectangle

If P is not inside of ABCD, you end up with somethink like this:

Point is outside of rectangle

You might note that the area of the four triangles in is bigger than the area of the rectangle. So if the area is bigger, you know that the point is outside of the rectangle.

## Formulae

If you know the coordinates of the points, you can calculate the area of the rectangle like this:

$$A_\text{rectangle} = \frac{1}{2} \left| (y_{A}-y_{C})\cdot(x_{D}-x_{B}) + (y_{B}-y_{D})\cdot(x_{A}-x_{C})\right|$$

The area of a triangle is:
$$A_\text{triangle} = \frac{1}{2} (x_1(y_2-y_3) + x_2(y_3-y_1) + x_3(y_1-y_2))$$

## Python

```def isPinRectangle(r, P):
    """ 
        r: A list of four points, each has a x- and a y- coordinate
        P: A point
    """

    areaRectangle = 0.5*abs(
        #                 y_A      y_C      x_D      x_B          
                        (r[0][1]-r[2][1])*(r[3][0]-r[1][0]) 
        #                  y_B     y_D       x_A     x_C
                      + (r[1][1]-r[3][1])*(r[0][0]-r[2][0])
                    )

    ABP = 0.5*(
             r[0][0]*(r[1][1]-r[2][1])
            +r[1][0]*(r[2][1]-r[0][1])
            +r[2][0]*(r[0][1]-r[1][1])
          )
    BCP = 0.5*(
             r[1][0]*(r[2][1]-r[3][1])
            +r[2][0]*(r[3][1]-r[1][1])
            +r[3][0]*(r[1][1]-r[2][1])
          )
    CDP = 0.5*(
             r[2][0]*(r[3][1]-r[0][1])
            +r[3][0]*(r[0][1]-r[2][1])
            +r[0][0]*(r[2][1]-r[3][1])
          )
    DAP = 0.5*(
             r[3][0]*(r[0][1]-r[1][1])
            +r[0][0]*(r[1][1]-r[3][1])
            +r[1][0]*(r[3][1]-r[0][1])
          )
    return areaRectangle == (ABP+BCP+CDP+DAP)```
Posted in Code | Tags: Geometry, Python by Martin Thoma on September 7th, 2012
You can leave a response, or trackback from your own site.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 2, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.8874073028564453, "perplexity_flag": "middle"}