How to check if a point is inside a rectangle? There is a point $(x,y)$, and a rectangle $a(x_1,y_1),b(x_2,y_2),c(x_3,y_3),d(x_4,y_4)$, how can one check if the point inside the rectangle?


area of the rectangle
Ai: areas of the triangles shown in the pictures. (i = 1, 2, 3, 4)
ai: lengths of the edges shown in the pictures. (i = 1, 2, 3, 4)
bi: lengths of the line segments connecting the point and the corners. (i = 1, 2, 3, 4)
If the point is inside the rectangle, the following equation holds:
$ \mathbf{A = A_1 + A_2 + A_3 + A_4} $
If the point is outside the rectangle, the following inequality holds:
$ \mathbf{A > A_1 + A_2 + A_3 + A_4} $
How do we calculate A, A1, A2, A3, A4, a1, a2, a3, a4, b1, b2, b3 and b4?
First we calculate the edge lengths:
$
a_1 = \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2} \\
a_2 = \sqrt{(x_2 - x_3)^2 + (y_2 - y_3)^2} \\
a_3 = \sqrt{(x_3 - x_4)^2 + (y_3 - y_4)^2} \\
a_4 = \sqrt{(x_4 - x_1)^2 + (y_4 - y_1)^2}
$
Next we calculate the lengths of the line segments:
$
b_1 = \sqrt{(x_1 - x)^2 + (y_1 - y)^2} \\
b_2 = \sqrt{(x_2 - x)^2 + (y_2 - y)^2} \\
b_3 = \sqrt{(x_3 - x)^2 + (y_3 - y)^2} \\
b_4 = \sqrt{(x_4 - x)^2 + (y_4 - y)^2}
$
Then we calculate the areas using Heron's Formula:
$
A \,\,\, = a_1a_2 = a_2a_3 = a_3a_4 = a_4a_1 \\
u_1 = \frac{a_1 + b_1 + b_2}{2} \\
u_2 = \frac{a_2 + b_2 + b_3}{2} \\
u_3 = \frac{a_3 + b_3 + b_4}{2} \\
u_4 = \frac{a_4 + b_4 + b_1}{2} \\
A_1 = \sqrt{u_1(u_1 - a_1)(u_1 - b_1)(u_1 - b_2)} \\
A_2 = \sqrt{u_2(u_2 - a_2)(u_2 - b_2)(u_2 - b_3)} \\
A_3 = \sqrt{u_3(u_3 - a_3)(u_3 - b_3)(u_3 - b_4)} \\
A_4 = \sqrt{u_4(u_4 - a_4)(u_4 - b_4)(u_4 - b_1)}
$
Finally you can do the area test to check if the point is inside or outside the rectangle.
