# Thread: Rhombus inside Rectangle : testing coordinates

1. ## Rhombus inside Rectangle : testing coordinates

Hi, I am building a web game, and I came across a small issue you might help me with:

I have a rhombus inside a rectangle, and some random coordinates inside the rectangle. I have to test if these coordinates are inside the rhombus or outside of it. That’s it!

I know everything about the rectangle and rhombus (angles & length)
I could devise an ugly switch case to determine this, but I was wondering if they’re wasn’t a better (and faster ) approach to this problem.

2. Consider centre of the rectangle as origin and let the diagonals of the rhombus lie along the $x-$axis and the $y-$axis.The coordinates of the vertices of the rhombus are $A(\frac{-d}{2},0),D(0,\frac{-D}{2}),C(\frac{d}{2},0), B(0,\frac{D}{2}).$

The cartesian equations of the sides $AD,DC,CB, BA$ are respectively

$\frac{X}{\frac{-d}{2}}+\frac{Y}{\frac{-D}{2}}-1=0$

$\frac{X}{\frac{d}{2}}+\frac{Y}{\frac{-D}{2}}-1=0$

$\frac{X}{\frac{d}{2}}+\frac{Y}{\frac{D}{2}}-1=0$

$\frac{X}{\frac{-d}{2}}+\frac{Y}{\frac{D}{2}}-1=0$

When we substitute $(0,0)$ in the LHS of the above equations we get $-1$ i.e. a negative quantity is obtained in all the four cases.Therefore all points inside the rhombus when substituted in the LHS of the above equations must yield a negative quantity.

Hope this helps.